Skip to content

Commit f07f9e1

Browse files
Merge pull request #199 from enthec/scripts
structure auto fix script
2 parents 55c1eb1 + e22e25d commit f07f9e1

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

scripts/fix.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import json
2+
import pathlib
3+
from typing import Any, Callable
4+
5+
6+
class StructureFix:
7+
def __init__(self):
8+
self._src_path: pathlib.Path = pathlib.Path("src")
9+
self._transform: dict[str, Callable[[str | int], list]] = {
10+
"html": self._fix_to_list,
11+
"text": self._fix_to_list,
12+
"css": self._fix_to_list,
13+
"excludes": self._fix_to_list,
14+
"implies": self._fix_to_list,
15+
"requires": self._fix_to_list,
16+
"requiresCategory": self._fix_to_list,
17+
"scriptSrc": self._fix_to_list,
18+
"scripts": self._fix_to_list,
19+
"url": self._fix_to_list,
20+
"xhr": self._fix_to_list,
21+
"robots": self._fix_to_list,
22+
"dom": self._fix_to_list
23+
}
24+
25+
@staticmethod
26+
def _fix_to_list(current_detector) -> list:
27+
if isinstance(current_detector, str) or isinstance(current_detector, int):
28+
return [current_detector]
29+
return current_detector
30+
31+
@staticmethod
32+
def _do_nothing(current_detector):
33+
return current_detector
34+
35+
def fix(self):
36+
for file in self._src_path.joinpath("technologies").iterdir():
37+
if not file.name.endswith(".json"):
38+
continue
39+
with file.open("r") as f:
40+
techs: dict[str, dict[str, Any]] = json.loads(f.read())
41+
for tech_name, tech_detectors in techs.copy().items():
42+
for detector_name, detector in tech_detectors.copy().items():
43+
tech_detectors[detector_name] = self._transform.get(detector_name, self._do_nothing)(detector)
44+
techs[tech_name.strip()] = tech_detectors
45+
with file.open("w") as f:
46+
f.write(json.dumps(techs, indent=2, sort_keys=True, ensure_ascii=False))
47+
48+
49+
if __name__ == '__main__':
50+
StructureFix().fix()

0 commit comments

Comments
 (0)