mirror of
https://github.com/bellingcat/auto-archiver.git
synced 2026-06-13 05:38:29 +03:00
Switch back to using yaml with dot notation
(two simple helper functions to convert between dot and dict notation)
This commit is contained in:
@@ -4,10 +4,13 @@ It supports CLI argument parsing, loading from YAML file, and overrides to allow
|
||||
flexible setup in various environments.
|
||||
|
||||
"""
|
||||
import argparse
|
||||
from configparser import ConfigParser
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
import argparse
|
||||
import yaml
|
||||
from dataclasses import dataclass, field
|
||||
from collections import OrderedDict
|
||||
|
||||
from .loader import MODULE_TYPES
|
||||
|
||||
# configurable_parents = [
|
||||
# Feeder,
|
||||
@@ -47,21 +50,56 @@ from dataclasses import dataclass, field
|
||||
# parser.add_argument('--config', action='store', dest='config', help='the filename of the YAML configuration file (defaults to \'config.yaml\')', default='orchestration.yaml')
|
||||
# parser.add_argument('--version', action='version', version=__version__)
|
||||
|
||||
|
||||
EMPTY_CONFIG = {
|
||||
"steps": dict((f"{module_type}s", []) for module_type in MODULE_TYPES)
|
||||
}
|
||||
class LoadFromFile (argparse.Action):
|
||||
def __call__ (self, parser, namespace, values, option_string = None):
|
||||
with values as f:
|
||||
# parse arguments in the file and store them in the target namespace
|
||||
parser.parse_args(f.read().split(), namespace)
|
||||
|
||||
def read_config(config_filename: str) -> dict:
|
||||
config = ConfigParser()
|
||||
config.read(config_filename)
|
||||
# setup basic format
|
||||
if 'STEPS' not in config.sections():
|
||||
config.add_section("STEPS")
|
||||
def to_dot_notation(yaml_conf: str) -> argparse.ArgumentParser:
|
||||
dotdict = {}
|
||||
for step, vals in yaml_conf.pop('steps', {}).items():
|
||||
if vals:
|
||||
dotdict[f"{step}s"] = vals
|
||||
|
||||
def process_subdict(subdict, prefix=""):
|
||||
for key, value in subdict.items():
|
||||
if type(value) == dict:
|
||||
process_subdict(value, f"{prefix}{key}.")
|
||||
else:
|
||||
dotdict[f"{prefix}{key}"] = value
|
||||
|
||||
process_subdict(yaml_conf)
|
||||
return dotdict
|
||||
|
||||
def merge_dicts(dotdict, yaml_dict):
|
||||
def process_subdict(subdict, prefix=""):
|
||||
for key, value in subdict.items():
|
||||
if "." in key:
|
||||
keys = key.split(".")
|
||||
subdict = yaml_dict
|
||||
for k in keys[:-1]:
|
||||
subdict = subdict.setdefault(k, {})
|
||||
subdict[keys[-1]] = value
|
||||
else:
|
||||
yaml_dict[key] = value
|
||||
|
||||
process_subdict(dotdict)
|
||||
return yaml_dict
|
||||
|
||||
def read_yaml(yaml_filename: str) -> dict:
|
||||
|
||||
try:
|
||||
with open(yaml_filename, "r", encoding="utf-8") as inf:
|
||||
config = yaml.safe_load(inf)
|
||||
except FileNotFoundError:
|
||||
config = EMPTY_CONFIG
|
||||
|
||||
return config
|
||||
|
||||
def store_config(config: ConfigParser, config_filename: str):
|
||||
with open(config_filename, "w", encoding="utf-8") as outf:
|
||||
config.write(outf)
|
||||
def store_yaml(config: dict, yaml_filename: str):
|
||||
with open(yaml_filename, "w", encoding="utf-8") as outf:
|
||||
yaml.dump(config, outf, default_flow_style=False)
|
||||
Reference in New Issue
Block a user