From e22e25dc64bf1aa8911a751971ad56c3e5d4a899 Mon Sep 17 00:00:00 2001 From: Enthec Date: Sat, 9 Nov 2024 11:12:53 +0100 Subject: [PATCH] structure auto fix script --- scripts/fix.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 scripts/fix.py diff --git a/scripts/fix.py b/scripts/fix.py new file mode 100644 index 00000000..cb327a32 --- /dev/null +++ b/scripts/fix.py @@ -0,0 +1,50 @@ +import json +import pathlib +from typing import Any, Callable + + +class StructureFix: + def __init__(self): + self._src_path: pathlib.Path = pathlib.Path("src") + self._transform: dict[str, Callable[[str | int], list]] = { + "html": self._fix_to_list, + "text": self._fix_to_list, + "css": self._fix_to_list, + "excludes": self._fix_to_list, + "implies": self._fix_to_list, + "requires": self._fix_to_list, + "requiresCategory": self._fix_to_list, + "scriptSrc": self._fix_to_list, + "scripts": self._fix_to_list, + "url": self._fix_to_list, + "xhr": self._fix_to_list, + "robots": self._fix_to_list, + "dom": self._fix_to_list + } + + @staticmethod + def _fix_to_list(current_detector) -> list: + if isinstance(current_detector, str) or isinstance(current_detector, int): + return [current_detector] + return current_detector + + @staticmethod + def _do_nothing(current_detector): + return current_detector + + def fix(self): + for file in self._src_path.joinpath("technologies").iterdir(): + if not file.name.endswith(".json"): + continue + with file.open("r") as f: + techs: dict[str, dict[str, Any]] = json.loads(f.read()) + for tech_name, tech_detectors in techs.copy().items(): + for detector_name, detector in tech_detectors.copy().items(): + tech_detectors[detector_name] = self._transform.get(detector_name, self._do_nothing)(detector) + techs[tech_name.strip()] = tech_detectors + with file.open("w") as f: + f.write(json.dumps(techs, indent=2, sort_keys=True, ensure_ascii=False)) + + +if __name__ == '__main__': + StructureFix().fix()