Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Drop support for system-wide configuration file #187

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .run/sodalite.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
<env name="DB_PATH" value="db.sqlite" />
<env name="CONFIG_FILE" value="docs/sodalite.conf" />
</envs>
<option name="SDK_HOME" value="$PROJECT_DIR$/venv/bin/python" />
<option name="SDK_HOME" value="$PROJECT_DIR$/.venv/bin/python" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
Expand Down
6 changes: 2 additions & 4 deletions docs/sodalite.1
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,10 @@ navigational preferences.
Upon startup, \f[C]sodalite\f[R] looks in following places for its
configuration:
.IP "1." 3
\f[C]$XDG_CONFIG_HOME/sodalite/sodalite.yml\f[R] (user specific
\f[C]$XDG_CONFIG_HOME/sodalite/sodalite.conf\f[R] (user specific
configuration).
If \f[C]$XDG_CONFIG_HOME\f[R] is not set, falls back to
\f[C]$HOME/.config/sodalite/sodalite.yml\f[R]
.IP "2." 3
\f[C]/etc/sodalite.yml\f[R] (system-wide configuration)
\f[C]$HOME/.config/sodalite/sodalite.conf\f[R]
.PP
The configuration is written in
YAML (https://learnxinyminutes.com/docs/yaml/).
Expand Down
5 changes: 2 additions & 3 deletions docs/sodalite.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,8 @@ Configuration
=============
Upon startup, `sodalite` looks in following places for its configuration:

1. `$XDG_CONFIG_HOME/sodalite/sodalite.yml` (user specific configuration).
If `$XDG_CONFIG_HOME` is not set, falls back to `$HOME/.config/sodalite/sodalite.yml`
2. `/etc/sodalite.yml` (system-wide configuration)
1. `$XDG_CONFIG_HOME/sodalite/sodalite.conf` (user specific configuration).
If `$XDG_CONFIG_HOME` is not set, falls back to `$HOME/.config/sodalite/sodalite.conf`

The configuration is written in [YAML](https://learnxinyminutes.com/docs/yaml/).

Expand Down
33 changes: 21 additions & 12 deletions sodalite/core/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import functools
import logging
import os
import shutil
from dataclasses import dataclass
from pathlib import Path
from typing import Optional, Union
Expand All @@ -17,20 +19,27 @@ class ConfigNotFound(Exception):
pass


_ENV_CONFIG_FILE = 'CONFIG_FILE'


@functools.cache
def _config_file() -> Path:
config_file_paths = [
env.config_file(),
env.USER_CONFIG / 'sodalite.conf',
Path('/etc/sodalite.conf'),
Path('/usr/local/etc/sodalite.conf'),
Path('/usr/share/sodalite/sodalite.conf'),
]

for file in config_file_paths:
if file.exists():
logger.debug(f"Using config file '{file}'")
return file
env_config_file = os.getenv(_ENV_CONFIG_FILE)
if env_config_file:
config_file = Path(env_config_file).absolute()
if config_file.exists():
logger.debug(f"Using config file from env variable: '{config_file}'")
return config_file
else:
config_file = Path(env.USER_CONFIG / 'sodalite.conf')
if config_file.exists():
logger.debug(f"Using config file '{config_file}'")
return config_file
else:
logger.info(f"Creating config file '{config_file}'")
shutil.copy(src=env.DOCS / 'sodalite.conf', dst=config_file)
return config_file

raise ConfigNotFound()


Expand Down
2 changes: 1 addition & 1 deletion sodalite/core/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class History:
"""

def __init__(self, history: list[Path] = None, index: int = 0, persist: bool = False):
self._history = history or [env.HOME]
self._history = history or [Path.home()]
self._index = index
if persist:
atexit.register(self.save)
Expand Down
3 changes: 1 addition & 2 deletions sodalite/ui/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from sodalite.ui import graphics, viewmodel, notify, theme
from sodalite.ui.entrylist import EntryList
from sodalite.ui.viewmodel import Mode, ViewModel
from sodalite.util import env

if TYPE_CHECKING:
from sodalite.ui.graphics import MainFrame
Expand Down Expand Up @@ -197,7 +196,7 @@ def go_to_parent(self):
self.clear_filter()

def go_to_home(self):
self.navigator.visit_path(env.HOME)
self.navigator.visit_path(Path.home())
self.clear_filter()

def go_to_root(self):
Expand Down
9 changes: 5 additions & 4 deletions sodalite/ui/mainpane.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from pathlib import Path

import urwid

Expand All @@ -8,7 +9,7 @@
from sodalite.ui.entrylist import EntryList
from sodalite.ui.filepreview import FilePreview
from sodalite.ui.viewmodel import ViewModel
from sodalite.util import env, pubsub
from sodalite.util import pubsub

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -43,10 +44,10 @@ def on_entry_changed(self, entry: Entry) -> None:

def _update_title(self, entry: Entry) -> None:
cwd = entry.path
if cwd == env.HOME:
if cwd == Path.home():
title = '~'
elif cwd.is_relative_to(env.HOME):
title = f"~/{cwd.relative_to(env.HOME)}"
elif cwd.is_relative_to(Path.home()):
title = f"~/{cwd.relative_to(Path.home())}"
else:
title = str(cwd)
self.box.set_title(title)
19 changes: 6 additions & 13 deletions sodalite/util/env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import functools
import logging.handlers
import os

Expand All @@ -25,24 +24,18 @@
ENV_DATA_DIR = 'DATA_DIR'
ENV_DB_PATH = 'DB_PATH'

HOME = Path.home()
DATA = Path(os.getenv(ENV_DATA_DIR, f'/usr/share/{PROGRAM_NAME}/')).absolute()
USER_DATA = Path(os.getenv('XDG_DATA_HOME', HOME / '.local/share')).absolute() / PROGRAM_NAME
USER_CONFIG = Path(os.getenv('XDG_CONFIG_HOME', HOME / '.config/')).absolute() / PROGRAM_NAME
ENV_CONFIG_FILE = 'CONFIG_FILE'


@functools.lru_cache
def config_file() -> Path:
return Path(os.getenv(ENV_CONFIG_FILE, '/etc/sodalite.conf')).absolute()

USER_DATA = Path(os.getenv('XDG_DATA_HOME', Path.home() / '.local/share')).absolute() / PROGRAM_NAME
USER_CONFIG = Path(os.getenv('XDG_CONFIG_HOME', Path.home() / '.config/')).absolute() / PROGRAM_NAME

db_file = Path(os.getenv(ENV_DB_PATH, USER_DATA / 'db.sqlite')).absolute()
logger.debug(f"Using database: {db_file.absolute()}")

buffer = USER_DATA / 'buffer'

dirs = [HOME, DATA, USER_DATA, USER_CONFIG, buffer]
dirs = [USER_DATA, USER_CONFIG, buffer]
for directory in dirs:
os.makedirs(directory, exist_ok=True)

logger.debug(f"Using database: {db_file.absolute()}")
ROOT = Path(__file__).parent.parent.parent.absolute()
DOCS = ROOT / 'docs'