emaint: remove callback usage
authorZac Medico <zmedico@gentoo.org>
Fri, 2 Aug 2013 20:18:11 +0000 (13:18 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 2 Aug 2013 20:18:11 +0000 (13:18 -0700)
This will simplify conversion to argparse.

pym/portage/emaint/defaults.py
pym/portage/emaint/main.py
pym/portage/emaint/modules/logs/__init__.py

index d9d83ffbb11c534f6f313e362b385c344b356059..ee0dc11183fd97d37e30e3eac3cabfe531cad455 100644 (file)
@@ -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'
        }
 
index 5775ce1185200f699e2f8eec449c48492790373f..4b6d6669c711c81f63cbca41af73f4925e7871fb 100644 (file)
@@ -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.
index 6f62d03629de56ef1e701dd5e6a5cf0cb13c406c..c5204b761f07c6e549d70bb14f55ec662912d132 100644 (file)
@@ -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'
                                        }
                                }