|
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 |
4 | 3 |
|
5 | 4 | def cli(caller_file): |
6 | | - |
7 | 5 | 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) |
59 | 7 | return cli |
60 | 8 |
|
61 | 9 | def config_file(cli): |
|
0 commit comments