|
1 | | -import os, re |
2 | | -from lib import data, init, log |
| 1 | +from lib import data, init, log, process |
3 | 2 |
|
4 | 3 | cli = init.cli() |
5 | 4 |
|
6 | 5 | print('') |
7 | 6 |
|
8 | | -# Prompt user for keys to remove |
9 | | -remove_keys = data.parse_csv_val(cli.args.remove_keys or '') |
10 | | -while True: |
11 | | - if remove_keys : print('Key(s) to remove:', remove_keys) |
| 7 | +cli.remove_keys = data.parse_csv_val(cli.args.remove_keys or '') |
| 8 | +while True: # prompt user for keys to remove |
| 9 | + if cli.remove_keys : print('Key(s) to remove:', cli.remove_keys) |
12 | 10 | key = input("Enter key to remove (or ENTER if done): ") |
13 | 11 | if not key : break |
14 | | - remove_keys.append(key) |
| 12 | + cli.remove_keys.append(key) |
15 | 13 |
|
16 | | -# Determine closest locales dir |
17 | 14 | log.trunc(f'\nSearching for {cli.json_dir}...') |
18 | 15 | cli.json_dir = init.json_dir(cli.json_dir) |
19 | 16 | if cli.json_dir : log.trunc(f'JSON directory found!\n\n>> {cli.json_dir}\n') |
20 | 17 | else : log.trunc(f'Unable to locate a {cli.json_dir} directory.') ; exit() |
21 | 18 |
|
22 | | -# Process JSON files and remove specified keys |
23 | | -keys_removed, keys_skipped, processed_cnt = [], [], 0 |
24 | | -for root, _, files in os.walk(cli.json_dir): |
25 | | - for filename in files: |
26 | | - if filename.endswith('.json'): |
| 19 | +keys_removed, keys_skipped, processed_cnt = process.json(cli) |
27 | 20 |
|
28 | | - # Open found JSON file |
29 | | - file_path = os.path.join(root, filename) |
30 | | - with open(file_path, 'r', encoding='utf-8') as f : data = f.read() |
31 | | - |
32 | | - # Remove keys |
33 | | - modified = False |
34 | | - for key in remove_keys: |
35 | | - re_key = fr'"{re.escape(key)}".*?[,\n]+.*?(?="|$)' |
36 | | - data, count = re.subn(re_key, '', data) |
37 | | - if count > 0: |
38 | | - keys_removed.append((key, os.path.relpath(file_path, cli.json_dir))) |
39 | | - modified = True |
40 | | - else : keys_skipped.append((key, os.path.relpath(file_path, cli.json_dir))) |
41 | | - if modified: |
42 | | - with open(file_path, 'w', encoding='utf-8') as f : f.write(data) |
43 | | - processed_cnt += 1 |
44 | | - |
45 | | -# Print file summaries |
46 | 21 | summary = { |
47 | 22 | 'removed': [f'{key} ({file_path})' for key, file_path in keys_removed], |
48 | 23 | 'skipped': [f'{key} ({file_path})' for key, file_path in keys_skipped], |
|
0 commit comments