summaryrefslogtreecommitdiff
path: root/src/pyssg/cfg
diff options
context:
space:
mode:
Diffstat (limited to 'src/pyssg/cfg')
-rw-r--r--src/pyssg/cfg/__init__.py0
-rw-r--r--src/pyssg/cfg/configuration.py51
-rw-r--r--src/pyssg/cfg/yaml_parser.py23
3 files changed, 74 insertions, 0 deletions
diff --git a/src/pyssg/cfg/__init__.py b/src/pyssg/cfg/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/pyssg/cfg/__init__.py
diff --git a/src/pyssg/cfg/configuration.py b/src/pyssg/cfg/configuration.py
new file mode 100644
index 0000000..55bfa0b
--- /dev/null
+++ b/src/pyssg/cfg/configuration.py
@@ -0,0 +1,51 @@
+import sys
+from importlib.metadata import version
+from logging import Logger, getLogger
+from typing import Any
+
+from pyssg.utils import get_expanded_path, get_time_now
+from pyssg.cfg.yaml_parser import get_yaml
+
+log: Logger = getLogger(__name__)
+VERSION: str = version('pyssg')
+
+
+def __expand_all_paths(config: list[dict[str, Any]]) -> None:
+ for option in config[0]['path'].keys():
+ path: str = get_expanded_path(config[0]['path'][option])
+ config[0]['path'][option] = path
+
+
+def __add_mandatory_config(config: list[dict[str, Any]]) -> None:
+ if 'fmt' not in config[0]:
+ config[0]['fmt'] = dict()
+ if 'rss_date' not in config[0]['fmt']:
+ config[0]['fmt']['rss_date'] = '%a, %d %b %Y %H:%M:%S GMT'
+ if 'sitemap_date' not in config[0]['fmt']:
+ config[0]['fmt']['sitemap_date'] = '%Y-%m-%d'
+
+ if 'info' not in config[0]:
+ config[0]['info'] = dict()
+ config[0]['info']['version'] = VERSION
+ config[0]['info']['rss_run_date'] = get_time_now('rss_date')
+ config[0]['info']['sitemap_run_date'] = get_time_now('sitemap_date')
+
+
+def get_parsed_config(path: str) -> list[dict[str, Any]]:
+ config: list[dict[str, Any]] = get_yaml(path)
+ log.info('found %s documents for config "%s"', len(config), path)
+
+ if len(config) < 2:
+ log.error('config file requires at least 2 documents:'
+ ' main config and root dir config')
+ sys.exit(1)
+
+ __expand_all_paths(config)
+ __add_mandatory_config(config)
+
+ if config[1]['dir'] != "/":
+ log.error('the first directory config needs to be'
+ ' root (/), found %s instead', config[1]['dir'])
+ sys.exit(1)
+ return config
+
diff --git a/src/pyssg/cfg/yaml_parser.py b/src/pyssg/cfg/yaml_parser.py
new file mode 100644
index 0000000..dacad70
--- /dev/null
+++ b/src/pyssg/cfg/yaml_parser.py
@@ -0,0 +1,23 @@
+import yaml
+from yaml import SafeLoader
+from yaml.nodes import SequenceNode
+from typing import Any
+
+
+# required to concat values in yaml using !join [value, value, ...]
+def __join_constructor(loader: SafeLoader, node: SequenceNode) -> str:
+ seq = loader.construct_sequence(node)
+ return ''.join([str(i) for i in seq])
+
+
+def setup_custom_yaml() -> None:
+ SafeLoader.add_constructor('!join', __join_constructor)
+
+
+def get_yaml(path: str) -> list[dict[str, Any]]:
+ all_docs: list[dict[str, Any]] = []
+ with open(path, 'r') as f:
+ for doc in yaml.safe_load_all(f):
+ all_docs.append(doc)
+ return all_docs
+