|
1 | 1 | import json, os, re |
| 2 | +from . import file |
2 | 3 |
|
3 | | -def read(path): |
4 | | - if not os.path.exists(path) : return {} |
5 | | - with open(path, 'r', encoding='utf-8') as file : return json.load(file) |
| 4 | +def read(json_path): |
| 5 | + if not os.path.exists(json_path) : return {} |
| 6 | + with open(json_path, 'r', encoding='utf-8') as file : return json.load(file) |
6 | 7 |
|
7 | 8 | def remove_keys(cli): |
8 | 9 | keys_removed, keys_skipped, files_processed_cnt = [], [], 0 |
9 | 10 | for root, _, files in os.walk(cli.config.json_dir): |
10 | 11 | for filename in files: |
11 | 12 | if filename.endswith('.json'): |
12 | | - |
13 | | - # Open found JSON file |
14 | 13 | file_path = os.path.join(root, filename) |
15 | | - with open(file_path, 'r', encoding='utf-8') as f : data = f.read() |
| 14 | + json_data = file.read(file_path) |
16 | 15 |
|
17 | 16 | # Remove keys |
18 | 17 | modified = False |
19 | 18 | for key in cli.config.keys: |
20 | 19 | re_key = fr'"{re.escape(key)}"\s*:\s*(?:\{{[^}}]*\}}|"[^"]*"|\d+|true|false|null)\s*,?\s*' |
21 | | - data, cnt = re.subn(re_key, '', data) |
| 20 | + json_data, cnt = re.subn(re_key, '', json_data) |
22 | 21 | if cnt > 0: |
23 | 22 | keys_removed.append((key, os.path.relpath(file_path, cli.config.json_dir))) |
24 | 23 | modified = True |
25 | 24 | else: |
26 | 25 | keys_skipped.append((key, os.path.relpath(file_path, cli.config.json_dir))) |
27 | 26 |
|
28 | 27 | # Save modified JSON |
29 | | - if modified: |
30 | | - with open(file_path, 'w', encoding='utf-8') as f : f.write(data) |
| 28 | + if modified : file.write(file_path, json_data) |
31 | 29 |
|
32 | 30 | files_processed_cnt += 1 |
33 | 31 |
|
|
0 commit comments