From 2543260a4111faa1599631dd5d462db5ed85abc0 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 1 Jul 2023 15:31:40 -0700 Subject: [PATCH] start scaffolding new config module --- jrnl/config/ArgsConfigReader.py | 21 +++++++++++++++++ jrnl/config/BaseConfigReader.py | 19 ++++++++++++++++ jrnl/config/Config.py | 20 +++++++++++++++++ jrnl/config/DefaultConfigReader.py | 22 ++++++++++++++++++ jrnl/config/FileConfigReader.py | 22 ++++++++++++++++++ jrnl/config/__init__.py | 36 ++++++++++++++++++++++++++++++ jrnl/controller.py | 17 +++++++++----- 7 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 jrnl/config/ArgsConfigReader.py create mode 100644 jrnl/config/BaseConfigReader.py create mode 100644 jrnl/config/Config.py create mode 100644 jrnl/config/DefaultConfigReader.py create mode 100644 jrnl/config/FileConfigReader.py create mode 100644 jrnl/config/__init__.py diff --git a/jrnl/config/ArgsConfigReader.py b/jrnl/config/ArgsConfigReader.py new file mode 100644 index 00000000..478ce882 --- /dev/null +++ b/jrnl/config/ArgsConfigReader.py @@ -0,0 +1,21 @@ +# Copyright © 2012-2023 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +import logging +from .BaseConfigReader import BaseConfigReader + + +class ArgsConfigReader(BaseConfigReader): + def __init__(self, args): + logging.debug("start") + super() + self.args = args + + def read(self): + self._parse_args() + # do some actual reading + + def _parse_args(self): + # read self.args + # update self.cofig somehow + pass diff --git a/jrnl/config/BaseConfigReader.py b/jrnl/config/BaseConfigReader.py new file mode 100644 index 00000000..8c1aa3ff --- /dev/null +++ b/jrnl/config/BaseConfigReader.py @@ -0,0 +1,19 @@ +# Copyright © 2012-2023 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +import logging +from abc import ABC +from abc import abstractmethod + + +class BaseConfigReader(ABC): + def __init__(self): + logging.debug("start") + self.config: dict = {} + + @abstractmethod + def read(self): + pass + + def get_config(self): + return self.config diff --git a/jrnl/config/Config.py b/jrnl/config/Config.py new file mode 100644 index 00000000..f3aabc01 --- /dev/null +++ b/jrnl/config/Config.py @@ -0,0 +1,20 @@ +# Copyright © 2012-2023 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +from collections import abc + +class Config(abc.MutableMapping): + def __init__(self, configs): + pass + + + def add_config(config, priority): + + def sub_configs(): + return [ + one, + two, + three, + ] + + diff --git a/jrnl/config/DefaultConfigReader.py b/jrnl/config/DefaultConfigReader.py new file mode 100644 index 00000000..b2baaa81 --- /dev/null +++ b/jrnl/config/DefaultConfigReader.py @@ -0,0 +1,22 @@ +# Copyright © 2012-2023 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +import logging +from .BaseConfigReader import BaseConfigReader +from pathlib import PurePath + + +class DefaultConfigReader(BaseConfigReader): + def __init__(self, filename: str): + logging.debug("start") + super() + self.filename: PurePath = PurePath(filename) + + def read(self): + self._parse_args() + # do some actual reading + + def _parse_args(self): + # read self.args + # update self.cofig somehow + pass diff --git a/jrnl/config/FileConfigReader.py b/jrnl/config/FileConfigReader.py new file mode 100644 index 00000000..7fd05c26 --- /dev/null +++ b/jrnl/config/FileConfigReader.py @@ -0,0 +1,22 @@ +# Copyright © 2012-2023 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +import logging +from .BaseConfigReader import BaseConfigReader +from pathlib import PurePath + + +class FileConfigReader(BaseConfigReader): + def __init__(self, filename: str): + logging.debug("start") + super() + self.filename: PurePath = PurePath(filename) + + def read(self): + self._parse_args() + # do some actual reading + + def _parse_args(self): + # read self.args + # update self.cofig somehow + pass diff --git a/jrnl/config/__init__.py b/jrnl/config/__init__.py new file mode 100644 index 00000000..497304bc --- /dev/null +++ b/jrnl/config/__init__.py @@ -0,0 +1,36 @@ +# Copyright © 2012-2023 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html + +from .Config import Config +from .BaseConfigReader import BaseConfigReader +from .DefaultConfigReader import DefaultConfigReader +from .FileConfigReader import FileConfigReader +from .ArgsConfigReader import ArgsConfigReader + + +def get_config(args): + config = Config() + + try: + # these are in ascending priority (last one has most priority) + config.add_config([ + DefaultConfigReader(), + ]) + + config.add_config([ + FileConfigReader(args.config_file), + FileConfigReader(config.get_config_path()), + FileConfigReader(jrnlV1Path), + ], required=True) + + config.add_config([ + ArgsConfigReader(args.config_override), + ]) + + # config.add_config(EnvConfigReader(env.whatever)) + config.validate() + + except e: + # TODO: catch warnings instead of fatal exceptions + + return config diff --git a/jrnl/controller.py b/jrnl/controller.py index e0e222b5..bb7d2f7d 100644 --- a/jrnl/controller.py +++ b/jrnl/controller.py @@ -8,6 +8,7 @@ from typing import TYPE_CHECKING from jrnl import install from jrnl import plugins from jrnl import time +from jrnl.config import get_config from jrnl.config import DEFAULT_JOURNAL_KEY from jrnl.config import get_config_path from jrnl.config import get_journal_name @@ -47,15 +48,21 @@ def run(args: "Namespace"): if callable(args.preconfig_cmd): return args.preconfig_cmd(args) - # Load the config, and extract journal name - config = install.load_or_install_jrnl(args.config_file_path) - original_config = config.copy() + config = get_config(args) + + if config.needs_upgrade(): + upgrade.run_upgrade(config) + + + # old code + # config = install.load_or_install_jrnl(args.config_file_path) + # original_config = config.copy() # Apply config overrides - config = apply_overrides(args, config) + # config = apply_overrides(args, config) args = get_journal_name(args, config) - config = scope_config(config, args.journal_name) + # config = scope_config(config, args.journal_name) # Run post-config command now that config is ready if callable(args.postconfig_cmd):