From a271cf22b49452aedb3e97c5e3c2306d81391861 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Sat, 2 Mar 2024 07:38:48 -0500 Subject: [PATCH] check for config in multiple directories --- relay/application.py | 2 +- relay/config.py | 21 ++++++++++++++++++++- relay/manage.py | 4 ++-- requirements.txt | 1 + 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/relay/application.py b/relay/application.py index 05aa8cc..9a49433 100644 --- a/relay/application.py +++ b/relay/application.py @@ -35,7 +35,7 @@ if typing.TYPE_CHECKING: class Application(web.Application): DEFAULT: Application = None - def __init__(self, cfgpath: str): + def __init__(self, cfgpath: str | None): web.Application.__init__(self, middlewares = [ handle_api_path diff --git a/relay/config.py b/relay/config.py index 3512726..e61c99a 100644 --- a/relay/config.py +++ b/relay/config.py @@ -7,6 +7,7 @@ import typing import yaml from pathlib import Path +from platformdirs import user_config_dir from .misc import IS_DOCKER @@ -51,7 +52,7 @@ if IS_DOCKER: class Config: def __init__(self, path: str, load: bool = False): - self.path = Path(path).expanduser().resolve() + self.path = Config.get_config_dir() self.listen = None self.port = None @@ -82,6 +83,24 @@ class Config: self.save() + @staticmethod + def get_config_dir(path: str | None = None) -> Path: + if path: + return Path(path).expanduser().resolve() + + dirs = ( + Path("relay.yaml").resolve(), + Path(user_config_dir("activityrelay"), "relay.yaml"), + Path("/etc/activityrelay/relay.yaml") + ) + + for directory in dirs: + if directory.exists(): + return directory + + return dirs[0] + + @property def sqlite_path(self) -> Path: if not os.path.isabs(self.sq_path): diff --git a/relay/manage.py b/relay/manage.py index 392f97b..01e034e 100644 --- a/relay/manage.py +++ b/relay/manage.py @@ -59,10 +59,10 @@ def check_alphanumeric(text: str) -> str: @click.group('cli', context_settings={'show_default': True}, invoke_without_command=True) -@click.option('--config', '-c', default='relay.yaml', help='path to the relay\'s config') +@click.option('--config', '-c', help='path to the relay\'s config') @click.version_option(version=__version__, prog_name='ActivityRelay') @click.pass_context -def cli(ctx: click.Context, config: str) -> None: +def cli(ctx: click.Context, config: str | None) -> None: ctx.obj = Application(config) if not ctx.invoked_subcommand: diff --git a/requirements.txt b/requirements.txt index 0439a98..310e1eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ barkshark-sql@https://git.barkshark.xyz/barkshark/bsql/archive/0.1.1.tar.gz click>=8.1.2 hamlish-jinja@https://git.barkshark.xyz/barkshark/hamlish-jinja/archive/0.3.5.tar.gz hiredis==2.3.2 +platformdirs==4.2.0 pyyaml>=6.0 redis==5.0.1