Skip to content

Commit 2ec8451

Browse files
committed
Added/used settings.js
1 parent 3064172 commit 2ec8451

6 files changed

Lines changed: 148 additions & 87 deletions

File tree

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,9 @@
1-
import argparse, os
2-
from types import SimpleNamespace as sns
3-
from . import data
1+
import os
2+
from . import data, settings
43

54
def cli():
6-
75
cli = data.sns.from_dict(data.json.read(os.path.join(os.path.dirname(__file__), '../package_data.json')))
8-
9-
# Parse CLI args
10-
argp = argparse.ArgumentParser(
11-
description="Simply remove JSON keys via CLI command",
12-
add_help=False # disable default --help arg to re-create last
13-
)
14-
argp.add_argument('-d', '--json-dir', '--json-folder',
15-
type=str, help='Name of the folder containing JSON files (default: "_locales")')
16-
argp.add_argument('-k', '--keys', '--key', '--remove-keys', '--remove-key', '--delete-keys', '--delete-key',
17-
type=str, help='Keys to remove (e.g. "appName,author")')
18-
argp.add_argument('-W', '--no-wizard', '--skip-wizard',
19-
action='store_true', default=None, help='Skip interactive prompts during start-up')
20-
argp.add_argument('-h', '--help', action='help', help="Show help screen")
21-
cli.config=sns()
22-
cli.config.__dict__.update({ key:val for key,val in vars(argp.parse_args()).items() if val is not None })
23-
24-
# Init cli.config vals
25-
cli.config.keys = data.csv.parse(getattr(cli.config, 'keys', None))
26-
cli.config.json_dir = getattr(cli.config, 'json_dir', '_locales')
27-
cli.config.no_wizard = getattr(cli.config, 'no_wizard', False)
28-
6+
settings.load(cli)
297
return cli
308

319
def json_dir(target_dir):
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import argparse
2+
from types import SimpleNamespace as sns
3+
from . import data
4+
5+
controls = sns(
6+
json_dir=sns(
7+
args=['-d', '--json-dir', '--json-folder'],
8+
type=str, default_val='_locales', help='Name of the folder containing JSON files (default: "_locales")'
9+
),
10+
keys=sns(
11+
args=['-k', '--keys', '--key', '--remove-keys', '--remove-key', '--delete-keys', '--delete-key'],
12+
type=str, parser='csv', help='Keys to remove (e.g. "appName,author")'
13+
),
14+
no_wizard=sns(
15+
args=['-W', '--no-wizard', '--skip-wizard'],
16+
action='store_true', default=None, help='Skip interactive prompts during start-up'
17+
),
18+
help=sns(
19+
args=['-h', '--help'],
20+
action='help', help='Show help screen'
21+
)
22+
)
23+
24+
def load(cli):
25+
26+
# Parse CLI args
27+
argp = argparse.ArgumentParser(
28+
description="Simply remove JSON keys via CLI command",
29+
add_help=False # disable default --help arg to re-create last
30+
)
31+
cli.config=sns()
32+
for attr_name in vars(controls):
33+
kwargs = getattr(controls, attr_name).__dict__.copy()
34+
args = kwargs.pop('args') # separate positional flags
35+
for forbidden in ('default_val', 'parser'): # remove custom attrs
36+
kwargs.pop(forbidden, None)
37+
argp.add_argument(*args, **kwargs)
38+
cli.config.__dict__.update({ key:val for key,val in vars(argp.parse_args()).items() if val is not None})
39+
40+
# Init cli.config vals
41+
for name, ctrl in vars(controls).items():
42+
val = getattr(cli.config, name, None)
43+
if getattr(ctrl, 'parser', None) == 'csv':
44+
val = data.csv.parse(val)
45+
if getattr(ctrl, 'action', None) == 'store_true':
46+
val = val if val is not None else False
47+
if val is None and hasattr(ctrl, 'default_val'):
48+
val = ctrl.default_val
49+
setattr(cli.config, name, val)

translate-messages/src/translate_messages/__main__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ def main():
2323

2424
cli.config.msgs_filename = 'messages.json'
2525
cli.config.en_msgs = data.json.read(os.path.join(cli.config.locales_dir, 'en', cli.config.msgs_filename))
26-
cli.config.output_langs = list(set(cli.config.target_locales)) # remove dupes
26+
cli.config.target_langs = list(set(cli.config.target_langs)) # remove dupes
2727

28-
if not cli.config.target_langs: # merge discovered locales w/ output_langs
28+
if not cli.config.target_langs: # merge discovered locales w/ target_langs
2929
for root, dirs, _ in os.walk(cli.config.locales_dir):
3030
for lang_folder in dirs:
3131
msgs_path = os.path.join(root, lang_folder, cli.config.msgs_filename)
3232
discovered_lang = lang_folder.replace('_', '-')
33-
if os.path.exists(msgs_path) and discovered_lang not in cli.config.output_langs:
34-
cli.config.output_langs.append(discovered_lang)
35-
cli.config.output_langs.sort()
33+
if os.path.exists(msgs_path) and discovered_lang not in cli.config.target_langs:
34+
cli.config.target_langs.append(discovered_lang)
35+
cli.config.target_langs.sort()
3636

3737
langs_translated, langs_skipped, langs_added, langs_not_translated = language.write_translations(cli)
3838

translate-messages/src/translate_messages/lib/init.py

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,9 @@
1-
import argparse, os, requests
2-
from types import SimpleNamespace as sns
3-
from . import data, log
1+
import os, requests
2+
from . import data, log, settings
43

54
def cli(caller_file):
6-
75
cli = data.sns.from_dict(data.json.read(os.path.join(os.path.dirname(__file__), '../package_data.json')))
8-
9-
# Load from config file
10-
cli.config = sns()
11-
cli.project_root = os.path.join(os.path.dirname(caller_file),
12-
f"{ '' if 'src' in os.path.dirname(caller_file) else '../../' }../../")
13-
possile_config_filenames = [
14-
'.translate-msgs.config.json', 'translate-msgs.config.json',
15-
f'.{cli.name}.config.json', f'{cli.name}.config.json'
16-
]
17-
for filename in possile_config_filenames:
18-
cli.config_path = os.path.join(cli.project_root, filename)
19-
if os.path.exists(cli.config_path):
20-
cli.config = data.sns.from_dict(data.json.read(cli.config_path))
21-
cli.config_filename = filename
22-
break
23-
24-
# Parse CLI args
25-
argp = argparse.ArgumentParser(
26-
description="Translate en/messages.json (chrome.i18n format) to other locales",
27-
add_help=False # disable default --help arg to re-create last
28-
)
29-
argp.add_argument('-d', '--locales-dir', '--locales-folder', '--json-dir', '--json-folder',
30-
type=str, help='Name of the folder containing locale files (default: "_locales")')
31-
argp.add_argument('-t', '--target-langs', '--target-lang', '--include-langs', '--include-lang',
32-
type=str, help='Languages to include (e.g. "en,es,fr") (default: all supported locales)')
33-
argp.add_argument('-k', '--keys', '--key', '--include-keys', '--include-key', '--translate-keys', '--translate-key',
34-
type=str, help='Keys to translate (e.g. "appDesc,err_notFound")')
35-
argp.add_argument('--exclude-langs', '--exclude-lang', '--ignore-langs', '--ignore-lang',
36-
type=str, help='Languages to exclude (e.g. "en,es")')
37-
argp.add_argument('--exclude-keys', '--exclude-key', '--ignore-keys', '--ignore-key',
38-
type=str, help='Keys to ignore (e.g. "appName,author")')
39-
argp.add_argument('-i', '--init', action='store_true', help=f'Create {cli.name}.config.json file to store defaults')
40-
argp.add_argument('-f', '--force', '--overwrite',
41-
action='store_true', help='Force overwrite existing config file when using --init')
42-
argp.add_argument('-W', '--no-wizard', '--skip-wizard',
43-
action='store_true', default=None, help='Skip interactive prompts during start-up')
44-
argp.add_argument('-h', '--help', action='help', help="Show help screen")
45-
cli.config.__dict__.update({ key:val for key,val in vars(argp.parse_args()).items() if val is not None })
46-
47-
# Init cli.config vals
48-
cli.config.target_langs = data.csv.parse(getattr(cli.config, 'target_langs', None))
49-
cli.config.target_locales = cli.config.target_langs or cli.supported_locales
50-
cli.config.exclude_langs = data.csv.parse(getattr(cli.config, 'exclude_langs', None))
51-
cli.config.keys = data.csv.parse(getattr(cli.config, 'keys', None))
52-
cli.config.exclude_keys = data.csv.parse(getattr(cli.config, 'exclude_keys', None))
53-
cli.config.locales_dir = getattr(cli.config, 'locales_dir', '_locales')
54-
if cli.config.exclude_langs:
55-
cli.config.target_locales = [lang for lang in cli.config.target_locales if lang not in cli.config.exclude_langs]
56-
cli.config.force = getattr(cli.config, 'force', False)
57-
cli.config.no_wizard = getattr(cli.config, 'no_wizard', False)
58-
6+
settings.load(cli, caller_file)
597
return cli
608

619
def config_file(cli):

translate-messages/src/translate_messages/lib/language.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ def create_translations(cli, target_msgs, lang_code):
3535
def write_translations(cli):
3636

3737
langs_added, langs_skipped, langs_translated, langs_not_translated = [], [], [], []
38-
for lang_code in cli.config.output_langs:
38+
for lang_code in cli.config.target_langs:
3939
lang_added, lang_skipped, lang_translated = False, False, False
4040
lang_folder = lang_code.replace('-', '_')
4141

4242
if lang_code.startswith('en'): # skip EN locales
43-
log.trunc(f'Skipped {lang_folder}/{cli.config.msgs_filename}...')
43+
print(f'\n{log.colors.gry}Skipped {lang_folder}/{cli.config.msgs_filename}...{log.colors.nc}', end='')
4444
langs_skipped.append(lang_code) ; langs_not_translated.append(lang_code) ; continue
4545

4646
if '-' in lang_code: # cap suffix
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import argparse, os
2+
from types import SimpleNamespace as sns
3+
from . import data
4+
5+
controls = sns(
6+
locales_dir=sns(
7+
args=['-d', '--locales-dir', '--locales-folder', '--json-dir', '--json-folder'],
8+
type=str, default_val='_locales', help='Name of the folder containing locale files (default: "_locales")'
9+
),
10+
target_langs=sns(
11+
args=['-t', '--target-langs', '--target-lang', '--include-langs', '--include-lang'],
12+
type=str, parser='csv', help='Languages to include (e.g. "en,es,fr") (default: all supported locales)'
13+
),
14+
keys=sns(
15+
args=['-k', '--keys', '--key', '--include-keys', '--include-key', '--translate-keys', '--translate-key'],
16+
type=str, parser='csv', help='Keys to translate (e.g. "appDesc,err_notFound")'
17+
),
18+
exclude_langs=sns(
19+
args=['--exclude-langs', '--exclude-lang', '--ignore-langs', '--ignore-lang'],
20+
type=str, parser='csv', help='Languages to exclude (e.g. "en,es")'
21+
),
22+
exclude_keys=sns(
23+
args=['--exclude-keys', '--exclude-key', '--ignore-keys', '--ignore-key'],
24+
type=str, parser='csv', help='Keys to ignore (e.g. "appName,author")'
25+
),
26+
init=sns(
27+
args=['-i', '--init'],
28+
action='store_true', help=f'Create .translate-msgs.config.json file to store defaults'
29+
),
30+
force=sns(
31+
args=['-f', '--force', '--overwrite'],
32+
action='store_true', help='Force overwrite existing config file when using --init'
33+
),
34+
no_wizard=sns(
35+
args=['-W', '--no-wizard', '--skip-wizard'],
36+
action='store_true', default=None, help='Skip interactive prompts during start-up'
37+
),
38+
help=sns(
39+
args=['-h', '--help'],
40+
action='help', help='Show help screen'
41+
)
42+
)
43+
44+
def load(cli, caller_file):
45+
46+
# Load from config file
47+
cli.config = sns()
48+
cli.project_root = os.path.join(os.path.dirname(caller_file),
49+
f"{ '' if 'src' in os.path.dirname(caller_file) else '../../' }../../")
50+
possile_config_filenames = [
51+
'.translate-msgs.config.json', 'translate-msgs.config.json',
52+
f'.{cli.name}.config.json', f'{cli.name}.config.json'
53+
]
54+
for filename in possile_config_filenames:
55+
cli.config_path = os.path.join(cli.project_root, filename)
56+
if os.path.exists(cli.config_path):
57+
cli.config = data.sns.from_dict(data.json.read(cli.config_path))
58+
cli.config_filename = filename
59+
break
60+
61+
# Parse CLI args
62+
argp = argparse.ArgumentParser(
63+
description="Translate en/messages.json (chrome.i18n format) to other locales",
64+
add_help=False # disable default --help arg to re-create last
65+
)
66+
for attr_name in vars(controls):
67+
kwargs = getattr(controls, attr_name).__dict__.copy()
68+
args = kwargs.pop('args') # separate positional flags
69+
for forbidden in ('default_val', 'parser'): # remove custom attrs
70+
kwargs.pop(forbidden, None)
71+
argp.add_argument(*args, **kwargs)
72+
cli.config.__dict__.update({ key:val for key,val in vars(argp.parse_args()).items() if val is not None})
73+
74+
# Init cli.config vals
75+
for name, ctrl in vars(controls).items():
76+
val = getattr(cli.config, name, None)
77+
if getattr(ctrl, 'parser', None) == 'csv':
78+
val = data.csv.parse(val)
79+
if getattr(ctrl, 'action', None) == 'store_true':
80+
val = val if val is not None else False
81+
if val is None and hasattr(ctrl, 'default_val'):
82+
val = ctrl.default_val
83+
setattr(cli.config, name, val)
84+
85+
if cli.config.exclude_langs:
86+
cli.config.target_langs = [lang for lang in cli.config.target_langs if lang not in cli.config.exclude_langs]

0 commit comments

Comments
 (0)