Skip to content

Commit 6a62410

Browse files
committed
Abstracted process.json()
1 parent 7e0bce0 commit 6a62410

3 files changed

Lines changed: 33 additions & 32 deletions

File tree

remove-json-keys/__main__.py

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,23 @@
1-
import os, re
2-
from lib import data, init, log
1+
from lib import data, init, log, process
32

43
cli = init.cli()
54

65
print('')
76

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)
1210
key = input("Enter key to remove (or ENTER if done): ")
1311
if not key : break
14-
remove_keys.append(key)
12+
cli.remove_keys.append(key)
1513

16-
# Determine closest locales dir
1714
log.trunc(f'\nSearching for {cli.json_dir}...')
1815
cli.json_dir = init.json_dir(cli.json_dir)
1916
if cli.json_dir : log.trunc(f'JSON directory found!\n\n>> {cli.json_dir}\n')
2017
else : log.trunc(f'Unable to locate a {cli.json_dir} directory.') ; exit()
2118

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)
2720

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
4621
summary = {
4722
'removed': [f'{key} ({file_path})' for key, file_path in keys_removed],
4823
'skipped': [f'{key} ({file_path})' for key, file_path in keys_skipped],

remove-json-keys/lib/init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def cli():
55

66
cli = sns(
77
name='remove-json-keys',
8-
version='2026.2.10.31',
8+
version='2026.2.10.33',
99
author=sns(name='Adam Lui', email='adam@kudoa.com', url='https://github.com/adamlui'),
1010
description='Remove key/value pairs from json_dir/**.json',
1111
urls=sns(

remove-json-keys/lib/process.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os, re
2+
3+
def json(cli):
4+
keys_removed, keys_skipped, processed_cnt = [], [], 0
5+
for root, _, files in os.walk(cli.json_dir):
6+
for filename in files:
7+
if filename.endswith('.json'):
8+
9+
# Open found JSON file
10+
file_path = os.path.join(root, filename)
11+
with open(file_path, 'r', encoding='utf-8') as f : data = f.read()
12+
13+
# Remove keys
14+
modified = False
15+
for key in cli.remove_keys:
16+
re_key = fr'"{re.escape(key)}".*?[,\n]+.*?(?="|$)'
17+
data, count = re.subn(re_key, '', data)
18+
if count > 0:
19+
keys_removed.append((key, os.path.relpath(file_path, cli.json_dir)))
20+
modified = True
21+
else : keys_skipped.append((key, os.path.relpath(file_path, cli.json_dir)))
22+
if modified:
23+
with open(file_path, 'w', encoding='utf-8') as f : f.write(data)
24+
processed_cnt += 1
25+
26+
return keys_removed, keys_skipped, processed_cnt

0 commit comments

Comments
 (0)