def _check_type(self): if self.type is None: if self.action in self.ALWAYS_TYPED_ACTIONS: if self.choices is not None: # The "choices" attribute implies "choice" type. self.type = "choice" else: # No type given? "string" is the most sensible default. self.type = "string" else: # Allow type objects or builtin type conversion functions # (int, str, etc.) as an alternative to their names. (The # complicated check of __builtin__ is only necessary for # Python 2.1 and earlier, and is short-circuited by the # first check on modern Pythons.) import __builtin__ if (type(self.type) is types.TypeType or (hasattr(self.type, "__name__") and getattr(__builtin__, self.type.__name__, None) is self.type)): self.type = self.type.__name__ if self.type == "str": self.type = "string" if self.type not in self.TYPES: raise OptionError("invalid option type: %r" % self.type, self) if self.action not in self.TYPED_ACTIONS: raise OptionError( "must not supply a type for action %r" % self.action, self)
def _check_choice(self): if self.type == "choice": if self.choices is None: raise OptionError( "must supply a list of choices for type 'choice'", self) elif type(self.choices) not in (types.TupleType, types.ListType): raise OptionError( "choices must be a list of strings ('%s' supplied)" % str(type(self.choices)).split("'")[1], self) elif self.choices is not None: raise OptionError( "must not supply choices for type %r" % self.type, self)
def _parse_command(command): """" Parses a command into action and options. Returns a dictionary with following keys: selector (string): the selector to identify the element action (string): the action type (if it's recognized) options (list): a list of options Raises an error if action type is not recognized or if options are invalid. Example inputs: #sync_button click NEXT send_keys some keys NEXT send_keys special characters like TAB and ENTER can be used like this Note that 'TAB', 'ENTER', and 'BACKSPACE' all have special meaning for send_keys """ command_args = command.split() if not command_args: return None else: selector = command_args[0] action = command_args[1] options = command_args[2:] if action not in ('click', 'send_keys', 'submit', ''): raise ActionError(action) # Validate input options for the specified action. if action == 'click' or action == 'submit': if options: raise OptionError("The action '%s' must not contain any arguments whereas supplied arguments: '%s'." % (action, repr(options))) return {'selector': selector, 'action': action, 'options': options}
def _check_nargs(self): if self.action in self.TYPED_ACTIONS: if self.nargs is None: self.nargs = 1 elif self.nargs is not None: raise OptionError( "'nargs' must not be supplied for action %r" % self.action, self)
def _set_opt_strings(self, opts): for opt in opts: if len(opt) < 2: raise OptionError( "invalid option string %r: " "must be at least two characters long" % opt, self) elif len(opt) == 2: if not (opt[0] == "-" and opt[1] != "-"): raise OptionError( "invalid short option string %r: " "must be of the form -x, (x any non-dash char)" % opt, self) self._short_opts.append(opt) else: if not (opt[0:2] == "--" and opt[2] != "-"): raise OptionError( "invalid long option string %r: " "must start with --, followed by non-dash" % opt, self) self._long_opts.append(opt)
def _check_callback(self): if self.action == "callback": if not callable(self.callback): raise OptionError("callback not callable: %r" % self.callback, self) if (self.callback_args is not None and type(self.callback_args) is not types.TupleType): raise OptionError( "callback_args, if supplied, must be a tuple: not %r" % self.callback_args, self) if (self.callback_kwargs is not None and type(self.callback_kwargs) is not types.DictType): raise OptionError( "callback_kwargs, if supplied, must be a dict: not %r" % self.callback_kwargs, self) else: if self.callback is not None: raise OptionError( "callback supplied (%r) for non-callback option" % self.callback, self) if self.callback_args is not None: raise OptionError( "callback_args supplied for non-callback option", self) if self.callback_kwargs is not None: raise OptionError( "callback_kwargs supplied for non-callback option", self)
def _set_attrs(self, attrs): for attr in self.ATTRS: if attrs.has_key(attr): setattr(self, attr, attrs[attr]) del attrs[attr] else: if attr == 'default': setattr(self, attr, NO_DEFAULT) else: setattr(self, attr, None) if attrs: attrs = attrs.keys() attrs.sort() raise OptionError( "invalid keyword arguments: %s" % ", ".join(attrs), self)
def _parse_command(command): """" Parses a command into action and options. Returns a dictionary with following keys: selector (string): the selector to identify the element action (string): the action type (if it's recognized) options (list): a list of options Raises an error if action type is not recognized or if options are invalid. """ command_args = command.split() selector = command_args[0] action = command_args[1] options = command_args[2:] if action not in ('click', 'send_keys', 'submit'): raise ActionError(action) # Validate input options for the specified action. if action == 'click' or action == 'submit': if options: raise OptionError("The action '%s' must not contain any arguments whereas supplied arguments: '%s'." % (action, repr(options))) return {'selector': selector, 'action': action, 'options': options}
def _check_const(self): if self.action not in self.CONST_ACTIONS and self.const is not None: raise OptionError( "'const' must not be supplied for action %r" % self.action, self)
def _check_action(self): if self.action is None: self.action = "store" elif self.action not in self.ACTIONS: raise OptionError("invalid action: %r" % self.action, self)