Skip to content

Commit cb42abb

Browse files
committed
Moved config vars into cli ↞ [auto-sync from https://github.com/adamlui/python-utils/tree/main/translate-messages]
1 parent a8720e6 commit cb42abb

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

chromium/utils/translate-en-messages.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
Name: translate-en-messages.py
3-
Version: 2026.2.10.21
3+
Version: 2026.2.10.22
44
Author: Adam Lui
55
Description: Translate en/messages.json to other locales
66
Homepage: https://github.com/adamlui/python-utils
@@ -31,13 +31,13 @@
3131
]
3232

3333
# Init/load config file
34-
script_name = os.path.splitext(os.path.basename(__file__))[0]
35-
config_filename = f'{script_name}.config.json'
36-
config_path = os.path.join(os.path.dirname(__file__), config_filename)
37-
config_data = {}
38-
if os.path.exists(config_path):
39-
with open(config_path, 'r', encoding='utf-8') as file_config:
40-
config_data.update(json.load(file_config))
34+
cli.script_name = os.path.splitext(os.path.basename(__file__))[0]
35+
cli.config_filename = f'{cli.script_name}.config.json'
36+
cli.config_path = os.path.join(os.path.dirname(__file__), cli.config_filename)
37+
cli.config_data = {}
38+
if os.path.exists(cli.config_path):
39+
with open(cli.config_path, 'r', encoding='utf-8') as file_config:
40+
cli.config_data.update(json.load(file_config))
4141

4242
# Parse CLI args
4343
parser = argparse.ArgumentParser(description='Translate en/messages.json to other locales')
@@ -47,26 +47,26 @@
4747
parser.add_argument('--locales-dir', type=str, help='Name of folder containing locales')
4848
parser.add_argument('--init', action='store_true', help='Create .config.json file to store defaults')
4949
args = parser.parse_args()
50-
locales_dir = args.locales_dir or config_data.get('locales_dir', '') or '_locales'
50+
locales_dir = args.locales_dir or cli.config_data.get('locales_dir', '') or '_locales'
5151

5252
if args.init: # create config file
53-
if os.path.exists(config_path):
54-
print(f'Config already exists at {config_path}')
53+
if os.path.exists(cli.config_path):
54+
print(f'Config already exists at {cli.config_path}')
5555
else:
5656
try: # try to fetch template from jsDelivr
57-
jsd_url = f'{cli.urls.jsdelivr}/{cli.name}/{config_filename}'
57+
jsd_url = f'{cli.urls.jsdelivr}/{cli.name}/{cli.config_filename}'
5858
with urlopen(jsd_url) as resp:
59-
if resp.status == 200 : config_data = json.loads(resp.read().decode('utf-8'))
59+
if resp.status == 200 : cli.config_data = json.loads(resp.read().decode('utf-8'))
6060
except Exception : pass
61-
with open(config_path, 'w', encoding='utf-8') as configFile:
62-
json.dump(config_data, configFile, indent=2)
63-
print(f'Default config created at {config_path}')
61+
with open(cli.config_path, 'w', encoding='utf-8') as configFile:
62+
json.dump(cli.config_data, configFile, indent=2)
63+
print(f'Default config created at {cli.config_path}')
6464
exit()
6565

6666
# Init target_locales
6767
def parse_csv_langs(str) : return [lang.strip() for lang in str.split(',') if lang.strip()]
68-
include_arg = args.include_langs or config_data.get('include_langs', '')
69-
exclude_arg = args.exclude_langs or config_data.get('exclude_langs', '')
68+
include_arg = args.include_langs or cli.config_data.get('include_langs', '')
69+
exclude_arg = args.exclude_langs or cli.config_data.get('exclude_langs', '')
7070
target_locales = parse_csv_langs(include_arg) or default_target_locales
7171
exclude_langs = set(parse_csv_langs(exclude_arg))
7272
target_locales = [lang for lang in target_locales if lang not in exclude_langs]
@@ -85,7 +85,7 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin
8585
print('')
8686

8787
# Prompt user for keys to ignore
88-
ignore_keys = parse_csv_langs(args.ignore_keys or config_data.get('ignore_keys', ''))
88+
ignore_keys = parse_csv_langs(args.ignore_keys or cli.config_data.get('ignore_keys', ''))
8989
while True:
9090
if ignore_keys : print('Ignored key(s):', ignore_keys)
9191
key = input('Enter key to ignore (or ENTER if done): ')

firefox/utils/translate-en-messages.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
Name: translate-en-messages.py
3-
Version: 2026.2.10.21
3+
Version: 2026.2.10.22
44
Author: Adam Lui
55
Description: Translate en/messages.json to other locales
66
Homepage: https://github.com/adamlui/python-utils
@@ -31,13 +31,13 @@
3131
]
3232

3333
# Init/load config file
34-
script_name = os.path.splitext(os.path.basename(__file__))[0]
35-
config_filename = f'{script_name}.config.json'
36-
config_path = os.path.join(os.path.dirname(__file__), config_filename)
37-
config_data = {}
38-
if os.path.exists(config_path):
39-
with open(config_path, 'r', encoding='utf-8') as file_config:
40-
config_data.update(json.load(file_config))
34+
cli.script_name = os.path.splitext(os.path.basename(__file__))[0]
35+
cli.config_filename = f'{cli.script_name}.config.json'
36+
cli.config_path = os.path.join(os.path.dirname(__file__), cli.config_filename)
37+
cli.config_data = {}
38+
if os.path.exists(cli.config_path):
39+
with open(cli.config_path, 'r', encoding='utf-8') as file_config:
40+
cli.config_data.update(json.load(file_config))
4141

4242
# Parse CLI args
4343
parser = argparse.ArgumentParser(description='Translate en/messages.json to other locales')
@@ -47,26 +47,26 @@
4747
parser.add_argument('--locales-dir', type=str, help='Name of folder containing locales')
4848
parser.add_argument('--init', action='store_true', help='Create .config.json file to store defaults')
4949
args = parser.parse_args()
50-
locales_dir = args.locales_dir or config_data.get('locales_dir', '') or '_locales'
50+
locales_dir = args.locales_dir or cli.config_data.get('locales_dir', '') or '_locales'
5151

5252
if args.init: # create config file
53-
if os.path.exists(config_path):
54-
print(f'Config already exists at {config_path}')
53+
if os.path.exists(cli.config_path):
54+
print(f'Config already exists at {cli.config_path}')
5555
else:
5656
try: # try to fetch template from jsDelivr
57-
jsd_url = f'{cli.urls.jsdelivr}/{cli.name}/{config_filename}'
57+
jsd_url = f'{cli.urls.jsdelivr}/{cli.name}/{cli.config_filename}'
5858
with urlopen(jsd_url) as resp:
59-
if resp.status == 200 : config_data = json.loads(resp.read().decode('utf-8'))
59+
if resp.status == 200 : cli.config_data = json.loads(resp.read().decode('utf-8'))
6060
except Exception : pass
61-
with open(config_path, 'w', encoding='utf-8') as configFile:
62-
json.dump(config_data, configFile, indent=2)
63-
print(f'Default config created at {config_path}')
61+
with open(cli.config_path, 'w', encoding='utf-8') as configFile:
62+
json.dump(cli.config_data, configFile, indent=2)
63+
print(f'Default config created at {cli.config_path}')
6464
exit()
6565

6666
# Init target_locales
6767
def parse_csv_langs(str) : return [lang.strip() for lang in str.split(',') if lang.strip()]
68-
include_arg = args.include_langs or config_data.get('include_langs', '')
69-
exclude_arg = args.exclude_langs or config_data.get('exclude_langs', '')
68+
include_arg = args.include_langs or cli.config_data.get('include_langs', '')
69+
exclude_arg = args.exclude_langs or cli.config_data.get('exclude_langs', '')
7070
target_locales = parse_csv_langs(include_arg) or default_target_locales
7171
exclude_langs = set(parse_csv_langs(exclude_arg))
7272
target_locales = [lang for lang in target_locales if lang not in exclude_langs]
@@ -85,7 +85,7 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin
8585
print('')
8686

8787
# Prompt user for keys to ignore
88-
ignore_keys = parse_csv_langs(args.ignore_keys or config_data.get('ignore_keys', ''))
88+
ignore_keys = parse_csv_langs(args.ignore_keys or cli.config_data.get('ignore_keys', ''))
8989
while True:
9090
if ignore_keys : print('Ignored key(s):', ignore_keys)
9191
key = input('Enter key to ignore (or ENTER if done): ')

0 commit comments

Comments
 (0)