aboutsummaryrefslogtreecommitdiff
path: root/lib/python/milc.py
diff options
context:
space:
mode:
authorskullydazed <skullydazed@users.noreply.github.com>2020-04-15 13:49:22 -0700
committerGitHub <noreply@github.com>2020-04-15 22:49:22 +0200
commit484c059d867e93e9374902121c6ce64774d28d7d (patch)
treee02b6442ee7799e73647599c61b550524c13994a /lib/python/milc.py
parent3cc68543ca8c69048ffc0655d92be238afcee376 (diff)
downloadqmk_firmware-484c059d867e93e9374902121c6ce64774d28d7d.tar.gz
qmk_firmware-484c059d867e93e9374902121c6ce64774d28d7d.zip
MILC: Fix setting config values for store_true and store_false (#8813)
Diffstat (limited to 'lib/python/milc.py')
-rw-r--r--lib/python/milc.py88
1 files changed, 52 insertions, 36 deletions
diff --git a/lib/python/milc.py b/lib/python/milc.py
index 83edfc7f5..eb18984eb 100644
--- a/lib/python/milc.py
+++ b/lib/python/milc.py
@@ -242,15 +242,24 @@ class SubparserWrapper(object):
242 242
243 This also stores the default for the argument in `self.cli.default_arguments`. 243 This also stores the default for the argument in `self.cli.default_arguments`.
244 """ 244 """
245 if 'action' in kwargs and kwargs['action'] == 'store_boolean': 245 if kwargs.get('action') == 'store_boolean':
246 # Store boolean will call us again with the enable/disable flag arguments 246 # Store boolean will call us again with the enable/disable flag arguments
247 return handle_store_boolean(self, *args, **kwargs) 247 return handle_store_boolean(self, *args, **kwargs)
248 248
249 self.cli.acquire_lock() 249 self.cli.acquire_lock()
250 argument_name = self.cli.get_argument_name(*args, **kwargs)
251
250 self.subparser.add_argument(*args, **kwargs) 252 self.subparser.add_argument(*args, **kwargs)
253
254 if kwargs.get('action') == 'store_false':
255 self.cli._config_store_false.append(argument_name)
256
257 if kwargs.get('action') == 'store_true':
258 self.cli._config_store_true.append(argument_name)
259
251 if self.submodule not in self.cli.default_arguments: 260 if self.submodule not in self.cli.default_arguments:
252 self.cli.default_arguments[self.submodule] = {} 261 self.cli.default_arguments[self.submodule] = {}
253 self.cli.default_arguments[self.submodule][self.cli.get_argument_name(*args, **kwargs)] = kwargs.get('default') 262 self.cli.default_arguments[self.submodule][argument_name] = kwargs.get('default')
254 self.cli.release_lock() 263 self.cli.release_lock()
255 264
256 265
@@ -268,11 +277,13 @@ class MILC(object):
268 277
269 # Define some basic info 278 # Define some basic info
270 self.acquire_lock() 279 self.acquire_lock()
280 self._config_store_true = []
281 self._config_store_false = []
271 self._description = None 282 self._description = None
272 self._entrypoint = None 283 self._entrypoint = None
273 self._inside_context_manager = False 284 self._inside_context_manager = False
274 self.ansi = ansi_colors 285 self.ansi = ansi_colors
275 self.arg_only = [] 286 self.arg_only = {}
276 self.config = self.config_source = None 287 self.config = self.config_source = None
277 self.config_file = None 288 self.config_file = None
278 self.default_arguments = {} 289 self.default_arguments = {}
@@ -377,7 +388,7 @@ class MILC(object):
377 self.add_argument('--log-file', help='File to write log messages to') 388 self.add_argument('--log-file', help='File to write log messages to')
378 self.add_argument('--color', action='store_boolean', default=True, help='color in output') 389 self.add_argument('--color', action='store_boolean', default=True, help='color in output')
379 self.add_argument('--config-file', help='The location for the configuration file') 390 self.add_argument('--config-file', help='The location for the configuration file')
380 self.arg_only.append('config_file') 391 self.arg_only['config_file'] = ['general']
381 392
382 def add_subparsers(self, title='Sub-commands', **kwargs): 393 def add_subparsers(self, title='Sub-commands', **kwargs):
383 if self._inside_context_manager: 394 if self._inside_context_manager:
@@ -427,17 +438,20 @@ class MILC(object):
427 raise RuntimeError('You must run this before the with statement!') 438 raise RuntimeError('You must run this before the with statement!')
428 439
429 def argument_function(handler): 440 def argument_function(handler):
430 if 'arg_only' in kwargs and kwargs['arg_only']: 441 subcommand_name = handler.__name__.replace("_", "-")
442
443 if kwargs.get('arg_only'):
431 arg_name = self.get_argument_name(*args, **kwargs) 444 arg_name = self.get_argument_name(*args, **kwargs)
432 self.arg_only.append(arg_name) 445 if arg_name not in self.arg_only:
446 self.arg_only[arg_name] = []
447 self.arg_only[arg_name].append(subcommand_name)
433 del kwargs['arg_only'] 448 del kwargs['arg_only']
434 449
435 name = handler.__name__.replace("_", "-")
436 if handler is self._entrypoint: 450 if handler is self._entrypoint:
437 self.add_argument(*args, **kwargs) 451 self.add_argument(*args, **kwargs)
438 452
439 elif name in self.subcommands: 453 elif subcommand_name in self.subcommands:
440 self.subcommands[name].add_argument(*args, **kwargs) 454 self.subcommands[subcommand_name].add_argument(*args, **kwargs)
441 455
442 else: 456 else:
443 raise RuntimeError('Decorated function is not entrypoint or subcommand!') 457 raise RuntimeError('Decorated function is not entrypoint or subcommand!')
@@ -511,35 +525,37 @@ class MILC(object):
511 if argument in ('subparsers', 'entrypoint'): 525 if argument in ('subparsers', 'entrypoint'):
512 continue 526 continue
513 527
514 if argument not in self.arg_only: 528 # Find the argument's section
515 # Find the argument's section 529 # Underscores in command's names are converted to dashes during initialization.
516 # Underscores in command's names are converted to dashes during initialization. 530 # TODO(Erovia) Find a better solution
517 # TODO(Erovia) Find a better solution 531 entrypoint_name = self._entrypoint.__name__.replace("_", "-")
518 entrypoint_name = self._entrypoint.__name__.replace("_", "-") 532 if entrypoint_name in self.default_arguments and argument in self.default_arguments[entrypoint_name]:
519 if entrypoint_name in self.default_arguments and argument in self.default_arguments[entrypoint_name]: 533 argument_found = True
520 argument_found = True 534 section = self._entrypoint.__name__
521 section = self._entrypoint.__name__ 535 if argument in self.default_arguments['general']:
522 if argument in self.default_arguments['general']: 536 argument_found = True
523 argument_found = True 537 section = 'general'
524 section = 'general' 538
525 539 if not argument_found:
526 if not argument_found: 540 raise RuntimeError('Could not find argument in `self.default_arguments`. This should be impossible!')
527 raise RuntimeError('Could not find argument in `self.default_arguments`. This should be impossible!') 541 exit(1)
528 exit(1) 542
543 if argument not in self.arg_only or section not in self.arg_only[argument]:
544 # Determine the arg value and source
545 arg_value = getattr(self.args, argument)
546 if argument in self._config_store_true and arg_value:
547 passed_on_cmdline = True
548 elif argument in self._config_store_false and not arg_value:
549 passed_on_cmdline = True
550 elif arg_value is not None:
551 passed_on_cmdline = True
552 else:
553 passed_on_cmdline = False
529 554
530 # Merge this argument into self.config 555 # Merge this argument into self.config
531 if argument in self.default_arguments['general'] or argument in self.default_arguments[entrypoint_name]: 556 if passed_on_cmdline and (argument in self.default_arguments['general'] or argument in self.default_arguments[entrypoint_name] or argument not in self.config[entrypoint_name]):
532 arg_value = getattr(self.args, argument) 557 self.config[section][argument] = arg_value
533 if arg_value is not None: 558 self.config_source[section][argument] = 'argument'
534 self.config[section][argument] = arg_value
535 self.config_source[section][argument] = 'argument'
536 else:
537 if argument not in self.config[entrypoint_name]:
538 # Check if the argument exist for this section
539 arg = getattr(self.args, argument)
540 if arg is not None:
541 self.config[section][argument] = arg
542 self.config_source[section][argument] = 'argument'
543 559
544 self.release_lock() 560 self.release_lock()
545 561