Robot Framework
typevalidator.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 from robot.utils import (is_dict_like, is_list_like, plural_or_not as s,
18  seq2str, type_name)
19 
20 
22 
23 
24  def __init__(self, argspec):
25  self._argspec_argspec = argspec
26 
27  def validate(self, types):
28  if types is None:
29  return None
30  if not types:
31  return {}
32  if is_dict_like(types):
33  return self.validate_type_dictvalidate_type_dict(types)
34  if is_list_like(types):
35  return self.convert_type_list_to_dictconvert_type_list_to_dict(types)
36  raise DataError('Type information must be given as a dictionary or '
37  'a list, got %s.' % type_name(types))
38 
39  def validate_type_dict(self, types):
40  # 'return' isn't used for anything yet but it may be shown by Libdoc
41  # in the future. Trying to be forward compatible.
42  names = set(self._argspec_argspec.argument_names + ['return'])
43  extra = [t for t in types if t not in names]
44  if extra:
45  raise DataError('Type information given to non-existing '
46  'argument%s %s.'
47  % (s(extra), seq2str(sorted(extra))))
48  return types
49 
50  def convert_type_list_to_dict(self, types):
51  names = self._argspec_argspec.argument_names
52  if len(types) > len(names):
53  raise DataError('Type information given to %d argument%s but '
54  'keyword has only %d argument%s.'
55  % (len(types), s(types), len(names), s(names)))
56  return {name: value for name, value in zip(names, types) if value}
def __init__(self, argspec)
:type argspec: :py:class:robot.running.arguments.ArgumentSpec
def seq2str(sequence, quote="'", sep=', ', lastsep=' and ')
Returns sequence in format ‘'item 1’, 'item 2' and 'item 3'`.
Definition: misc.py:79
def is_dict_like(item)
Definition: robottypes.py:72
def type_name(item, capitalize=False)
Return "non-technical" type name for objects and types.
Definition: robottypes.py:86
def is_list_like(item)
Definition: robottypes.py:66