Skip to content

Commit 8315ddf

Browse files
committed
Moved vars into cli, renamed config file
1 parent 53460ee commit 8315ddf

4 files changed

Lines changed: 39 additions & 40 deletions

File tree

remove-json-keys/__main__.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
Name: remove-json-keys
3-
Version: 2026.2.10.17
3+
Version: 2026.2.10.18
44
Author: Adam Lui
55
Description: Remove key/value pairs from json_dir/**.json
66
Homepage: https://github.com/adamlui/python-utils
@@ -18,8 +18,8 @@
1818
parser = argparse.ArgumentParser(description='Remove key/value pairs from JSON files')
1919
parser.add_argument('--remove-keys', type=str, help='Keys to remove')
2020
parser.add_argument('--json-dir', type=str, help='Name of folder containing JSON files')
21-
args = parser.parse_args()
22-
json_dir = args.json_dir or '_locales'
21+
cli.args = parser.parse_args()
22+
cli.json_dir = cli.args.json_dir or '_locales'
2323

2424
# UI initializations
2525
try:
@@ -35,36 +35,36 @@ def print_trunc(msg, end='\n'):
3535

3636
# Prompt user for keys to remove
3737
def parse_csv_val(val) : return [item.strip() for item in val.split(',') if item.strip()]
38-
remove_keys = parse_csv_val(args.remove_keys or '')
38+
remove_keys = parse_csv_val(cli.args.remove_keys or '')
3939
while True:
4040
if remove_keys : print('Key(s) to remove:', remove_keys)
4141
key = input("Enter key to remove (or ENTER if done): ")
4242
if not key : break
4343
remove_keys.append(key)
4444

45-
# Determine closest JSON dir
46-
print_trunc(f'Searching for { json_dir }...')
45+
# Determine closest locales dir
46+
print_trunc(f'\nSearching for {cli.json_dir}...')
4747
script_dir = os.path.abspath(os.path.dirname(__file__))
4848
for root, dirs, files in os.walk(script_dir): # search script dir recursively
49-
if json_dir in dirs:
50-
json_dir = os.path.join(root, json_dir) ; break
49+
if cli.json_dir in dirs:
50+
cli.json_dir = os.path.join(root, cli.json_dir) ; break
5151
else: # search script parent dirs recursively
5252
parent_dir = os.path.dirname(script_dir)
5353
while parent_dir and parent_dir != script_dir:
5454
for root, dirs, files in os.walk(parent_dir):
55-
if json_dir in dirs:
56-
json_dir = os.path.join(root, json_dir) ; break
57-
if json_dir : break
55+
if cli.json_dir in dirs:
56+
cli.json_dir = os.path.join(root, cli.json_dir) ; break
57+
if cli.json_dir : break
5858
parent_dir = os.path.dirname(parent_dir)
59-
else : json_dir = None
59+
else : cli.json_dir = None
6060

6161
# Print result
62-
if json_dir : print_trunc(f'JSON directory found!\n\n>> { json_dir }\n')
63-
else : print_trunc(f'Unable to locate a { json_dir } directory.') ; exit()
62+
if cli.json_dir : print_trunc(f'JSON directory found!\n\n>> {cli.json_dir}\n')
63+
else : print_trunc(f'Unable to locate a {cli.json_dir} directory.') ; exit()
6464

6565
# Process JSON files and remove specified keys
6666
keys_removed, keys_skipped, processed_count = [], [], 0
67-
for root, _, files in os.walk(json_dir):
67+
for root, _, files in os.walk(cli.json_dir):
6868
for filename in files:
6969
if filename.endswith('.json'):
7070

@@ -78,9 +78,9 @@ def parse_csv_val(val) : return [item.strip() for item in val.split(',') if item
7878
re_key = fr'"{re.escape(key)}".*?[,\n]+.*?(?="|$)'
7979
data, count = re.subn(re_key, '', data)
8080
if count > 0:
81-
keys_removed.append((key, os.path.relpath(file_path, json_dir)))
81+
keys_removed.append((key, os.path.relpath(file_path, cli.json_dir)))
8282
modified = True
83-
else : keys_skipped.append((key, os.path.relpath(file_path, json_dir)))
83+
else : keys_skipped.append((key, os.path.relpath(file_path, cli.json_dir)))
8484
if modified:
8585
with open(file_path, 'w', encoding='utf-8') as f : f.write(data)
8686
processed_count += 1

translate-messages/__main__.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
Name: translate-en-messages
3-
Version: 2026.2.10.29
3+
Version: 2026.2.10.30
44
Author: Adam Lui
55
Description: Translate en/messages.json to other locales
66
Homepage: https://github.com/adamlui/python-utils
@@ -25,11 +25,11 @@
2525
parser.add_argument('--locales-dir', type=str, help='Name of folder containing locales')
2626
parser.add_argument('--provider', type=str, help='Name of provider to use for translation')
2727
parser.add_argument('--init', action='store_true', help='Create .config.json file to store defaults')
28-
args = parser.parse_args()
29-
locales_dir = args.locales_dir or cli.config_data.get('locales_dir', '') or '_locales'
30-
provider = args.provider or cli.config_data.get('provider', '')
28+
cli.args = parser.parse_args()
29+
cli.locales_dir = cli.args.locales_dir or cli.config_data.get('locales_dir', '') or '_locales'
30+
cli.provider = cli.args.provider or cli.config_data.get('provider', '')
3131

32-
if args.init: # create config file
32+
if cli.args.init: # create config file
3333
if os.path.exists(cli.config_path):
3434
print(f'Config already exists at {cli.config_path}')
3535
else:
@@ -45,8 +45,8 @@
4545

4646
# Init target_locales
4747
def parse_csv_val(val) : return [item.strip() for item in val.split(',') if item.strip()]
48-
include_arg = args.include_langs or cli.config_data.get('include_langs', '')
49-
exclude_arg = args.exclude_langs or cli.config_data.get('exclude_langs', '')
48+
include_arg = cli.args.include_langs or cli.config_data.get('include_langs', '')
49+
exclude_arg = cli.args.exclude_langs or cli.config_data.get('exclude_langs', '')
5050
target_locales = parse_csv_val(include_arg) or cli.default_target_locales
5151
exclude_langs = set(parse_csv_val(exclude_arg))
5252
target_locales = [lang for lang in target_locales if lang not in exclude_langs]
@@ -65,42 +65,42 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin
6565
print('')
6666

6767
# Prompt user for keys to ignore
68-
ignore_keys = parse_csv_val(args.ignore_keys or cli.config_data.get('ignore_keys', ''))
68+
ignore_keys = parse_csv_val(cli.args.ignore_keys or cli.config_data.get('ignore_keys', ''))
6969
while True:
7070
if ignore_keys : print('Ignored key(s):', ignore_keys)
7171
key = input('Enter key to ignore (or ENTER if done): ')
7272
if not key : break
7373
ignore_keys.append(key)
7474

7575
# Determine closest locales dir
76-
print_trunc(f'\nSearching for { locales_dir }...')
76+
print_trunc(f'\nSearching for {cli.locales_dir}...')
7777
script_dir = os.path.abspath(os.path.dirname(__file__))
7878
for root, dirs, files in os.walk(script_dir): # search script dir recursively
79-
if locales_dir in dirs:
80-
locales_dir = os.path.join(root, locales_dir) ; break
79+
if cli.locales_dir in dirs:
80+
cli.locales_dir = os.path.join(root, cli.locales_dir) ; break
8181
else: # search script parent dirs recursively
8282
parent_dir = os.path.dirname(script_dir)
8383
while parent_dir and parent_dir != script_dir:
8484
for root, dirs, files in os.walk(parent_dir):
85-
if locales_dir in dirs:
86-
locales_dir = os.path.join(root, locales_dir) ; break
87-
if locales_dir : break
85+
if cli.locales_dir in dirs:
86+
cli.locales_dir = os.path.join(root, cli.locales_dir) ; break
87+
if cli.locales_dir : break
8888
parent_dir = os.path.dirname(parent_dir)
89-
else : locales_dir = None
89+
else : cli.locales_dir = None
9090

9191
# Print result
92-
if locales_dir : print_trunc(f'_locales directory found!\n\n>> { locales_dir }\n')
93-
else : print_trunc(f'Unable to locate a { locales_dir } directory.') ; exit()
92+
if cli.locales_dir : print_trunc(f'_locales directory found!\n\n>> {cli.locales_dir}\n')
93+
else : print_trunc(f'Unable to locate a {cli.locales_dir} directory.') ; exit()
9494

9595
# Load en/messages.json
9696
msgs_filename = 'messages.json'
97-
en_msgs_path = os.path.join(locales_dir, 'en', msgs_filename)
97+
en_msgs_path = os.path.join(cli.locales_dir, 'en', msgs_filename)
9898
with open(en_msgs_path, 'r', encoding='utf-8') as en_file:
9999
en_messages = json.load(en_file)
100100

101101
# Combine [target_locales] w/ languages discovered in _locales
102102
output_langs = list(set(target_locales)) # remove duplicates
103-
for root, dirs, files in os.walk(locales_dir):
103+
for root, dirs, files in os.walk(cli.locales_dir):
104104
for folder in dirs:
105105
folder_path = os.path.join(root, folder)
106106
msgs_path = os.path.join(folder_path, msgs_filename)
@@ -123,7 +123,7 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin
123123
langs_skipped.append(lang_code) ; langs_not_translated.append(lang_code) ; continue
124124

125125
# Initialize target locale folder
126-
folder_path = os.path.join(locales_dir, folder)
126+
folder_path = os.path.join(cli.locales_dir, folder)
127127
if not os.path.exists(folder_path): # if missing, create folder
128128
os.makedirs(folder_path) ; langs_added.append(lang_code) ; lang_added = True
129129

@@ -146,7 +146,7 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin
146146
if key not in messages:
147147
original_msg = translated_msg = en_messages[key]['message']
148148
try:
149-
translator = Translator(provider=provider, to_lang=lang_code)
149+
translator = Translator(provider=cli.provider, to_lang=lang_code)
150150
translated_msg = translator.translate(original_msg).replace('"', "'").replace(''', "'")
151151
if any(flag in translated_msg for flag in fail_flags):
152152
translated_msg = original_msg

translate-messages/lib/init.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def cli():
1717
)
1818

1919
def configFile(cli, caller_file):
20-
cli.script_name = os.path.splitext(os.path.basename(caller_file))[0]
21-
cli.config_filename = f'{cli.script_name}.config.json'
20+
cli.config_filename = f'{cli.name}.config.json'
2221
cli.config_path = os.path.join(os.path.dirname(caller_file), cli.config_filename)
2322
cli.config_data = {}
2423
if os.path.exists(cli.config_path):
File renamed without changes.

0 commit comments

Comments
 (0)