Robot Framework Integrated Development Environment (RIDE)
store.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, VariableError
17 from robotide.lib.robot.utils import (DotDict, is_dict_like, is_list_like, NormalizedDict,
18  type_name)
19 
20 from .isvar import validate_var
21 from .notfound import variable_not_found
22 from .tablesetter import VariableTableValueBase
23 
24 
25 class VariableStore():
26 
27  def __init__(self, variables):
28  self.datadata = NormalizedDict(ignore='_')
29  self._variables_variables = variables
30 
31  def resolve_delayed(self):
32  for name, value in list(self.datadata.items()):
33  try:
34  self._resolve_delayed_resolve_delayed(name, value)
35  except DataError:
36  pass
37 
38  def _resolve_delayed(self, name, value):
39  if not self._is_resolvable_is_resolvable(value):
40  return value
41  try:
42  self.datadata[name] = value.resolve(self._variables_variables)
43  except DataError as err:
44  # Recursive resolving may have already removed variable.
45  if name in self:
46  self.removeremove(name)
47  value.report_error(err)
48  variable_not_found('${%s}' % name, self.datadata,
49  "Variable '${%s}' not found." % name)
50  return self.datadata[name]
51 
52  def _is_resolvable(self, value):
53  try: # isinstance can throw an exception in ironpython and jython
54  return isinstance(value, VariableTableValueBase)
55  except Exception:
56  return False
57 
58  def __getitem__(self, name):
59  return self._resolve_delayed_resolve_delayed(name, self.datadata[name])
60 
61  def update(self, store):
62  self.datadata.update(store.data)
63 
64  def clear(self):
65  self.datadata.clear()
66 
67  def add(self, name, value, overwrite=True, decorated=True):
68  if decorated:
69  name, value = self._undecorate_undecorate(name, value)
70  if overwrite or name not in self.datadata:
71  self.datadata[name] = value
72 
73  def _undecorate(self, name, value):
74  validate_var(name)
75  if name[0] == '@':
76  if not is_list_like(value):
77  self._raise_cannot_set_type_raise_cannot_set_type(name, value, 'list')
78  value = list(value)
79  if name[0] == '&':
80  if not is_dict_like(value):
81  self._raise_cannot_set_type_raise_cannot_set_type(name, value, 'dictionary')
82  value = DotDict(value)
83  return name[2:-1], value
84 
85  def _raise_cannot_set_type(self, name, value, expected):
86  raise VariableError("Cannot set variable '%s': Expected %s-like value, "
87  "got %s." % (name, expected, type_name(value)))
88 
89  def remove(self, name):
90  if name in self.datadata:
91  self.datadata.pop(name)
92 
93  def __len__(self):
94  return len(self.datadata)
95 
96  def __iter__(self):
97  return iter(self.datadata)
98 
99  def __contains__(self, name):
100  return name in self.datadata
101 
102  def as_dict(self, decoration=True):
103  if decoration:
104  variables = (self._decorate_decorate(name, self[name]) for name in self)
105  else:
106  variables = self.datadata
107  return NormalizedDict(variables, ignore='_')
108 
109  def _decorate(self, name, value):
110  if is_dict_like(value):
111  name = '&{%s}' % name
112  elif is_list_like(value):
113  name = '@{%s}' % name
114  else:
115  name = '${%s}' % name
116  return name, value
Used when no keyword is found or there is more than one match.
Definition: errors.py:75
Custom dictionary implementation automatically normalizing keys.
Definition: normalizing.py:58
def _resolve_delayed(self, name, value)
Definition: store.py:38
def _raise_cannot_set_type(self, name, value, expected)
Definition: store.py:85
def add(self, name, value, overwrite=True, decorated=True)
Definition: store.py:67
def as_dict(self, decoration=True)
Definition: store.py:102
def validate_var(string, identifiers='$@&')
Definition: isvar.py:53
def variable_not_found(name, candidates, msg=None, deco_braces=True)
Raise DataError for missing variable name.
Definition: notfound.py:27