Source code for

#  Copyright 2008-2015 Nokia Networks
#  Copyright 2016-     Robot Framework Foundation
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  See the License for the specific language governing permissions and
#  limitations under the License.

from robot.errors import DataError, VariableError
from robot.utils import DotDict, is_dict_like, is_list_like, NormalizedDict, type_name

from .notfound import variable_not_found
from .search import is_assign
from .tablesetter import VariableTableValueBase

NOT_SET = object()

[docs]class VariableStore(object): def __init__(self, variables): = NormalizedDict(ignore='_') self._variables = variables
[docs] def resolve_delayed(self, item=None): if item: return self._resolve_delayed(*item) for name, value in list( try: self._resolve_delayed(name, value) except DataError: pass
def _resolve_delayed(self, name, value): if not self._is_resolvable(value): return value try:[name] = value.resolve(self._variables) except DataError as err: # Recursive resolving may have already removed variable. if name in value.report_error(err) variable_not_found('${%s}' % name, return[name] def _is_resolvable(self, value): try: # isinstance can throw an exception in ironpython and jython return isinstance(value, VariableTableValueBase) except Exception: return False def __getitem__(self, name): if name not in variable_not_found('${%s}' % name, return self._resolve_delayed(name,[name])
[docs] def get(self, name, default=NOT_SET, decorated=True): try: if decorated: name = self._undecorate(name) return self[name] except VariableError: if default is NOT_SET: raise return default
[docs] def update(self, store):
[docs] def clear(self):
[docs] def add(self, name, value, overwrite=True, decorated=True): if decorated: name, value = self._undecorate_and_validate(name, value) if overwrite or name not in[name] = value
def _undecorate(self, name): if not is_assign(name): raise VariableError("Invalid variable name '%s'." % name) return name[2:-1] def _undecorate_and_validate(self, name, value): undecorated = self._undecorate(name) if name[0] == '@': if not is_list_like(value): self._raise_cannot_set_type(name, value, 'list') value = list(value) if name[0] == '&': if not is_dict_like(value): self._raise_cannot_set_type(name, value, 'dictionary') value = DotDict(value) return undecorated, value def _raise_cannot_set_type(self, name, value, expected): raise VariableError("Cannot set variable '%s': Expected %s-like value, got %s." % (name, expected, type_name(value))) def __len__(self): return len( def __iter__(self): return iter( def __contains__(self, name): return name in
[docs] def as_dict(self, decoration=True): if decoration: variables = (self._decorate(name, self[name]) for name in self) else: variables = return NormalizedDict(variables, ignore='_')
def _decorate(self, name, value): if is_dict_like(value): name = '&{%s}' % name elif is_list_like(value): name = '@{%s}' % name else: name = '${%s}' % name return name, value