Robot Framework Integrated Development Environment (RIDE)
javaargumentcoercer.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 java.lang import Byte, Short, Integer, Long, Boolean, Float, Double
17 
18 from robotide.lib.robot.variables import contains_var
19 from robotide.lib.robot.utils import is_string, is_list_like
20 
21 
23 
24  def __init__(self, signatures, argspec):
25  self._argspec_argspec = argspec
26  self._coercers_coercers = CoercerFinder().find_coercers(signatures)
27  self._varargs_handler_varargs_handler = VarargsHandler(argspec)
28 
29  def coerce(self, arguments, named, dryrun=False):
30  arguments = self._varargs_handler_varargs_handler.handle(arguments)
31  arguments = [c.coerce(a, dryrun)
32  for c, a in zip(self._coercers_coercers, arguments)]
33  if self._argspec_argspec.kwargs:
34  arguments.append(dict(named))
35  return arguments
36 
37 
38 class CoercerFinder():
39 
40  def find_coercers(self, signatures):
41  return [self._get_coercer_get_coercer(types, position)
42  for position, types in self._parse_types_parse_types(signatures)]
43 
44  def _parse_types(self, signatures):
45  types = {}
46  for sig in signatures:
47  for index, arg in enumerate(sig.args):
48  types.setdefault(index + 1, []).append(arg)
49  return sorted(types.items())
50 
51  def _get_coercer(self, types, position):
52  possible = [BooleanCoercer(position), IntegerCoercer(position),
53  FloatCoercer(position), NullCoercer(position)]
54  coercers = [self._get_coercer_for_type_get_coercer_for_type(t, possible) for t in types]
55  if self._coercers_conflict_coercers_conflict(*coercers):
56  return NullCoercer()
57  return coercers[0]
58 
59  def _get_coercer_for_type(self, type, coercers):
60  for coercer in coercers:
61  if coercer.handles(type):
62  return coercer
63 
64  def _coercers_conflict(self, first, *rest):
65  return not all(coercer is first for coercer in rest)
66 
67 
68 class _Coercer():
69 
72  _name = ''
73 
76  _types = []
77 
80  _primitives = []
81 
82  def __init__(self, position=None):
83  self._position_position = position
84 
85  def handles(self, type):
86  return type in self._types_types or type.__name__ in self._primitives_primitives
87 
88  def coerce(self, argument, dryrun=False):
89  if not is_string(argument) \
90  or (dryrun and contains_var(argument)):
91  return argument
92  try:
93  return self._coerce_coerce(argument)
94  except ValueError:
95  raise ValueError('Argument at position %d cannot be coerced to %s.'
96  % (self._position_position, self._name_name))
97 
98  def _coerce(self, argument):
99  raise NotImplementedError
100 
101 
103 
106  _name = 'boolean'
107 
110  _types = [Boolean]
111 
114  _primitives = ['boolean']
115 
116  def _coerce(self, argument):
117  try:
118  return {'false': False, 'true': True}[argument.lower()]
119  except KeyError:
120  raise ValueError
121 
122 
124 
127  _name = 'integer'
128 
131  _types = [Byte, Short, Integer, Long]
132 
135  _primitives = ['byte', 'short', 'int', 'long']
136 
137  def _coerce(self, argument):
138  return int(argument)
139 
140 
142 
145  _name = 'floating point number'
146 
149  _types = [Float, Double]
150 
153  _primitives = ['float', 'double']
154 
155  def _coerce(self, argument):
156  return float(argument)
157 
158 
160 
161  def handles(self, argument):
162  return True
163 
164  def _coerce(self, argument):
165  return argument
166 
167 
169 
170  def __init__(self, argspec):
171  self._index_index = argspec.minargs if argspec.varargs else -1
172 
173  def handle(self, arguments):
174  if self._index_index > -1 and not self._passing_list_passing_list(arguments):
175  arguments[self._index_index:] = [arguments[self._index_index:]]
176  return arguments
177 
178  def _passing_list(self, arguments):
179  return self._correct_count_correct_count(arguments) and is_list_like(arguments[-1])
180 
181  def _correct_count(self, arguments):
182  return len(arguments) == self._index_index + 1
def contains_var(string, identifiers='$@&')
Definition: isvar.py:46