Robot Framework Integrated Development Environment (RIDE)
tablepopulators.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 import re
17 
18 from robotide.lib.robot.utils import py2to3
19 
20 from .comments import CommentCache, Comments
21 from .settings import Documentation, MetadataList
22 
23 
24 
25 class Populator():
26 
27  def add(self, row):
28  raise NotImplementedError
29 
30  def populate(self):
31  raise NotImplementedError
32 
33 
34 @py2to3
36 
37  def add(self, row):
38  pass
39 
40  def populate(self):
41  pass
42 
43  def __nonzero__(self):
44  return False
45 
46 
48 
49  def __init__(self, table):
50  self._table_table = table
51  self._populator_populator = NullPopulator()
52  # DEBUG: Not using comments self._comment_cache = CommentCache()
53 
54  def add(self, row):
55  # DEBUG: Not using comments
56  # if self._is_cacheable_comment_row(row):
57  # self._comment_cache.add(row)
58  # else:
59  self._add_add(row)
60 
61  def _is_cacheable_comment_row(self, row):
62  return row.is_commented()
63 
64  def _add(self, row):
65  # DEBUG: Not using comments
66  # if self._is_continuing(row):
67  # self._consume_comments()
68  # else:
69  if not self._is_continuing_is_continuing(row):
70  self._populator_populator.populate()
71  self._populator_populator = self._get_populator_get_populator(row)
72  # DEBUG: Not using comments self._consume_standalone_comments()
73  self._populator_populator.add(row)
74 
75  def _is_continuing(self, row):
76  return row.is_continuing() and self._populator_populator
77 
78  def _get_populator(self, row):
79  raise NotImplementedError
80 
81  # DEBUG: Not using comments
82  """
83  def _consume_comments(self):
84  self._comment_cache.consume_with(self._populator.add)
85 
86  def _consume_standalone_comments(self):
87  self._consume_comments()
88  """
89 
90  def populate(self):
91  # DEBUG: Not using comments self._consume_comments()
92  self._populator_populator.populate()
93 
94 
96 
97  def _get_populator(self, row):
98  setter = self._table_table.get_setter(row.head) if row.head else None
99  if not setter:
100  return NullPopulator()
101  if isinstance(setter.__self__, Documentation):
102  return DocumentationPopulator(setter)
103  if isinstance(setter.__self__, MetadataList):
104  return MetadataPopulator(setter)
105  return SettingPopulator(setter)
106 
107 
109 
110  def _get_populator(self, row):
111  return VariablePopulator(self._table_table.add, row.head)
112 
114  self._comment_cache.consume_with(self._populate_standalone_comment_populate_standalone_comment)
115 
116  def _populate_standalone_comment(self, comment):
117  populator = self._get_populator_get_populator_get_populator(comment)
118  populator.add(comment)
119  populator.populate()
120 
121  def populate(self):
122  self._populator_populator.populate()
123  # DEBUG Not using comments self._consume_standalone_comments()
124 
125 
127 
128  def _is_continuing(self, row):
129  return row.is_indented() and self._populator_populator or row.is_commented()
130 
132  return row.is_commented() and not self._populator_populator
133 
134 
136 
137  def _get_populator(self, row):
138  return TestCasePopulator(self._table_table.add)
139 
140 
142 
143  def _get_populator(self, row):
144  return UserKeywordPopulator(self._table_table.add)
145 
146 
148 
149  def __init__(self, for_loop_creator):
150  self._for_loop_creator_for_loop_creator = for_loop_creator
151  self._loop_loop = None
152  self._populator_populator = NullPopulator()
153  self._declaration_declaration = []
154  self._declaration_comments_declaration_comments = []
155 
156  def add(self, row):
157  # dedented_row = row.dedent() # DEBUG remove dedent
158  # print(f"DEBUG: Forloop Add row = {row.data}\ndentented={dedented_row}")
159  if not self._loop_loop:
160  declaration_ready = self._populate_declaration_populate_declaration(row)
161  if not declaration_ready:
162  return
163  self._create_for_loop_create_for_loop()
164  if not row.is_continuing():
165  self._populator_populator.populate()
166  self._populator_populator = StepPopulator(self._loop_loop.add_step)
167  # self._populator.add(dedented_row) #DEBUG remove dedent
168  self._populator_populator.add(row)
169 
170  def _populate_declaration(self, row):
171  if row.starts_for_loop() or row.is_continuing():
172  self._declaration_declaration.extend(row.dedent().data) #DEBUG remove dedent
173  # self._declaration.extend(row.data)
174  self._declaration_comments_declaration_comments.extend(row.comments)
175  return False
176  return True
177 
178  def _create_for_loop(self):
179  self._loop_loop = self._for_loop_creator_for_loop_creator(self._declaration_declaration,
180  self._declaration_comments_declaration_comments)
181 
182  def populate(self):
183  if not self._loop_loop:
184  self._create_for_loop_create_for_loop()
185  self._populator_populator.populate()
186 
187 
189 
190  def __init__(self, test_or_uk_creator):
191  self._test_or_uk_creator_test_or_uk_creator = test_or_uk_creator
192  self._test_or_uk_test_or_uk = None
193  self._populator_populator = NullPopulator()
194  # DEBUG: Not using comments self._comment_cache = CommentCache()
195 
196  def add(self, row):
197  # if row:
198  # print(f"DEBUG: _TestCaseUserKeywordPopulator enter row {row.cells} {row.comments}")
199  # DEBUG: Not using comments
200  # if row.is_commented():
201  # self._comment_cache.add(row)
202  # # print(f"DEBUG: _TestCaseUserKeywordPopulator returning from comment {row.cells} {row.comments}")
203  # return
204  if not self._test_or_uk_test_or_uk:
205  self._test_or_uk_test_or_uk = self._test_or_uk_creator_test_or_uk_creator(row.head)
206  dedented_row = row.dedent()
207  if dedented_row:
208  # print(f"DEBUG: _TestCaseUserKeywordPopulator processing after dedented {dedented_row.cells} {dedented_row.comments}")
209  self._handle_data_row_handle_data_row(dedented_row)
210 
211  def _handle_data_row(self, row):
212  ending_for_loop = False
213  if not self._continues_continues(row):
214  self._populator_populator.populate()
215  if row.all == ['END']:
216  ending_for_loop = self._end_for_loop_end_for_loop()
217  self._populator_populator = self._get_populator_get_populator(row)
218  # DEBUG: Not using comments self._comment_cache.consume_with(self._populate_comment_row)
219  # DEBUG: Not using comments else:
220  # DEBUG: Not using comments self._comment_cache.consume_with(self._populator.add)
221  # if not ending_for_loop: # END was being omitted
222  # print("DEBUG: handle_data_row %s" % row.all)
223  # print("DEBUG: handle_data_row %s" % row.all)
224  self._populator_populator.add(row)
225 
226  def _end_for_loop(self):
227  if self._populating_for_loop_populating_for_loop():
228  return True
229  return self._test_or_uk_test_or_uk.end_for_loop()
230 
232  return isinstance(self._populator_populator, ForLoopPopulator)
233 
234  def _continues(self, row):
235  return (row.is_continuing() and self._populator_populator or
236  self._populating_for_loop_populating_for_loop() and row.is_indented())
237 
238  def _populate_comment_row(self, crow):
239  # print("DEBUG: _populate_comment_row ENTER %s" % crow)
240  populator = StepPopulator(self._test_or_uk_test_or_uk.add_step)
241  populator.add(crow)
242  populator.populate()
243 
244  def populate(self):
245  self._populator_populator.populate()
246  # self._comment_cache.consume_with(self._populate_comment_row)
247 
248  def _get_populator(self, row):
249  if row.starts_test_or_user_keyword_setting():
250  setter = self._setting_setter_setting_setter(row)
251  if not setter:
252  return NullPopulator()
253  if isinstance(setter.__self__, Documentation):
254  return DocumentationPopulator(setter)
255  return SettingPopulator(setter)
256  # REMOVE SPECIAL FOR PARSING
257  # if row.starts_for_loop():
258  # return ForLoopPopulator(self._test_or_uk.add_for_loop)
259  return StepPopulator(self._test_or_uk_test_or_uk.add_step)
260 
261  def _setting_setter(self, row):
262  setting_name = row.test_or_user_keyword_setting_name()
263  return self._test_or_uk_test_or_uk.get_setter(setting_name)
264 
265 
267 
270  _item_type = 'test case'
271 
272 
274 
277  _item_type = 'keyword'
278 
279 
281 
282  def __init__(self, setter):
283  self._setter_setter = setter
284  self._value_value = []
285  self._comments_comments = Comments()
286  self._data_added_data_added = False
287 
288  def add(self, row):
289  # print(f"DEBUG: tablepopulators add ENTER row={row}")
290  # self._add(row)
291  if not row.is_commented():
292  self._add_add(row)
293  self._comments_comments.add(row)
294 
295  def _add(self, row):
296  if row.cells == ['...']:
297  self._deprecate_continuation_without_values_deprecate_continuation_without_values()
298  self._value_value.extend(row.tail if not self._data_added_data_added else row.data)
299  self._data_added_data_added = True
300 
302  location = self._get_deprecation_location_get_deprecation_location()
303  message = ("%sIgnoring lines with only continuation marker '...' is "
304  "deprecated." % ('In %s: ' % location if location else ''))
305  self._setter_setter.__self__.report_invalid_syntax(message, level='WARN')
306 
308  return ''
309 
310 
312 
313  def __init__(self, setter, name):
314  _PropertyPopulator.__init__(self, setter)
315  self._name_name = name
316 
317  def populate(self):
318  self._setter_setter(self._name_name, self._value_value, self._comments_comments.value)
319 
321  return "'Variables' section"
322 
323 
325 
326  def populate(self):
327  self._setter_setter(self._value_value, self._comments_comments.value)
328 
330  return "'%s' setting" % self._setter_setter.__self__.setting_name
331 
332 
334 
337  _end_of_line_escapes = re.compile(r'(\\+)n?$')
338 
339  def populate(self):
340  self._setter_setter(self._value_value, self._comments_comments.value)
341 
342  def _add(self, row):
343  self._add_to_value_add_to_value(row.dedent().data)
344 
345  def _add_to_value(self, data):
346  joiner = self._row_joiner_row_joiner()
347  if joiner:
348  self._value_value.append(joiner)
349  self._value_value.append(' '.join(data))
350 
351  def _row_joiner(self):
352  if self._is_empty_is_empty():
353  return None
354  return self._joiner_based_on_eol_escapes_joiner_based_on_eol_escapes()
355 
356  def _is_empty(self):
357  return not self._value_value or \
358  (len(self._value_value) == 1 and self._value_value[0] == '')
359 
361  match = self._end_of_line_escapes_end_of_line_escapes.search(self._value_value[-1])
362  if not match or len(match.group(1)) % 2 == 0:
363  return '\\n'
364  if not match.group(0).endswith('n'):
365  return ' '
366  return None
367 
368 
370 
371  def __init__(self, setter):
372  _PropertyPopulator.__init__(self, setter)
373  self._name_name = None
374 
375  def populate(self):
376  self._setter_setter(self._name_name, self._value_value, self._comments_comments.value)
377 
378  def _add(self, row):
379  data = row.dedent().data
380  if self._name_name is None:
381  self._name_name = data[0] if data else ''
382  data = data[1:]
383  self._add_to_value_add_to_value(data)
384 
385 
387 
388  def _add(self, row):
389  if row.cells == ['...']:
390  self._deprecate_continuation_without_values_deprecate_continuation_without_values()
391  self._value_value.extend(row.data)
392 
393  def populate(self):
394  if self._value_value or self._comments_comments.value:
395  self._setter_setter(self._value_value, self._comments_comments.value)
Explicit interface for all populators.