Exemple #1
0
 def set_option(self, optname, value, action=None, optdict=None):
     """method called to set an option (registered in the options list)"""
     if optdict is None:
         optdict = self.get_option_def(optname)
     if value is not None:
         value = _validate(value, optdict, optname)
     if action is None:
         action = optdict.get("action", "store")
     if action == "store":
         setattr(self.config, self.option_attrname(optname, optdict), value)
     elif action in ("store_true", "count"):
         setattr(self.config, self.option_attrname(optname, optdict), 0)
     elif action == "store_false":
         setattr(self.config, self.option_attrname(optname, optdict), 1)
     elif action == "append":
         optname = self.option_attrname(optname, optdict)
         _list = getattr(self.config, optname, None)
         if _list is None:
             if isinstance(value, (list, tuple)):
                 _list = value
             elif value is not None:
                 _list = []
                 _list.append(value)
             setattr(self.config, optname, _list)
         elif isinstance(_list, tuple):
             setattr(self.config, optname, _list + (value,))
         else:
             _list.append(value)
     elif action == "callback":
         optdict["callback"](None, optname, value, None)
     else:
         raise UnsupportedAction(action)
Exemple #2
0
 def set_option(self, optname, value, action=None, optdict=None):
     """Method called to set an option (registered in the options list)."""
     if optdict is None:
         optdict = self.get_option_def(optname)
     if value is not None:
         value = _validate(value, optdict, optname)
     if action is None:
         action = optdict.get("action", "store")
     if action == "store":
         setattr(self.config, self.option_attrname(optname, optdict), value)
     elif action in {"store_true", "count"}:
         setattr(self.config, self.option_attrname(optname, optdict), value)
     elif action == "store_false":
         setattr(self.config, self.option_attrname(optname, optdict), value)
     elif action == "append":
         optname = self.option_attrname(optname, optdict)
         _list = getattr(self.config, optname, None)
         if _list is None:
             if isinstance(value, (list, tuple)):
                 _list = value
             elif value is not None:
                 _list = [value]
             setattr(self.config, optname, _list)
         elif isinstance(_list, tuple):
             setattr(self.config, optname, _list + (value,))
         else:
             _list.append(value)
     elif (
         action == "callback"
         or (not isinstance(action, str))
         and issubclass(action, _CallbackAction)
     ):
         return
     else:
         raise UnsupportedAction(action)