Robot Framework Integrated Development Environment (RIDE)
datarow.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.output import LOGGER
17 from robotide.lib.robot.utils import py2to3
18 
19 
20 @py2to3
21 class DataRow():
22 
25  _row_continuation_marker = '...'
26 
27  def __init__(self, cells, source=None):
28  self.sourcesource = source
29  if cells:
30  self.cellscells, self.commentscomments = self._parse_parse(cells)
31  # print(f"DEBUG: DataRow init cells and comments cells={self.cells} + {self.comments} \n")
32  else:
33  self.cellscells = []
34  self.commentscomments = []
35 
36  def _parse(self, row):
37  data = []
38  comments = []
39  # print(f"DEBUG: datarow enter _parse row={row}\n")
40  for cell in row:
41  # print(f"DEBUG: datarow before clean cell={cell}")
42  cell = self._collapse_whitespace_collapse_whitespace(cell)
43  # print(f"DEBUG: datarow after clean cell={cell}")
44  if cell and cell[0] == '#' or comments:
45  comments.append(cell)
46  else:
47  data.append(cell)
48  # if self._row_continuation_marker in data and self.source:
49  # self._deprecate_escaped_cells_before_continuation(data)
50  # return self._purge_empty_cells(data), comments # DEBUG don't self._purge_empty_cells(comments)
51  return data, comments # DEBUG keep empty cells
52 
53  def _collapse_whitespace(self, cell):
54  if cell.startswith('#'):
55  return cell
56  return ' '.join(cell.split())
57 
59  index = data.index(self._row_continuation_marker_row_continuation_marker)
60  if any(cell == '\\' for cell in data[:index]):
61  LOGGER.warn("Error in file '%s': Escaping empty cells with "
62  "'\\' before line continuation marker '...' is "
63  "deprecated. Remove escaping before Robot "
64  "Framework 3.2." % self.sourcesource)
65 
66  """ DEBUG: This function is not used
67  def _purge_empty_cells(self, row):
68  # DEBUG Lets not remove empty cells
69  # while row and not row[-1]:
70  # row.pop()
71  # Cells with only a single backslash are considered empty
72  return [cell if cell != '\\' else '' for cell in row]
73  """
74 
75  @property
76  first_non_empty_cell = property
77 
79  # print(f"DEBUG: datarow enter _first_non_empty_cell")
80  # if self.cells:
81  # print(f"DEBUG: datarow _first_non_empty_cell: {self.cells[:]}")
82  index = 0
83  while index < len(self.cellscells) and self.cellscells[index] == '':
84  index += 1
85  # print(f"DEBUG: datarow RETURNING _first_non_empty_cell index ={index}")
86  return index # if index < len(self.cells) else index - 1
87 
88  @property
89  head = property
90 
91  def head(self):
92  # print(f"DEBUG: datarow head={self.cells[:] if self.cells else 'NONE!!!'}")
93  # return self.cells[self.first_non_empty_cell] if self.cells else ''
94  return self.cellscells[0] if self.cellscells else ''
95 
96  @property
97  tail = property
98 
99  def tail(self):
100  # print(f"DEBUG: datarow tail={self.cells[self.first_non_empty_cell:]}")
101  # We want to keep indentation, so we only remove first empty cell
102  # index = 1 if len(self.cells) > 1 else 0
103  # return self.cells[self.first_non_empty_cell:]
104  return self.cellscells[1:]
105 
106  @property
107  all = property
108 
109  def all(self):
110  return self.cellscells
111 
112  @property
113  data = property
114 
115  def data(self):
116  if self.is_continuingis_continuing():
117  index = self.cellscells.index(self._row_continuation_marker_row_continuation_marker) + 1
118  return self.cellscells[index:]
119  return self.cellscells
120 
121  def dedent(self):
122  # DEBUG: this is used only for debugging: import inspect
123  datarow = DataRow([])
124  datarow.cells = self.tailtailtail
125  datarow.comments = self.commentscomments
126  # stack = inspect.stack()
127  # the_class = stack[1][0].f_locals["self"].__class__.__name__
128  # the_method = stack[1][0].f_code.co_name
129  # print("DEBUG: datarow dedent called by {}.{}()".format(the_class, the_method))
130  # print(f"DEBUG: datarow dedent={datarow.all[:]}")
131  return datarow
132 
133  def starts_for_loop(self):
134  # head = self.head
135  head = self.cellscells[self.first_non_empty_cellfirst_non_empty_cellfirst_non_empty_cell]
136  if not self.headheadhead:
137  self.__setattr__(self.headheadhead, head)
138  # print(f"DEBUG: datarow starts_for_loop NEW CALCULATION head={head}")
139  # else:
140  # head = self.head
141  # print(f"DEBUG: datarow starts_for_loop head={head}")
142  if head.startswith(':'):
143  return head.replace(':', '').replace(' ', '').upper() == 'FOR'
144  return head == 'FOR'
145 
147  head = self.headheadhead
148  # print(f"DEBUG: datarow CALLING starts_test_or_user_keyword_setting head={head}")
149  return head and head[0] == '[' and head[-1] == ']'
150 
152  return self.headheadhead[1:-1].strip()
153 
154  def is_indented(self):
155  return self.headheadhead == ''
156  # return self.first_non_empty_cell > 0
157 
158  def is_continuing(self):
159  for cell in self.cellscells:
160  if cell == self._row_continuation_marker_row_continuation_marker:
161  return True
162  if cell:
163  return False
164 
165  def is_commented(self):
166  return bool(not self.cellscells and self.commentscomments)
167 
168  def __nonzero__(self):
169  return bool(self.cellscells or self.commentscomments)
def __init__(self, cells, source=None)
Definition: datarow.py:27
def _deprecate_escaped_cells_before_continuation(self, data)
Definition: datarow.py:58