18 from robot.utils import (DotDict, escape, get_error_message, is_dict_like, is_list_like,
19 is_string, safe_str, type_name, unescape)
21 from .finders
import VariableFinder
22 from .search
import VariableMatch, search_variable
41 def replace_list(self, items, replace_until=None, ignore_errors=False):
42 items = list(items
or [])
43 if replace_until
is not None:
45 return list(self.
_replace_list_replace_list(items, ignore_errors))
52 while len(replaced) < replace_until
and items:
53 replaced.extend(self.
_replace_list_replace_list([items.pop(0)], ignore_errors))
54 if len(replaced) > replace_until:
55 replaced[replace_until:] = [
escape(item)
56 for item
in replaced[replace_until:]]
57 return replaced + items
80 match = self.
_search_variable_search_variable(item, ignore_errors=ignore_errors)
86 if isinstance(item, VariableMatch):
91 if not match.is_variable():
92 return self.
replace_stringreplace_string(match, ignore_errors=ignore_errors)
100 unescaper = custom_unescaper
or unescape
101 match = self.
_search_variable_search_variable(item, ignore_errors=ignore_errors)
103 return safe_str(unescaper(match.string))
104 return self.
_replace_string_replace_string(match, unescaper, ignore_errors)
110 unescaper(match.before),
114 parts.append(unescaper(match.string))
115 return ''.join(parts)
118 match.resolve_base(self, ignore_errors)
120 if match.identifier ==
'*':
121 logger.warn(
r"Syntax '%s' is reserved for future use. Please "
122 r"escape it like '\%s'." % (match, match))
125 value = self.
_finder_finder.find(match)
130 except VariableError:
136 if not ignore_errors:
143 for item
in match.items:
146 elif hasattr(value,
'__getitem__'):
150 "Variable '%s' is %s, which is not subscriptable, and "
151 "thus accessing item '%s' from it is not possible. To use "
152 "'[%s]' as a literal value, it needs to be escaped like "
153 "'\\[%s]'." % (name,
type_name(value), item, item, item)
155 name =
'%s[%s]' % (name, item)
164 return variable[index]
167 "To use '[%s]' as a literal value, it needs "
168 "to be escaped like '\\[%s]'."
169 % (
type_name(variable, capitalize=
True), name,
170 index, index, index))
175 return variable[index]
178 % (
type_name(variable, capitalize=
True), name, index))
181 if isinstance(index, (int, slice)):
187 if index.count(
':') > 2:
189 return slice(*[int(i)
if i
else None for i
in index.split(
':')])
198 except TypeError
as err:
199 raise VariableError(
"Dictionary '%s' used with invalid key: %s"
203 if match.identifier ==
'@':
206 "list-like." % match)
208 if match.identifier ==
'&':
210 raise VariableError(
"Value of variable '%s' is not dictionary "
211 "or dictionary-like." % match)
Used when variable does not exist.
def _validate_value(self, match, value)
def _parse_sequence_variable_index(self, index)
def _replace_list_item(self, item, ignore_errors)
def _get_variable_value(self, match, ignore_errors)
def replace_string(self, item, custom_unescaper=None, ignore_errors=False)
Replaces variables from a string.
def _get_variable_item(self, match, value)
def _get_sequence_variable_item(self, name, variable, index)
def replace_list(self, items, replace_until=None, ignore_errors=False)
Replaces variables from a list of items.
def _replace_list_until(self, items, replace_until, ignore_errors)
def _get_dict_variable_item(self, name, variable, key)
def _replace_list(self, items, ignore_errors)
def __init__(self, variable_store)
def _search_variable(self, item, ignore_errors)
def _replace_scalar(self, match, ignore_errors=False)
def _replace_string(self, match, unescaper, ignore_errors)
def replace_scalar(self, item, ignore_errors=False)
Replaces variables from a scalar item.
def get_error_message()
Returns error message of the last occurred exception.
def type_name(item, capitalize=False)
Return "non-technical" type name for objects and types.
def search_variable(string, identifiers='$@&% *', ignore_errors=False)