mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-07-01 06:46:14 +02:00
start scaffolding new config module
This commit is contained in:
parent
7a9bc929a5
commit
2543260a41
7 changed files with 152 additions and 5 deletions
21
jrnl/config/ArgsConfigReader.py
Normal file
21
jrnl/config/ArgsConfigReader.py
Normal file
|
@ -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
|
19
jrnl/config/BaseConfigReader.py
Normal file
19
jrnl/config/BaseConfigReader.py
Normal file
|
@ -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
|
20
jrnl/config/Config.py
Normal file
20
jrnl/config/Config.py
Normal file
|
@ -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,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
22
jrnl/config/DefaultConfigReader.py
Normal file
22
jrnl/config/DefaultConfigReader.py
Normal file
|
@ -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
|
22
jrnl/config/FileConfigReader.py
Normal file
22
jrnl/config/FileConfigReader.py
Normal file
|
@ -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
|
36
jrnl/config/__init__.py
Normal file
36
jrnl/config/__init__.py
Normal file
|
@ -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
|
|
@ -8,6 +8,7 @@ from typing import TYPE_CHECKING
|
||||||
from jrnl import install
|
from jrnl import install
|
||||||
from jrnl import plugins
|
from jrnl import plugins
|
||||||
from jrnl import time
|
from jrnl import time
|
||||||
|
from jrnl.config import get_config
|
||||||
from jrnl.config import DEFAULT_JOURNAL_KEY
|
from jrnl.config import DEFAULT_JOURNAL_KEY
|
||||||
from jrnl.config import get_config_path
|
from jrnl.config import get_config_path
|
||||||
from jrnl.config import get_journal_name
|
from jrnl.config import get_journal_name
|
||||||
|
@ -47,15 +48,21 @@ def run(args: "Namespace"):
|
||||||
if callable(args.preconfig_cmd):
|
if callable(args.preconfig_cmd):
|
||||||
return args.preconfig_cmd(args)
|
return args.preconfig_cmd(args)
|
||||||
|
|
||||||
# Load the config, and extract journal name
|
config = get_config(args)
|
||||||
config = install.load_or_install_jrnl(args.config_file_path)
|
|
||||||
original_config = config.copy()
|
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
|
# Apply config overrides
|
||||||
config = apply_overrides(args, config)
|
# config = apply_overrides(args, config)
|
||||||
|
|
||||||
args = get_journal_name(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
|
# Run post-config command now that config is ready
|
||||||
if callable(args.postconfig_cmd):
|
if callable(args.postconfig_cmd):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue