Robot Framework
argumentmapper.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from robot.errors import DataError
17 
18 
20 
21 
22  def __init__(self, argspec):
23  self._argspec_argspec = argspec
24 
25  def map(self, positional, named, replace_defaults=True):
26  template = KeywordCallTemplate(self._argspec_argspec)
27  template.fill_positional(positional)
28  template.fill_named(named)
29  if replace_defaults:
30  template.replace_defaults()
31  return template.args, template.kwargs
32 
33 
35 
36 
37  def __init__(self, argspec):
38  self._argspec_argspec = argspec
39  self.argsargs = [None if arg not in argspec.defaults
40  else DefaultValue(argspec.defaults[arg])
41  for arg in argspec.positional]
42  self.kwargskwargs = []
43 
44  def fill_positional(self, positional):
45  self.argsargs[:len(positional)] = positional
46 
47  def fill_named(self, named):
48  spec = self._argspec_argspec
49  for name, value in named:
50  if name in spec.positional_or_named:
51  index = spec.positional_or_named.index(name)
52  self.argsargs[index] = value
53  elif spec.var_named or name in spec.named_only:
54  self.kwargskwargs.append((name, value))
55  else:
56  raise DataError("Non-existing named argument '%s'." % name)
57  named_names = {name for name, _ in named}
58  for name in spec.named_only:
59  if name not in named_names:
60  value = DefaultValue(spec.defaults[name])
61  self.kwargskwargs.append((name, value))
62 
63  def replace_defaults(self):
64  is_default = lambda arg: isinstance(arg, DefaultValue)
65  while self.argsargs and is_default(self.argsargs[-1]):
66  self.argsargs.pop()
67  self.argsargs = [a if not is_default(a) else a.value for a in self.argsargs]
68  self.kwargskwargs = [(n, v) for n, v in self.kwargskwargs if not is_default(v)]
69 
70 
72 
73  def __init__(self, value):
74  self.valuevalue = value
75 
76  def resolve(self, variables):
77  try:
78  return variables.replace_scalar(self.valuevalue)
79  except DataError as err:
80  raise DataError('Resolving argument default values failed: %s'
81  % err.message)
def __init__(self, argspec)
:type argspec: :py:class:robot.running.arguments.ArgumentSpec
def map(self, positional, named, replace_defaults=True)
def __init__(self, argspec)
:type argspec: :py:class:robot.running.arguments.ArgumentSpec