Robot Framework Integrated Development Environment (RIDE)
settings.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.utils import is_string, py2to3, unicode
17 
18 from .comments import Comment
19 
20 
21 @py2to3
22 class Setting():
23 
24  def __init__(self, setting_name, parent=None, comment=None):
25  self.setting_namesetting_name = setting_name
26  self.parentparent = parent
27  self._set_initial_value_set_initial_value()
28  self._set_comment_set_comment(comment)
29  self._populated_populated = False
30 
31  def _set_initial_value(self):
32  self.valuevalue = []
33 
34  def _set_comment(self, comment):
35  self.commentcomment = Comment(comment)
36 
37  def reset(self):
38  self.__init____init__(self.setting_namesetting_name, self.parentparent)
39 
40  @property
41  source = property
42 
43  def source(self):
44  return self.parentparent.source if self.parentparent is not None else None
45 
46  @property
47  directory = property
48 
49  def directory(self):
50  return self.parentparent.directory if self.parentparent is not None else None
51 
52 
53  def populate(self, value, comment=None):
54  if not self._populated_populated:
55  self._populate_populate(value)
56  self._set_comment_set_comment(comment)
57  self._populated_populated = True
58  else:
59  self._set_initial_value_set_initial_value()
60  self._set_comment_set_comment(None)
61  self.report_invalid_syntaxreport_invalid_syntax("Setting '%s' used multiple times."
62  % self.setting_namesetting_name, 'ERROR')
63 
64  def _populate(self, value):
65  self.valuevalue = value
66 
67  def is_set(self):
68  return bool(self.valuevalue)
69 
70  def is_for_loop(self):
71  return False
72 
73  def report_invalid_syntax(self, message, level='ERROR'):
74  self.parentparent.report_invalid_syntax(message, level)
75 
76  def _string_value(self, value):
77  return value if is_string(value) else ' '.join(value)
78 
79  def _concat_string_with_value(self, string, value):
80  if string:
81  return string + ' ' + self._string_value_string_value(value)
82  return self._string_value_string_value(value)
83 
84  def as_list(self):
85  return self._data_as_list_data_as_list() + self.commentcomment.as_list()
86 
87  def _data_as_list(self):
88  ret = [self.setting_namesetting_name]
89  if self.valuevalue:
90  ret.extend(self.valuevalue)
91  return ret
92 
93  def __nonzero__(self):
94  return self.is_setis_set()
95 
96  def __iter__(self):
97  return iter(self.valuevalue or ())
98 
99  def __unicode__(self):
100  return unicode(self.valuevalue or '')
101 
102 
104 
105  def __init__(self, separator):
106  self._separator_separator = separator
107 
108  def join_string_with_value(self, string, value):
109  if string:
110  return string + self._separator_separator + self.string_valuestring_value(value)
111  return self.string_valuestring_value(value)
112 
113  def string_value(self, value):
114  if is_string(value):
115  return value
116  return self._separator_separator.join(value)
117 
118 
120 
122  self.valuevaluevalue = ''
123 
124  def _populate(self, value):
125  self.valuevaluevalue = self._concat_string_with_value_concat_string_with_value(self.valuevaluevalue, value)
126 
127  def _string_value(self, value):
128  return value if is_string(value) else ''.join(value)
129 
130  def _data_as_list(self):
131  return [self.setting_namesetting_name, self.valuevaluevalue]
132 
133 
135 
137  self.valuevaluevalue = None
138 
139  def _populate(self, value):
140  self.valuevaluevalue = self._concat_string_with_value_concat_string_with_value(self.valuevaluevalue, value)
141 
142  def is_set(self):
143  return self.valuevaluevalue is not None
144 
145  def is_active(self):
146  return self.valuevaluevalue and self.valuevaluevalue.upper() != 'NONE'
147 
148  def _data_as_list(self):
149  ret = [self.setting_namesetting_name]
150  if self.valuevaluevalue:
151  ret.append(self.valuevaluevalue)
152  return ret
153 
154 
156 
157  # `keyword`, `is_comment` and `assign` make the API compatible with Step.
158 
159  @property
160  keyword = property
161 
162  def keyword(self):
163  return self.namename or ''
164 
165  def is_comment(self):
166  return False
167 
169  self.namename = None
170  self.argsargs = []
171  self.assignassign = ()
172 
173  def _populate(self, value):
174  if not self.namename:
175  self.namename = value[0] if value else ''
176  value = value[1:]
177  self.argsargs.extend(value)
178 
179  def is_set(self):
180  return self.namename is not None
181 
182  def is_active(self):
183  return self.namename and self.namename.upper() != 'NONE'
184 
185  def _data_as_list(self):
186  ret = [self.setting_namesetting_name]
187  if self.namename or self.argsargs:
188  ret.append(self.namename or '')
189  if self.argsargs:
190  ret.extend(self.argsargs)
191  return ret
192 
193 
195 
197  self.valuevaluevalue = None
198  self.messagemessage = ''
199 
200  def _populate(self, value):
201  if not self.valuevaluevalue:
202  self.valuevaluevalue = value[0] if value else ''
203  value = value[1:]
204  self.messagemessage = self._concat_string_with_value_concat_string_with_value(self.messagemessage, value)
205  # TODO: Remove custom timeout message support in RF 3.2.
206  if value and self.parentparent:
207  self.parentparent.report_invalid_syntax(
208  'Using custom timeout messages is deprecated since Robot '
209  'Framework 3.0.1 and will be removed in future versions. '
210  "Message that was used is '%s'." % self.messagemessage, level='WARN')
211 
212  def is_set(self):
213  return self.valuevaluevalue is not None
214 
215  def _data_as_list(self):
216  ret = [self.setting_namesetting_name]
217  if self.valuevaluevalue or self.messagemessage:
218  ret.append(self.valuevaluevalue or '')
219  if self.messagemessage:
220  ret.append(self.messagemessage)
221  return ret
222 
223 
224 class Tags(Setting):
225 
227  self.valuevaluevalue = None
228 
229  def _populate(self, value):
230  self.valuevaluevalue = (self.valuevaluevalue or []) + value
231 
232  def is_set(self):
233  return self.valuevaluevalue is not None
234 
235  def __add__(self, other):
236  if not isinstance(other, Tags):
237  raise TypeError('Tags can only be added with tags')
238  tags = Tags('Tags')
239  tags.value = (self.valuevaluevalue or []) + (other.value or [])
240  return tags
241 
242 
244  pass
245 
246 
247 class Return(Setting):
248  pass
249 
250 
252  setting_name = 'Metadata'
253 
254  def __init__(self, parent, name, value, comment=None, joined=False):
255  self.parentparentparent = parent
256  self.namename = name
257  joiner = StringValueJoiner('' if joined else ' ')
258  self.valuevaluevalue = joiner.join_string_with_value('', value)
259  self._set_comment_set_comment(comment)
260 
261  def reset(self):
262  pass
263 
264  def is_set(self):
265  return True
266 
267  def _data_as_list(self):
268  return [self.setting_namesetting_namesetting_name, self.namename, self.valuevaluevalue]
269 
270 
272 
273  def __init__(self, parent, name, args=None, alias=None, comment=None):
274  self.parentparentparent = parent
275  self.namename = name
276  self.argsargs = args or []
277  self.aliasalias = alias
278  self._set_comment_set_comment(comment)
279 
280  def reset(self):
281  pass
282 
283  @property
284  type = property
285 
286  def type(self):
287  return type(self).__name__
288 
289  def is_set(self):
290  return True
291 
292  def _data_as_list(self):
293  return [self.typetypetype, self.namename] + self.argsargs
294 
295  def report_invalid_syntax(self, message, level='ERROR', parent=None):
296  parent = parent or getattr(self, 'parent', None)
297  if parent:
298  parent.report_invalid_syntax(message, level)
299  else:
300  from robotide.lib.robot.api import logger
301  logger.write(message, level)
302 
303 
305 
306  def __init__(self, parent, name, args=None, alias=None, comment=None):
307  if args and not alias:
308  args, alias = self._split_possible_alias_split_possible_alias(args)
309  _Import.__init__(self, parent, name, args, alias, comment)
310 
311  def _split_possible_alias(self, args):
312  if len(args) > 1 and args[-2] == 'WITH NAME':
313  return args[:-2], args[-1]
314  return args, None
315 
316  def _data_as_list(self):
317  data = ['Library', self.namename] + self.argsargs
318  if self.aliasalias:
319  data += ['WITH NAME', self.aliasalias]
320  return data
321 
322 
324 
325  def __init__(self, parent, name, invalid_args=None, comment=None):
326  if invalid_args:
327  name += ' ' + ' '.join(invalid_args)
328  _Import.__init__(self, parent, name, comment=comment)
329 
330 
332 
333  def __init__(self, parent, name, args=None, comment=None):
334  _Import.__init__(self, parent, name, args, comment=comment)
335 
336 
337 class _DataList():
338 
339  def __init__(self, parent):
340  self._parent_parent = parent
341  self.datadata = []
342 
343  def add(self, meta):
344  self._add_add(meta)
345 
346  def _add(self, meta):
347  self.datadata.append(meta)
348 
349  def _parse_name_and_value(self, value):
350  name = value[0] if value else ''
351  return name, value[1:]
352 
353  def __getitem__(self, index):
354  return self.datadata[index]
355 
356  def __setitem__(self, index, item):
357  self.datadata[index] = item
358 
359  def __len__(self):
360  return len(self.datadata)
361 
362  def __iter__(self):
363  return iter(self.datadata)
364 
365 
367 
368  def populate_library(self, data, comment):
369  self._populate_populate(Library, data, comment)
370 
371  def populate_resource(self, data, comment):
372  self._populate_populate(Resource, data, comment)
373 
374  def populate_variables(self, data, comment):
375  self._populate_populate(Variables, data, comment)
376 
377  def _populate(self, item_class, data, comment):
378  name, value = self._parse_name_and_value_parse_name_and_value(data)
379  self._add_add(item_class(self._parent_parent, name, value, comment=comment))
380 
381 
383 
384  def populate(self, name, value, comment):
385  self._add_add(Metadata(self._parent_parent, name, value, comment, joined=True))
def populate_resource(self, data, comment)
Definition: settings.py:371
def populate_variables(self, data, comment)
Definition: settings.py:374
def populate_library(self, data, comment)
Definition: settings.py:368
def _populate(self, item_class, data, comment)
Definition: settings.py:377
def __init__(self, parent, name, args=None, alias=None, comment=None)
Definition: settings.py:306
def populate(self, name, value, comment)
Definition: settings.py:384
def __init__(self, parent, name, value, comment=None, joined=False)
Definition: settings.py:254
def __init__(self, parent, name, invalid_args=None, comment=None)
Definition: settings.py:325
def report_invalid_syntax(self, message, level='ERROR')
Definition: settings.py:73
def populate(self, value, comment=None)
Mainly used at parsing time, later attributes can be set directly.
Definition: settings.py:53
def __init__(self, setting_name, parent=None, comment=None)
Definition: settings.py:24
def _concat_string_with_value(self, string, value)
Definition: settings.py:79
def __init__(self, parent, name, args=None, comment=None)
Definition: settings.py:333
def __init__(self, parent, name, args=None, alias=None, comment=None)
Definition: settings.py:273
def report_invalid_syntax(self, message, level='ERROR', parent=None)
Definition: settings.py:295
unicode
Exceptions and return codes used internally.
Definition: errors.py:24