diff --git a/archmgr.py b/archmgr.py index 852b008..6274865 100755 --- a/archmgr.py +++ b/archmgr.py @@ -9,6 +9,7 @@ import commands.init as init import commands.pull as pull import commands.push as push import commands.prepare as setup +from config import load_config if __name__ == "__main__": args, ap = cliargs.add_cli_args() @@ -29,6 +30,7 @@ if __name__ == "__main__": \\___/ """) + print(load_config("config.yml")) try: if args.cmd == "commit": commit.commit(args.force, args.no_render) diff --git a/config.schema.json b/config.schema.json index 8df178a..e081743 100644 --- a/config.schema.json +++ b/config.schema.json @@ -88,6 +88,12 @@ }, "maxItems": 5, "minItems": 1 + }, + "count": { + "type": "number", + "description": "The number of mirrors to add to the list", + "maximum": 20, + "minimum": 3 } }, "required": [ diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..85222ac --- /dev/null +++ b/config/__init__.py @@ -0,0 +1,31 @@ +from typing import Any, cast +import yaml + +from config import validator +from config.merger import merge_configs + + +def _load_config_file(file: str): + with open(file, "r") as f: + parsed = yaml.load(f, Loader=yaml.FullLoader) + return parsed + + +def load_config(file: str): + # Load and validate initial config + try: + loaded_conf = _load_config_file(file) + except Exception: + return {} + if not validator.validate(loaded_conf): + return {} + + conf = cast(dict[str, Any], loaded_conf) + requires = cast(list[str], conf["requires"]) + conf.pop("requires") + + # Recursively load files + for conf_file in requires: + conf = merge_configs(conf, load_config(conf_file)) + + return conf diff --git a/config/merger.py b/config/merger.py new file mode 100644 index 0000000..6699e35 --- /dev/null +++ b/config/merger.py @@ -0,0 +1,6 @@ +def merge_configs(config: dict, new_config: dict): + if len(new_config) == 0 or len(config) == 0: + return config + + # Merge configs + return config diff --git a/config/parser.py b/config/parser.py deleted file mode 100644 index e69de29..0000000 diff --git a/config/validator.py b/config/validator.py index 7af6606..7a4611f 100644 --- a/config/validator.py +++ b/config/validator.py @@ -1,2 +1,18 @@ -def validate(): - pass +import json +import jsonschema + +with open("config.schema.json") as file: + schema = json.load(file) + + +def validate(config): + try: + jsonschema.validate(config, schema) + except jsonschema.SchemaError: + print("Schema invalid") + return False + except jsonschema.ValidationError: + print("Config invalid") + return False + + return True diff --git a/requirements.txt b/requirements.txt index 2457530..ee1cbde 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +pyyaml pylette argcomplete inquirer