From: Zac Medico Date: Fri, 2 Aug 2013 20:18:11 +0000 (-0700) Subject: emaint: remove callback usage X-Git-Tag: v2.2.0_alpha192~18 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=42b0a6e898172c375d4d47c3346b25dac2333bf0;p=portage.git emaint: remove callback usage This will simplify conversion to argparse. --- diff --git a/pym/portage/emaint/defaults.py b/pym/portage/emaint/defaults.py index d9d83ffbb..ee0dc1118 100644 --- a/pym/portage/emaint/defaults.py +++ b/pym/portage/emaint/defaults.py @@ -1,16 +1,18 @@ -# Copyright 2005-2012 Gentoo Foundation +# Copyright 2005-2013 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # parser option data CHECK = {"short": "-c", "long": "--check", "help": "Check for problems (a default option for most modules)", 'status': "Checking %s for problems", + 'action': 'store_true', 'func': 'check' } FIX = {"short": "-f", "long": "--fix", "help": "Attempt to fix problems (a default option for most modules)", 'status': "Attempting to fix %s", + 'action': 'store_true', 'func': 'fix' } diff --git a/pym/portage/emaint/main.py b/pym/portage/emaint/main.py index 5775ce118..4b6d6669c 100644 --- a/pym/portage/emaint/main.py +++ b/pym/portage/emaint/main.py @@ -30,36 +30,16 @@ class OptionItem(object): self.help = opt['help'] self.status = opt['status'] self.func = opt['func'] - self.action = opt.get('action', "callback") + self.action = opt.get('action', 'store') self.type = opt.get('type', None) self.dest = opt.get('dest', None) - self.callback = opt.get('callback', self._exclusive) - self.callback_kwargs = opt.get('callback_kwargs', {"var":"action"}) - def _exclusive(self, option, *args, **kw): """Generic check for the 2 default options """ - var = kw.get("var", None) - if var is None: - raise ValueError("var not specified to exclusive()") if getattr(self.parser, var, ""): raise OptionValueError("%s and %s are exclusive options" % (getattr(self.parser, var), option)) - setattr(self.parser, var, str(option)) - - def check_action(self, action): - """Checks if 'action' is the same as this option - - @type action: string - @param action: the action to compare - @rtype: boolean - """ - if action == self.action: - return True - elif action == '/'.join([self.short, self.long]): - return True - return False def usage(module_controller): @@ -172,24 +152,27 @@ def emaint_main(myargv): parser_options.append(OptionItem(desc[opt], parser)) for opt in parser_options: parser.add_option(opt.short, opt.long, help=opt.help, action=opt.action, - type=opt.type, dest=opt.dest, - callback=opt.callback, callback_kwargs=opt.callback_kwargs) - - parser.action = None + type=opt.type, dest=opt.dest) (options, args) = parser.parse_args(args=myargv) - #print('options', options, '\nargs', args, '\naction', parser.action) if len(args) != 1: parser.error("Incorrect number of arguments") if args[0] not in module_names: parser.error("%s target is not a known target" % args[0]) - if parser.action: - action = parser.action - else: - action = "-c/--check" - long_action = action.split('/')[1].lstrip('-') - #print("DEBUG: action = ", action, long_action) + + func = status = long_action = None + for opt in parser_options: + if opt.long == '--check': + # Default action + status = opt.status + func = opt.func + long_action = 'check' + elif opt.status and getattr(options, opt.long.lstrip("-"), False): + status = opt.status + func = opt.func + long_action = opt.long.lstrip('-') + break if args[0] == "all": tasks = [] @@ -200,15 +183,12 @@ def emaint_main(myargv): elif long_action in module_controller.get_functions(args[0]): tasks = [module_controller.get_class(args[0] )] else: - print("\nERROR: module '%s' does not have option '%s'\n" %(args[0], action)) - print(module_opts(module_controller, args[0])) + portage.util.writemsg( + "\nERROR: module '%s' does not have option '--%s'\n\n" % + (args[0], long_action), noiselevel=-1) + portage.util.writemsg(module_opts(module_controller, args[0]), + noiselevel=-1) sys.exit(1) - func = status = None - for opt in parser_options: - if opt.check_action(action): - status = opt.status - func = opt.func - break # need to pass the parser options dict to the modules # so they are available if needed. diff --git a/pym/portage/emaint/modules/logs/__init__.py b/pym/portage/emaint/modules/logs/__init__.py index 6f62d0362..c5204b761 100644 --- a/pym/portage/emaint/modules/logs/__init__.py +++ b/pym/portage/emaint/modules/logs/__init__.py @@ -20,17 +20,15 @@ module_spec = { "help": "Cleans out logs more than 7 days old (cleanlogs only)" + \ " module-options: -t, -p", 'status': "Cleaning %s", - 'func': 'clean' + 'action': 'store_true', + 'func': 'clean', }, 'time': { "short": "-t", "long": "--time", "help": "(cleanlogs only): -t, --time Delete logs older than NUM of days", 'status': "", - 'action': 'store', 'type': 'int', 'dest': 'NUM', - 'callback': None, - 'callback_kwargs': None, 'func': 'clean' }, 'pretend': { @@ -39,8 +37,6 @@ module_spec = { 'status': "", 'action': 'store_true', 'dest': 'pretend', - 'callback': None, - 'callback_kwargs': None, 'func': 'clean' } }