Robot Framework Integrated Development Environment (RIDE)
variablematcher.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 import re
17 
18 from .. import utils
19 
20 
23 _VAR_BODY = r'([^\}]|\\\})*'
24 
27 _SCALAR_VARIABLE_MATCHER = re.compile(r'\$\{'+_VAR_BODY+'\}')
28 
31 _SCALAR_VARIABLE_LINE_MATCHER = re.compile(r'^(\$\{'+_VAR_BODY+'\}) *=?$')
32 
35 _LIST_VARIABLE_MATCHER = re.compile(r'^(@\{'+_VAR_BODY+'\})( ?=?|\[\d*\])$')
36 
39 _DICT_VARIABLE_MATCHER = \
40  re.compile(r'^(&\{'+_VAR_BODY+'\})( ?=?|\[[a-zA-Z_]*\])$')
41 
44 _LIST_VARIABLE_SUBITEM_END_MATCHER = re.compile(r'\[\d+\]$')
45 
48 _DICT_VARIABLE_SUBITEM_END_MATCHER = re.compile(r'\[[a-zA-Z_]+\]$')
49 
50 
51 def is_variable(value):
52  return is_scalar_variable(value) or is_list_variable(value) or \
53  is_dict_variable(value)
54 
55 
56 def is_scalar_variable(value):
57  return _SCALAR_VARIABLE_LINE_MATCHER.match(value.strip())
58 
59 
60 def is_list_variable(value):
61  return _LIST_VARIABLE_MATCHER.match(value.strip())
62 
63 
64 def is_dict_variable(value):
65  return _DICT_VARIABLE_MATCHER.match(value.strip())
66 
67 
69  return is_list_variable(value) and \
70  _LIST_VARIABLE_SUBITEM_END_MATCHER.search(value)
71 
72 
73 def is_dict_var_access(value):
74  return is_dict_variable(value) and \
75  _DICT_VARIABLE_SUBITEM_END_MATCHER.search(value)
76 
77 
78 
81 def get_variable(value):
82  match = is_variable(value)
83  return match.groups()[0] if match else None
84 
85 
87  "Return variable without extended variable syntax part"
88  if is_list_variable(value) or is_dict_variable(value):
89  return get_variable(value)
90  match = re.match('\${(.+?)[^\s\w-]+.*?}?', value)
91  if not match:
92  return None
93  return '${%s}' % (match.groups()[0].strip())
94 
95 
97  return [get_variable_basename(var)
98  for var in re.findall('[@$&]{.*?}', value)]
99 
100 
102  return bool(_SCALAR_VARIABLE_MATCHER.findall(value))
103 
104 
105 def value_contains_variable(value, varname):
106  return utils.Matcher("*%s*" % varname).match(value)
107 
108 
109 def find_unique(seq):
110  seen = set()
111  seen_add = seen.add
112  return [x for x in seq if not (x in seen or seen_add(x))]
def get_variable(value)
Returns variables name without equal sign '=' and indexing '[2]' or None.
def value_contains_variable(value, varname)