Skip to content

Commit 68d7778

Browse files
committed
Moved process.json() to data.removeJSONkeys()
1 parent 0d27eba commit 68d7778

4 files changed

Lines changed: 30 additions & 29 deletions

File tree

remove-json-keys/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lib import data, init, log, process
1+
from lib import data, init, log
22

33
cli = init.cli()
44

@@ -16,7 +16,7 @@
1616
if cli.json_dir : log.trunc(f'JSON directory found!\n\n>> {cli.json_dir}\n')
1717
else : log.trunc(f'Unable to locate a {cli.json_dir} directory.') ; exit()
1818

19-
keys_removed, keys_skipped, processed_cnt = process.json(cli)
19+
keys_removed, keys_skipped, processed_cnt = data.removeJSONkeys(cli)
2020

2121
summary = {
2222
'removed': [f'{key} ({file_path})' for key, file_path in keys_removed],

remove-json-keys/lib/data.py

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

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.33',
8+
version='2026.2.10.34',
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: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)