Skip to content

Commit 22cb9dd

Browse files
committed
Added --debug [target_key] support
1 parent 904ec3b commit 22cb9dd

4 files changed

Lines changed: 52 additions & 14 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
)
1919

2020
def data(msg, *args, **kwargs) : print(f'\n{colors.bw}{msg.format(*args, **kwargs)}{colors.nc}')
21-
def debug(msg, *args, **kwargs):
22-
if '--debug' in sys.argv : print(f'\n{colors.by}DEBUG: {msg.format(*args, **kwargs)}{colors.nc}')
2321
def dim(msg, *args, **kwargs) : print(f'\n{colors.gry}{msg.format(*args, **kwargs)}{colors.nc}')
2422
def error(msg, *args, **kwargs) : print(f'\n{colors.br}ERROR: {msg.format(*args, **kwargs)}{colors.nc}')
2523
def info(msg, *args, end='', **kwargs) : print(f'\n{colors.by}{msg.format(*args, **kwargs)}{colors.nc}', end=end)
@@ -29,6 +27,16 @@ def success(msg, *args, **kwargs) : print(f'\n{colors.bg}{msg.format(*args, **kw
2927
def tip(msg, *args, **kwargs) : print(f'\n{colors.bo}TIP: {msg.format(*args, **kwargs)}{colors.nc}')
3028
def warn(msg, *args, **kwargs) : print(f'\n{colors.bo}WARNING: {msg.format(*args, **kwargs)}{colors.nc}')
3129

30+
def debug(msg, cli=None, *args, **kwargs):
31+
if '--debug' not in sys.argv : return
32+
data = colors.gry
33+
if cli:
34+
if getattr(cli, 'debug_key', None):
35+
data += str(getattr(cli.config, cli.debug_key, f'Key "{cli.debug_key}" not found'))
36+
else:
37+
data += str(cli.config)
38+
print(f'\n{colors.by}DEBUG: {msg.format(data, *args, **kwargs)}{colors.nc}')
39+
3240
def final_summary(msgs, summary_dict):
3341
success(f'{msgs.log_ALL_JSON_PROCESSED}!')
3442
for name, file_set in summary_dict.items():

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@
3131
),
3232
debug=sn(
3333
args=['--debug'],
34-
action='store_true', help='Show debug logs'
34+
nargs='?', const=True, help='Show debug logs'
3535
)
3636
)
3737

3838
def load(cli, caller_file):
3939

40+
# Init debug target
41+
if '--debug' in sys.argv:
42+
debug_arg_idx = sys.argv.index('--debug')
43+
if len(sys.argv) > debug_arg_idx +1 and not sys.argv[debug_arg_idx +1].startswith('-'):
44+
cli.debug_key = sys.argv[debug_arg_idx +1]
45+
4046
# Assign help tips from cli.msgs
4147
for ctrl_key, ctrl in vars(controls).items():
4248
if not hasattr(ctrl, 'help') : ctrl.help = getattr(cli.msgs, f'help_{ctrl_key.upper()}')
@@ -48,7 +54,8 @@ def load(cli, caller_file):
4854
else caller_path.parent.parent)
4955
possible_config_filenames = [
5056
f'{prefix}{name}.config.json{suffix}'
51-
for prefix in ['.', ''] for name in [cli.short_name, cli.name] for suffix in ['5', '', 'c']
57+
for prefix in ['.', ''] for name in [cli.short_name, cli.name]
58+
for suffix in ['5', '', 'c']
5259
]
5360
for filename in possible_config_filenames:
5461
config_path = Path(cli.project_root) / filename
@@ -57,7 +64,10 @@ def load(cli, caller_file):
5764
cli.config = data.sns.from_dict(data.json.read(cli.config_filepath))
5865
cli.config_filename = filename
5966
break
60-
log.debug(f'Config file loaded!\n{log.colors.gry}{cli.config}' if cli.config_filepath else 'No config file found.')
67+
if hasattr(cli, 'config_filename'):
68+
log.debug('Config file loaded!\n{}', cli)
69+
else:
70+
log.debug('No config file found.')
6171

6272
# Parse CLI args
6373
argp = argparse.ArgumentParser(description=cli.description, add_help=False)
@@ -74,7 +84,7 @@ def load(cli, caller_file):
7484
for key, val in vars(parsed_args).items(): # apply parsed_args to cli.config
7585
if not getattr(cli.config, key, ''):
7686
setattr(cli.config, key, val)
77-
log.debug(f'Args parsed!\n{log.colors.gry}{cli.config}')
87+
log.debug('Args parsed!\n{}', cli)
7888

7989
# Init all cli.config vals
8090
for name, ctrl in vars(controls).items():
@@ -84,4 +94,5 @@ def load(cli, caller_file):
8494
if val is None and hasattr(ctrl, 'default_val'):
8595
val = ctrl.default_val
8696
setattr(cli.config, name, val)
87-
log.debug(f'All cli.config vals set!\n{log.colors.gry}{cli.config}')
97+
log.debug('All cli.config vals set!\n{}', cli)
98+

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
)
1919

2020
def data(msg, *args, **kwargs) : print(f'\n{colors.bw}{msg.format(*args, **kwargs)}{colors.nc}')
21-
def debug(msg, *args, **kwargs):
22-
if '--debug' in sys.argv : print(f'\n{colors.by}DEBUG: {msg.format(*args, **kwargs)}{colors.nc}')
2321
def dim(msg, *args, **kwargs) : print(f'\n{colors.gry}{msg.format(*args, **kwargs)}{colors.nc}')
2422
def error(msg, *args, **kwargs) : print(f'\n{colors.br}ERROR: {msg.format(*args, **kwargs)}{colors.nc}')
2523
def info(msg, *args, end='', **kwargs) : print(f'\n{colors.by}{msg.format(*args, **kwargs)}{colors.nc}', end=end)
@@ -29,6 +27,16 @@ def success(msg, *args, **kwargs) : print(f'\n{colors.bg}{msg.format(*args, **kw
2927
def tip(msg, *args, **kwargs) : print(f'\n{colors.bo}TIP: {msg.format(*args, **kwargs)}{colors.nc}')
3028
def warn(msg, *args, **kwargs) : print(f'\n{colors.bo}WARNING: {msg.format(*args, **kwargs)}{colors.nc}')
3129

30+
def debug(msg, cli=None, *args, **kwargs):
31+
if '--debug' not in sys.argv : return
32+
data = colors.gry
33+
if cli:
34+
if getattr(cli, 'debug_key', None):
35+
data += str(getattr(cli.config, cli.debug_key, f'Key "{cli.debug_key}" not found'))
36+
else:
37+
data += str(cli.config)
38+
print(f'\n{colors.by}DEBUG: {msg.format(data, *args, **kwargs)}{colors.nc}')
39+
3240
def final_summary(msgs, summary_dict):
3341
success(f'\n{msgs.log_ALL_JSON_UPDATED}!')
3442
for name, lang_set in summary_dict.items():

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@
2828
help=sn(
2929
args=['-h', '--help'], action='help'),
3030
debug=sn(
31-
args=['--debug'], action='store_true')
31+
args=['--debug'], nargs='?', const=True)
3232
)
3333

3434
def load(cli, caller_file):
3535

36+
# Init debug target
37+
if '--debug' in sys.argv:
38+
debug_arg_idx = sys.argv.index('--debug')
39+
if len(sys.argv) > debug_arg_idx +1 and not sys.argv[debug_arg_idx +1].startswith('-'):
40+
cli.debug_key = sys.argv[debug_arg_idx +1]
41+
3642
# Assign help tips from cli.msgs
3743
for ctrl_key, ctrl in vars(controls).items():
3844
if not hasattr(ctrl, 'help') : ctrl.help = getattr(cli.msgs, f'help_{ctrl_key.upper()}')
@@ -44,7 +50,8 @@ def load(cli, caller_file):
4450
else caller_path.parent.parent)
4551
possible_config_filenames = [
4652
f'{prefix}{name}.config.json{suffix}'
47-
for prefix in ['.', ''] for name in [cli.short_name, cli.name] for suffix in ['5', '', 'c']
53+
for prefix in ['.', ''] for name in [cli.short_name, cli.name]
54+
for suffix in ['5', '', 'c']
4855
]
4956
for filename in possible_config_filenames:
5057
config_path = Path(cli.project_root) / filename
@@ -53,7 +60,10 @@ def load(cli, caller_file):
5360
cli.config = data.sns.from_dict(data.json.read(cli.config_filepath))
5461
cli.config_filename = filename
5562
break
56-
log.debug(f'Config file loaded!\n{log.colors.gry}{cli.config}' if cli.config_filepath else 'No config file found.')
63+
if hasattr(cli, 'config_filename'):
64+
log.debug('Config file loaded!\n{}', cli)
65+
else:
66+
log.debug('No config file found.')
5767

5868
# Parse CLI args
5969
argp = argparse.ArgumentParser(description=cli.description, add_help=False)
@@ -70,7 +80,7 @@ def load(cli, caller_file):
7080
for key, val in vars(parsed_args).items(): # apply parsed_args to cli.config
7181
if not getattr(cli.config, key, ''):
7282
setattr(cli.config, key, val)
73-
log.debug(f'Args parsed!\n{log.colors.gry}{cli.config}')
83+
log.debug('Args parsed!\n{}', cli)
7484

7585
# Init all cli.config vals
7686
for name, ctrl in vars(controls).items():
@@ -80,4 +90,5 @@ def load(cli, caller_file):
8090
if val is None and hasattr(ctrl, 'default_val'):
8191
val = ctrl.default_val
8292
setattr(cli.config, name, val)
83-
log.debug(f'All cli.config vals set!\n{log.colors.gry}{cli.config}')
93+
log.debug('All cli.config vals set!\n{}', cli)
94+

0 commit comments

Comments
 (0)