Skip to content

Commit 83caee8

Browse files
committed
Created cli.json_path to not mutate cli.config.json_dir
1 parent 73d3252 commit 83caee8

3 files changed

Lines changed: 10 additions & 11 deletions

File tree

remove-json-keys/src/remove_json_keys/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ def main():
1010
if not cli.config.no_wizard : wizard.run(cli)
1111

1212
log.info(f'{cli.msgs.log_SEARCHING_FOR} {cli.config.json_dir}...')
13-
init.json_dir(cli)
14-
if Path(cli.config.json_dir).exists():
13+
init.json_path(cli)
14+
if Path(cli.json_path).exists():
1515
log.success(f'{cli.msgs.log_DIR_FOUND}!')
16-
print(f'\n>> {cli.config.json_dir}')
16+
print(f'\n>> {cli.json_path}')
1717
else:
1818
log.warn(f'{cli.msgs.warn_DIR_NOT_FOUND}.')
1919
sys.exit(1)
2020

21-
keys_removed, keys_skipped, files_processed_cnt = data.json.remove_keys(cli.config.json_dir, cli.config.keys)
21+
keys_removed, keys_skipped, files_processed_cnt = data.json.remove_keys(cli.json_path, cli.config.keys)
2222

2323
log.final_summary(cli.msgs, {
2424
'removed': [f'{key} ({file_path})' for key, file_path in keys_removed],

remove-json-keys/src/remove_json_keys/lib/data/json.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ def read(file_path, encoding='utf-8'):
99
with open(file_path, 'r', encoding=encoding) as file:
1010
return json5.load(file)
1111

12-
def remove_keys(json_dir, keys):
12+
def remove_keys(json_path, keys):
1313
keys_removed, keys_skipped, files_processed_cnt = [], [], 0
14-
json_dir = Path(json_dir)
15-
for file_path in json_dir.rglob('*.json'):
14+
for file_path in json_path.rglob('*.json'):
1615
json_data = file.read(file_path)
1716
modified = False
1817
for key in keys: # remove matched ones
1918
re_key = fr'"{re.escape(key)}"\s*:\s*(?:\{{[^}}]*\}}|"[^"]*"|\d+|true|false|null)\s*,?\s*'
2019
json_data, cnt = re.subn(re_key, '', json_data)
21-
rel_path = str(file_path.relative_to(json_dir))
20+
rel_path = str(file_path.relative_to(json_path))
2221
if cnt > 0:
2322
keys_removed.append((key, rel_path))
2423
modified = True

remove-json-keys/src/remove_json_keys/lib/init.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def config_filepath(cli): # for settings.load()
5858
cli.config_filepath = possible_config_file
5959
return
6060

61-
def json_dir(cli):
61+
def json_path(cli):
6262
for path in Path.cwd().rglob(cli.config.json_dir):
6363
if path.is_dir():
64-
cli.config.json_dir = str(path)
64+
cli.json_path = Path(path)
6565
return
66-
cli.config.json_dir = None
66+
cli.json_path = None

0 commit comments

Comments
 (0)