Robot Framework Integrated Development Environment (RIDE)
isvar.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.errors import DataError
17 from robotide.lib.robot.utils import is_string
18 
19 from .splitter import VariableIterator
20 
21 
22 def is_var(string, identifiers='$@&'):
23  if not string or not is_string(string) or len(string.strip('${}')) < 1:
24  return False
25  if string[0] not in identifiers or (string[0] != '$' and string[1] != '{' and string[-1] != '}'):
26  return False
27  body = string[2:-1]
28  # print(f"DEBUG: is_var body={body}")
29  if string[0] == '$' and string[1] == '{' and string[-1] == '}':
30  return True
31  return '{' not in body and '}' not in body and body == string.strip(f"{identifiers}"+'{}')
32 
33 
34 def is_scalar_var(string):
35  return is_var(string, identifiers='$')
36 
37 
38 def is_list_var(string):
39  return is_var(string, identifiers='@')
40 
41 
42 def is_dict_var(string):
43  return is_var(string, identifiers='&')
44 
45 
46 def contains_var(string, identifiers='$@&'):
47  return (is_string(string) and
48  any(i in string for i in identifiers) and
49  '{' in string and '}' in string and
50  bool(VariableIterator(string, identifiers)))
51 
52 
53 def validate_var(string, identifiers='$@&'):
54  if not is_var(string, identifiers):
55  raise DataError("Invalid variable name '%s'." % string)
Used when variable does not exist.
Definition: errors.py:67
def validate_var(string, identifiers='$@&')
Definition: isvar.py:53
def is_var(string, identifiers='$@&')
Definition: isvar.py:22
def contains_var(string, identifiers='$@&')
Definition: isvar.py:46