Esempio n. 1
0
 def test_is_list_var(self):
     for ok in LISTS:
         assert is_list_var(ok)
         assert is_list_var(ok + '=', allow_assign_mark=True)
         assert is_list_var(ok + ' =', allow_assign_mark=True)
     for nok in NOKS + SCALARS + DICTS:
         assert not is_list_var(nok)
 def validate(self, positional, named, dryrun=False, no_error=False):
     if dryrun and any(is_list_var(arg) for arg in positional):
         return
     if no_error and any(is_list_var(arg) for arg in positional):
         return
     named = set(name for name, value in named)
     self._validate_no_multiple_values(positional, named, self._argspec)
     self._validate_limits(positional, named, self._argspec)
     self._validate_no_mandatory_missing(positional, named, self._argspec)
Esempio n. 3
0
 def _set(self, variable):
     if is_scalar_var(variable):
         self.scalar_vars.append(variable)
     elif is_list_var(variable):
         self.list_var = variable
     else:
         raise DataError('Invalid variable to assign: %s' % variable)
Esempio n. 4
0
 def _set(self, variable):
     if is_scalar_var(variable):
         self.scalar_vars.append(variable)
     elif is_list_var(variable):
         self.list_var = variable
     else:
         raise DataError('Invalid variable to assign: %s' % variable)
Esempio n. 5
0
    def _get_arg_spec(self, origargs):
        """Returns argument spec in a tuple (args, defaults, varargs).

        args     - tuple of all accepted arguments
        defaults - tuple of default values
        varargs  - name of the argument accepting varargs or None

        Examples:
          ['${arg1}', '${arg2}']
            => ('${arg1}', '${arg2}'), (), None
          ['${arg1}', '${arg2}=default', '@{varargs}']
            => ('${arg1}', '${arg2}'), ('default',), '@{varargs}'
        """
        args = []
        defaults = []
        varargs = None
        for arg in origargs:
            if varargs:
                raise DataError('Only last argument can be a list')
            if is_list_var(arg):
                varargs = arg
                continue   # should be last round (otherwise DataError in next)
            arg, default = self._split_default(arg)
            if defaults and default is None:
                raise DataError('Non default argument after default arguments')
            if not is_scalar_var(arg):
                raise DataError("Invalid argument '%s'" % arg)
            args.append(arg)
            if default is not None:
                defaults.append(default)
        return args, defaults, varargs
 def validate(self, positional, named, dryrun=False):
     if dryrun and any(is_list_var(arg) for arg in positional):
         return
     named = set(name for name, value in named)
     self._validate_no_multiple_values(positional, named, self._argspec)
     self._validate_limits(positional, named, self._argspec)
     self._validate_no_mandatory_missing(positional, named, self._argspec)
    def _get_arg_spec(self, origargs):
        """Returns argument spec in a tuple (args, defaults, varargs).

        args     - tuple of all accepted arguments
        defaults - tuple of default values
        varargs  - name of the argument accepting varargs or None

        Examples:
          ['${arg1}', '${arg2}']
            => ('${arg1}', '${arg2}'), (), None
          ['${arg1}', '${arg2}=default', '@{varargs}']
            => ('${arg1}', '${arg2}'), ('default',), '@{varargs}'
        """
        args = []
        defaults = []
        varargs = None
        for arg in origargs:
            if varargs:
                raise DataError('Only last argument can be a list')
            if is_list_var(arg):
                varargs = arg
                continue  # should be last round (otherwise DataError in next)
            arg, default = self._split_default(arg)
            if defaults and default is None:
                raise DataError('Non default argument after default arguments')
            if not is_scalar_var(arg):
                raise DataError("Invalid argument '%s'" % arg)
            args.append(arg)
            if default is not None:
                defaults.append(default)
        return args, defaults, varargs
Esempio n. 8
0
 def _split_run_kw_if_args(self, given_args, control_word, required_after):
     index = list(given_args).index(control_word)
     expr_and_call = given_args[:index]
     remaining = given_args[index + 1 :]
     if not (self._validate_kw_call(expr_and_call) and self._validate_kw_call(remaining, required_after)):
         raise DataError("Invalid 'Run Keyword If' usage.")
     if is_list_var(expr_and_call[0]):
         return (), remaining
     return expr_and_call[1:], remaining
Esempio n. 9
0
 def _get_return_value(self, variables, return_):
     ret = self.return_value if not return_ else return_.return_value
     if not ret:
         return None
     contains_list_var = any(is_list_var(item) for item in ret)
     try:
         ret = variables.replace_list(ret)
     except DataError, err:
         raise DataError("Replacing variables from keyword return value " "failed: %s" % unicode(err))
Esempio n. 10
0
 def _split_run_kw_if_args(self, given_args, control_word, required_after):
     index = given_args.index(control_word)
     expr_and_call = given_args[:index]
     remaining = given_args[index + 1:]
     if not (self._validate_kw_call(expr_and_call)
             and self._validate_kw_call(remaining, required_after)):
         raise DataError("Invalid 'Run Keyword If' usage.")
     if is_list_var(expr_and_call[0]):
         return [], remaining
     return expr_and_call[1:], remaining
Esempio n. 11
0
 def _get_return_value(self, variables, return_):
     ret = self.return_value if not return_ else return_.return_value
     if not ret:
         return None
     contains_list_var = any(is_list_var(item) for item in ret)
     try:
         ret = variables.replace_list(ret)
     except DataError, err:
         raise DataError('Replacing variables from keyword return value '
                         'failed: %s' % unicode(err))
Esempio n. 12
0
 def _verity_values_for_set_variable_if(self,values,default=False):
     if not values:
         if default:
             return [None]
         raise RuntimeError('At least one value is required')
     if is_list_var(values[0]):
         values[:1] =[utils.escape(item) for item in
                      self._variables[values[0]]]
         return self._verity_values_for_set_variable_if(values)
     return values
Esempio n. 13
0
 def _get_run_kw_if_calls(self, given_args):
     while 'ELSE IF' in given_args:
         kw_call, given_args = self._split_run_kw_if_args(given_args, 'ELSE IF', 2)
         yield kw_call
     if 'ELSE' in given_args:
         kw_call, else_call = self._split_run_kw_if_args(given_args, 'ELSE', 1)
         yield kw_call
         yield else_call
     elif self._validate_kw_call(given_args):
         expr, kw_call = given_args[0], given_args[1:]
         if not is_list_var(expr):
             yield kw_call
Esempio n. 14
0
 def _get_run_kw_if_calls(self, given_args):
     while "ELSE IF" in given_args:
         kw_call, given_args = self._split_run_kw_if_args(given_args, "ELSE IF", 2)
         yield kw_call
     if "ELSE" in given_args:
         kw_call, else_call = self._split_run_kw_if_args(given_args, "ELSE", 1)
         yield kw_call
         yield else_call
     elif self._validate_kw_call(given_args):
         expr, kw_call = given_args[0], given_args[1:]
         if not is_list_var(expr):
             yield kw_call
Esempio n. 15
0
 def _get_return_value(self, variables, return_):
     ret = self.return_value if not return_ else return_.return_value
     if not ret:
         return None
     contains_list_var = any(is_list_var(item) for item in ret)
     try:
         ret = variables.replace_list(ret)
     except DataError as err:
         raise VariableError('Replacing variables from keyword return value '
                             'failed: %s' % err.message)
     if len(ret) != 1 or contains_list_var:
         return ret
     return ret[0]
Esempio n. 16
0
 def _get_return_value(self, variables, return_):
     ret = self.return_value if not return_ else return_.return_value
     if not ret:
         return None
     contains_list_var = any(is_list_var(item) for item in ret)
     try:
         ret = variables.replace_list(ret)
     except DataError as err:
         raise DataError('Replacing variables from keyword return value '
                         'failed: %s' % err.message)
     if len(ret) != 1 or contains_list_var:
         return ret
     return ret[0]
 def validate_dry_run(self, arguments):
     if not any(is_list_var(arg) for arg in arguments):
         self._validate_limits(arguments, {}, self._argspec)
Esempio n. 18
0
 def _validate_kw_call(self, kw_call, min_length=2):
     if len(kw_call) >= min_length:
         return True
     return any(is_list_var(item) for item in kw_call)
Esempio n. 19
0
 def check_arg_limits_for_dry_run(self, args):
     arg_count = len(args)
     scalar_arg_count = len([a for a in args if not is_list_var(a)])
     if scalar_arg_count <= self.minargs and arg_count - scalar_arg_count:
         arg_count = self.minargs
     self._check_arg_limits(arg_count)
Esempio n. 20
0
 def test_is_list_var(self):
     for ok in LISTS:
         assert is_list_var(ok)
     for nok in SCALARS + NOKS:
         assert not is_list_var(nok)
Esempio n. 21
0
 def test_is_list_var(self):
     for ok in LISTS:
         assert is_list_var(ok)
     for nok in SCALARS + NOKS:
         assert not is_list_var(nok)
Esempio n. 22
0
 def _is_varargs(self, arg):
     return is_list_var(arg)
 def _is_varargs(self, arg):
     return is_list_var(arg)
Esempio n. 24
0
 def _validate_kw_call(self, kw_call, min_length=2):
     if len(kw_call) >= min_length:
         return True
     return any(is_list_var(item) for item in kw_call)
        if self._errors:
            raise DataError('User keyword initialization failed:\n%s'
                            % '\n'.join(self._errors))
        if not (self.keywords or self.return_value):
            raise DataError("User keyword '%s' contains no keywords"
                            % self.name)

    def _get_return_value(self, variables):
        if not self.return_value:
            return None
        try:
            ret = variables.replace_list(self.return_value)
        except DataError, err:
            raise DataError('Replacing variables from keyword return value '
                            'failed: %s' % unicode(err))
        if len(ret) != 1 or is_list_var(self.return_value[0]):
            return ret
        return ret[0]


class EmbeddedArgsTemplate(UserKeywordHandler):
    _regexp_extension = re.compile(r'(?<!\\)\(\?.+\)')
    _regexp_group_start = re.compile(r'(?<!\\)\((.*?)\)')
    _regexp_group_escape = r'(?:\1)'
    _default_pattern = '.*?'
    _variable_pattern = r'\$\{[^\}]+\}'

    def __init__(self, keyword, libname):
        if keyword.args.value:
            raise TypeError('Cannot have normal arguments')
        self.embedded_args, self.name_regexp \
Esempio n. 26
0
        return run_errors.teardown_error

    def _verify_keyword_is_valid(self):
        if self._errors:
            raise DataError("User keyword initialization failed:\n%s" % "\n".join(self._errors))
        if not (self.keywords or self.return_value):
            raise DataError("User keyword '%s' contains no keywords" % self.name)

    def _get_return_value(self, variables):
        if not self.return_value:
            return None
        try:
            ret = variables.replace_list(self.return_value)
        except DataError, err:
            raise DataError("Replacing variables from keyword return value " "failed: %s" % unicode(err))
        if len(ret) != 1 or is_list_var(self.return_value[0]):
            return ret
        return ret[0]


class EmbeddedArgsTemplate(UserKeywordHandler):
    _regexp_extension = re.compile(r"(?<!\\)\(\?.+\)")
    _regexp_group_start = re.compile(r"(?<!\\)\((.*?)\)")
    _regexp_group_escape = r"(?:\1)"
    _default_pattern = ".*?"
    _variable_pattern = r"\$\{[^\}]+\}"

    def __init__(self, keyword, libname):
        if keyword.args.value:
            raise TypeError("Cannot have normal arguments")
        self.embedded_args, self.name_regexp = self._read_embedded_args_and_regexp(keyword.name)
 def check_arg_limits_for_dry_run(self, args):
     arg_count = len(args)
     scalar_arg_count = len([a for a in args if not is_list_var(a)])
     if scalar_arg_count <= self.minargs and arg_count - scalar_arg_count:
         arg_count = self.minargs
     self._check_arg_limits(arg_count)
Esempio n. 28
0
 def validate(self, positional, named, dryrun=False):
     if dryrun and any(is_list_var(arg) for arg in positional):
         return
     self._validate_no_multiple_values(positional, named, self._argspec)
     self._validate_limits(positional, named, self._argspec)
     self._validate_no_mandatory_missing(positional, named, self._argspec)
Esempio n. 29
0
class UserKeywordHandler(object):
    type = 'user'

    def __init__(self, keyword, libname):
        self.name = keyword.name
        self.keywords = Keywords(keyword.steps)
        self.return_value = keyword.return_.value
        self._libname = libname
        self.doc = self._doc = keyword.doc.value
        self._timeout = keyword.timeout
        self._keyword_args = keyword.args.value

    @property
    def longname(self):
        return '%s.%s' % (self._libname,
                          self.name) if self._libname else self.name

    @property
    def shortdoc(self):
        return self.doc.splitlines()[0] if self.doc else ''

    def init_keyword(self, varz):
        self._errors = []
        self.doc = varz.replace_meta('Documentation', self._doc, self._errors)
        self.timeout = KeywordTimeout(self._timeout.value,
                                      self._timeout.message)
        self.timeout.replace_variables(varz)

    def run(self, context, arguments):
        context.namespace.start_user_keyword(self)
        try:
            return self._run(context, arguments)
        finally:
            context.namespace.end_user_keyword()

    def _run(self, context, argument_values):
        args_spec = UserKeywordArguments(self._keyword_args, self.longname)
        variables = context.get_current_vars()
        if context.dry_run:
            return self._dry_run(context, variables, args_spec,
                                 argument_values)
        return self._variable_resolving_run(context, variables, args_spec,
                                            argument_values)

    def _dry_run(self, context, variables, args_spec, argument_values):
        resolved_arguments = args_spec.resolve_arguments_for_dry_run(
            argument_values)
        self._execute(context, variables, args_spec, resolved_arguments)
        return None

    def _variable_resolving_run(self, context, variables, args_spec,
                                argument_values):
        resolved_arguments = args_spec.resolve(argument_values, variables)
        self._execute(context, variables, args_spec, resolved_arguments)
        return self._get_return_value(variables)

    def _execute(self, context, variables, args_spec, resolved_arguments):
        args_spec.set_variables(resolved_arguments, variables, context.output)
        self._verify_keyword_is_valid()
        self.timeout.start()
        self.keywords.run(context)

    def _verify_keyword_is_valid(self):
        if self._errors:
            raise DataError('User keyword initialization failed:\n%s' %
                            '\n'.join(self._errors))
        if not (self.keywords or self.return_value):
            raise DataError("User keyword '%s' contains no keywords" %
                            self.name)

    def _get_return_value(self, variables):
        if not self.return_value:
            return None
        try:
            ret = variables.replace_list(self.return_value)
        except DataError, err:
            raise DataError('Replacing variables from keyword return value '
                            'failed: %s' % unicode(err))
        if len(ret) != 1 or is_list_var(self.return_value[0]):
            return ret
        return ret[0]