From eda359a1ef1d8670c2c1e24f29e5fa12696d9dae Mon Sep 17 00:00:00 2001 From: Patrick Robertson Date: Thu, 20 Feb 2025 13:10:23 +0000 Subject: [PATCH] Fix json loader - it should go in 'validators' not 'utils' Fixes #214 --- docs/scripts/scripts.py | 2 +- src/auto_archiver/core/config.py | 2 +- src/auto_archiver/core/validators.py | 6 +++++- src/auto_archiver/modules/gsheet_feeder/__manifest__.py | 2 +- src/auto_archiver/modules/gsheet_feeder/gsheet_feeder.py | 5 ++--- .../modules/telethon_extractor/__manifest__.py | 2 +- src/auto_archiver/utils/misc.py | 4 ---- 7 files changed, 11 insertions(+), 12 deletions(-) diff --git a/docs/scripts/scripts.py b/docs/scripts/scripts.py index a5f2998..d6fd392 100644 --- a/docs/scripts/scripts.py +++ b/docs/scripts/scripts.py @@ -68,7 +68,7 @@ def generate_module_docs(): config_yaml = {} for key, value in manifest['configs'].items(): type = value.get('type', 'string') - if type == 'auto_archiver.utils.json_loader': + if type == 'json_loader': value['type'] = 'json' elif type == 'str': type = "string" diff --git a/src/auto_archiver/core/config.py b/src/auto_archiver/core/config.py index 2e5d273..425f96c 100644 --- a/src/auto_archiver/core/config.py +++ b/src/auto_archiver/core/config.py @@ -208,7 +208,7 @@ def read_yaml(yaml_filename: str) -> CommentedMap: pass if not config: - config = EMPTY_CONFIG + config = deepcopy(EMPTY_CONFIG) return config diff --git a/src/auto_archiver/core/validators.py b/src/auto_archiver/core/validators.py index b868ddf..0d3f01f 100644 --- a/src/auto_archiver/core/validators.py +++ b/src/auto_archiver/core/validators.py @@ -1,6 +1,7 @@ # used as validators for config values. Should raise an exception if the value is invalid. from pathlib import Path import argparse +import json def example_validator(value): if "example" not in value: @@ -16,4 +17,7 @@ def positive_number(value): def valid_file(value): if not Path(value).is_file(): raise argparse.ArgumentTypeError(f"File '{value}' does not exist.") - return value \ No newline at end of file + return value + +def json_loader(cli_val): + return json.loads(cli_val) \ No newline at end of file diff --git a/src/auto_archiver/modules/gsheet_feeder/__manifest__.py b/src/auto_archiver/modules/gsheet_feeder/__manifest__.py index 77026ea..d8b112d 100644 --- a/src/auto_archiver/modules/gsheet_feeder/__manifest__.py +++ b/src/auto_archiver/modules/gsheet_feeder/__manifest__.py @@ -35,7 +35,7 @@ "replaywebpage": "replaywebpage", }, "help": "names of columns in the google sheet (stringified JSON object)", - "type": "auto_archiver.utils.json_loader", + "type": "json_loader", }, "allow_worksheets": { "default": set(), diff --git a/src/auto_archiver/modules/gsheet_feeder/gsheet_feeder.py b/src/auto_archiver/modules/gsheet_feeder/gsheet_feeder.py index 8612d02..7cf4263 100644 --- a/src/auto_archiver/modules/gsheet_feeder/gsheet_feeder.py +++ b/src/auto_archiver/modules/gsheet_feeder/gsheet_feeder.py @@ -24,9 +24,8 @@ class GsheetsFeeder(Feeder): def setup(self) -> None: self.gsheets_client = gspread.service_account(filename=self.service_account) # TODO mv to validators - assert self.sheet or self.sheet_id, ( - "You need to define either a 'sheet' name or a 'sheet_id' in your manifest." - ) + if not (self.sheet or self.sheet_id): + raise ValueError("You need to define either a 'sheet' name or a 'sheet_id' in your manifest.") def open_sheet(self): if self.sheet: diff --git a/src/auto_archiver/modules/telethon_extractor/__manifest__.py b/src/auto_archiver/modules/telethon_extractor/__manifest__.py index e16d9db..458428b 100644 --- a/src/auto_archiver/modules/telethon_extractor/__manifest__.py +++ b/src/auto_archiver/modules/telethon_extractor/__manifest__.py @@ -18,7 +18,7 @@ "channel_invites": { "default": {}, "help": "(JSON string) private channel invite links (format: t.me/joinchat/HASH OR t.me/+HASH) and (optional but important to avoid hanging for minutes on startup) channel id (format: CHANNEL_ID taken from a post url like https://t.me/c/CHANNEL_ID/1), the telegram account will join any new channels on setup", - "type": "auto_archiver.utils.json_loader", + "type": "json_loader", } }, "description": """ diff --git a/src/auto_archiver/utils/misc.py b/src/auto_archiver/utils/misc.py index 108deae..e46a93d 100644 --- a/src/auto_archiver/utils/misc.py +++ b/src/auto_archiver/utils/misc.py @@ -59,10 +59,6 @@ def random_str(length: int = 32) -> str: return str(uuid.uuid4()).replace("-", "")[:length] -def json_loader(cli_val): - return json.loads(cli_val) - - def calculate_file_hash(filename: str, hash_algo = hashlib.sha256, chunksize: int = 16000000) -> str: hash = hash_algo() with open(filename, "rb") as f: