Skip to content

Commit

Permalink
feat(config): additionally check /usr/local/share and /usr/share for …
Browse files Browse the repository at this point in the history
…system-wide config

For some distros (immutable ones?), contents for /etc may not be
easily packaged, so add support for /usr/share locations to help.

This is only enabled for Linux hosts for now, because conventions for
system-wide config files are different on other platforms.
  • Loading branch information
xen0n committed Sep 29, 2024
1 parent 4f453a2 commit e3da53e
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions ruyi/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os.path
from os import PathLike
import pathlib
import sys
import tomllib
from typing import Any, Iterable, NotRequired, Self, TypedDict

Expand All @@ -10,6 +11,16 @@
from ..utils.xdg_basedir import XDGBaseDir
from .news import NewsReadStatusStore

PRESET_GLOBAL_CONFIG_LOCATIONS: list[str] = []

if sys.platform == "linux":
PRESET_GLOBAL_CONFIG_LOCATIONS = [
# TODO: enable distro packagers to customize the $PREFIX to suit their
# particular FS layout if necessary.
"/usr/share/ruyi/system-wide-config.toml",
"/usr/local/share/ruyi/system-wide-config.toml",
]

DEFAULT_APP_NAME = "ruyi"
DEFAULT_REPO_URL = "https://github.com/ruyisdk/packages-index.git"
DEFAULT_REPO_BRANCH = "main"
Expand Down Expand Up @@ -195,6 +206,16 @@ def ensure_state_dir(self) -> os.PathLike[Any]:
p.mkdir(parents=True, exist_ok=True)
return p

def iter_preset_configs(self) -> Iterable[os.PathLike[Any]]:
"""
Yields possible Ruyi config files in all preset config path locations,
sorted by precedence from lowest to highest (so that each file may be
simply applied consecutively).
"""

for path in PRESET_GLOBAL_CONFIG_LOCATIONS:
yield pathlib.Path(path)

def iter_xdg_configs(self) -> Iterable[os.PathLike[Any]]:
"""
Yields possible Ruyi config files in all XDG config paths, sorted by precedence
Expand All @@ -204,20 +225,27 @@ def iter_xdg_configs(self) -> Iterable[os.PathLike[Any]]:
for config_dir in reversed(list(self._dirs.app_config_dirs)):
yield config_dir / "config.toml"

def try_apply_config_file(self, path: os.PathLike[Any]) -> None:
try:
with open(path, "rb") as fp:
data: Any = tomllib.load(fp)
except FileNotFoundError:
return

log.D(f"applying config: {data}")
self.apply_config(data)

@classmethod
def load_from_config(cls) -> Self:
obj = cls()

for config_path in obj.iter_preset_configs():
log.D(f"trying config file from preset location: {config_path}")
obj.try_apply_config_file(config_path)

for config_path in obj.iter_xdg_configs():
log.D(f"trying config file: {config_path}")
try:
with open(config_path, "rb") as fp:
data: Any = tomllib.load(fp)
except FileNotFoundError:
continue

log.D(f"applying config: {data}")
obj.apply_config(data)
log.D(f"trying config file from XDG path: {config_path}")
obj.try_apply_config_file(config_path)

# let environment variable take precedence
if is_env_var_truthy(ENV_TELEMETRY_OPTOUT_KEY):
Expand Down

0 comments on commit e3da53e

Please sign in to comment.