Robot Framework Integrated Development Environment (RIDE)
tablesetter.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 contextlib import contextmanager
17 
18 from robotide.lib.robot.errors import DataError
19 from robotide.lib.robot.utils import DotDict, is_string, split_from_equals, unic
20 
21 from .isvar import validate_var
22 from .splitter import VariableSplitter
23 
24 
26 
27  def __init__(self, store):
28  self._store_store = store
29 
30  def set(self, variables, overwrite=False):
31  for name, value in VariableTableReader().read(variables):
32  self._store_store.add(name, value, overwrite, decorated=False)
33 
34 
36 
37  def read(self, variables):
38  for var in variables:
39  if not var:
40  continue
41  try:
42  yield self._get_name_and_value_get_name_and_value(var.name, var.value,
43  var.report_invalid_syntax)
44  except DataError as err:
45  var.report_invalid_syntax(err)
46 
47  def _get_name_and_value(self, name, value, error_reporter):
48  return name[2:-1], VariableTableValue(value, name, error_reporter)
49 
50 
51 def VariableTableValue(value, name, error_reporter=None):
52  validate_var(name)
53  VariableTableValue = {'$': ScalarVariableTableValue,
54  '@': ListVariableTableValue,
55  '&': DictVariableTableValue}[name[0]]
56  return VariableTableValue(value, error_reporter)
57 
58 
60 
61  def __init__(self, values, error_reporter=None):
62  self._values_values = self._format_values_format_values(values)
63  self._error_reporter_error_reporter = error_reporter
64  self._resolving_resolving = False
65 
66  def _format_values(self, values):
67  return values
68 
69  def resolve(self, variables):
70  with self._avoid_recursion_avoid_recursion_avoid_recursion:
71  return self._replace_variables_replace_variables(self._values_values, variables)
72 
73  @property
74  @contextmanager
75  _avoid_recursion = property
76 
77  def _avoid_recursion(self):
78  if self._resolving_resolving:
79  raise DataError('Recursive variable definition.')
80  self._resolving_resolving = True
81  try:
82  yield
83  finally:
84  self._resolving_resolving = False
85 
86  def _replace_variables(self, value, variables):
87  raise NotImplementedError
88 
89  def report_error(self, error):
90  if self._error_reporter_error_reporter:
91  self._error_reporter_error_reporter(unic(error))
92 
93 
95 
96  def _format_values(self, values):
97  separator = None
98  if is_string(values):
99  values = [values]
100  elif values and values[0].startswith('SEPARATOR='):
101  separator = values[0][10:]
102  values = values[1:]
103  return separator, values
104 
105  def _replace_variables(self, values, variables):
106  separator, values = values
107  # Avoid converting single value to string.
108  if self._is_single_value_is_single_value(separator, values):
109  return variables.replace_scalar(values[0])
110  if separator is None:
111  separator = ' '
112  separator = variables.replace_string(separator)
113  values = variables.replace_list(values)
114  return separator.join(unic(item) for item in values)
115 
116  def _is_single_value(self, separator, values):
117  return (separator is None and len(values) == 1 and
118  not VariableSplitter(values[0]).is_list_variable())
119 
120 
122 
123  def _replace_variables(self, values, variables):
124  return variables.replace_list(values)
125 
126 
128 
129  def _format_values(self, values):
130  return list(self._yield_formatted_yield_formatted(values))
131 
132  def _yield_formatted(self, values):
133  for item in values:
135  yield item
136  else:
137  name, value = split_from_equals(item)
138  if value is None:
139  raise DataError("Dictionary item '%s' does not contain "
140  "'=' separator." % item)
141  yield name, value
142 
143  def _replace_variables(self, values, variables):
144  try:
145  return DotDict(self._yield_replaced_yield_replaced(values,
146  variables.replace_scalar))
147  except TypeError as err:
148  raise DataError('Creating dictionary failed: %s' % err)
149 
150  def _yield_replaced(self, values, replace_scalar):
151  for item in values:
152  if isinstance(item, tuple):
153  key, values = item
154  yield replace_scalar(key), replace_scalar(values)
155  else:
156  for key, values in replace_scalar(item).items():
157  yield key, values
Used when variable does not exist.
Definition: errors.py:67
def _get_name_and_value(self, name, value, error_reporter)
Definition: tablesetter.py:47
def validate_var(string, identifiers='$@&')
Definition: isvar.py:53
def VariableTableValue(value, name, error_reporter=None)
Definition: tablesetter.py:51