|
1 | | -import json, re |
| 1 | +import json |
2 | 2 | from pathlib import Path |
3 | | -from typing import Any, Dict, List, Union |
| 3 | +from typing import Any, Dict, Union |
4 | 4 |
|
5 | 5 | import json5 |
6 | 6 |
|
@@ -36,24 +36,6 @@ def read(input: Union[Path, str], encoding: str = 'utf-8') -> Any: |
36 | 36 | return json5.load(file) |
37 | 37 | else : return json5.loads(input_str) |
38 | 38 |
|
39 | | -def remove_keys(json_path: Path, keys: List[str]): |
40 | | - keys_removed, keys_skipped, files_processed_cnt = [], [], 0 |
41 | | - for file_path in json_path.rglob('*.json'): |
42 | | - json_data = file.read(file_path) |
43 | | - modified = False |
44 | | - for key in keys: # remove matched ones |
45 | | - re_key = fr'"{re.escape(key)}"\s*:\s*(?:\{{[^}}]*\}}|"[^"]*"|\d+|true|false|null)\s*,?\s*' |
46 | | - json_data, cnt = re.subn(re_key, '', json_data) |
47 | | - rel_path = str(file_path.relative_to(json_path)) |
48 | | - if cnt > 0: |
49 | | - keys_removed.append((key, rel_path)) |
50 | | - modified = True |
51 | | - else: |
52 | | - keys_skipped.append((key, rel_path)) |
53 | | - if modified : file.write(file_path, json_data) |
54 | | - files_processed_cnt += 1 |
55 | | - return keys_removed, keys_skipped, files_processed_cnt |
56 | | - |
57 | 39 | def write(file_path: Union[Path, str], data: Any, encoding: str = 'utf-8', ensure_ascii: bool = False, |
58 | 40 | style: str = 'pretty', atomic: bool =True): |
59 | 41 | Path(file_path).parent.mkdir(parents=True, exist_ok=True) |
|
0 commit comments