Robot Framework Integrated Development Environment (RIDE)
argumentconverter.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 robotide.lib.robot.utils import is_unicode
17 from robotide.lib.robot.variables import contains_var
18 
19 from .typeconverters import TypeConverter
20 
21 
23 
24 
25  def __init__(self, argspec, dry_run=False):
26  self._argspec_argspec = argspec
27  self._dry_run_dry_run = dry_run
28 
29  def convert(self, positional, named):
30  return self._convert_positional_convert_positional(positional), self._convert_named_convert_named(named)
31 
32  def _convert_positional(self, positional):
33  names = self._argspec_argspec.positional
34  converted = [self._convert_convert(name, value)
35  for name, value in zip(names, positional)]
36  if self._argspec_argspec.varargs:
37  converted.extend(self._convert_convert(self._argspec_argspec.varargs, value)
38  for value in positional[len(names):])
39  return converted
40 
41  def _convert_named(self, named):
42  names = set(self._argspec_argspec.positional) | set(self._argspec_argspec.kwonlyargs)
43  kwargs = self._argspec_argspec.kwargs
44  return [(name, self._convert_convert(name if name in names else kwargs, value))
45  for name, value in named]
46 
47  def _convert(self, name, value):
48  type_, explicit_type = self._get_type_get_type(name, value)
49  if type_ is None or (self._dry_run_dry_run and
50  contains_var(value, identifiers='$@&%')):
51  return value
52  converter = TypeConverter.converter_for(type_)
53  if converter is None:
54  return value
55  return converter.convert(name, value, explicit_type)
56 
57  def _get_type(self, name, value):
58  if self._argspec_argspec.types is None or not is_unicode(value):
59  return None, None
60  if name in self._argspec_argspec.types:
61  return self._argspec_argspec.types[name], True
62  if name in self._argspec_argspec.defaults:
63  return type(self._argspec_argspec.defaults[name]), False
64  return None, None
def __init__(self, argspec, dry_run=False)
:type argspec: :py:class:robot.running.arguments.ArgumentSpec
def contains_var(string, identifiers='$@&')
Definition: isvar.py:46