Robot Framework Integrated Development Environment (RIDE)
rowsplitter.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 itertools
17 
18 
19 class RowSplitter():
20 
23  _comment_mark = '#'
24 
27  _empty_cell_escape = '' # '${EMPTY}'
28 
31  _line_continuation = '...'
32 
35  _setting_table = 'setting'
36 
39  _indented_tables = ('test case', 'keyword')
40 
43  _split_from = ('ELSE', 'ELSE IF', 'AND')
44 
45  def __init__(self, cols=8, split_multiline_doc=False):
46  self._cols_cols = cols
47  self._split_multiline_doc_split_multiline_doc = split_multiline_doc
48 
49  def split(self, row, table_type):
50  if not row:
51  return self._split_empty_row_split_empty_row()
52  indent = self._get_indent_get_indent(row, table_type)
53  if self._split_multiline_doc_split_multiline_doc and self._is_doc_row_is_doc_row(row, table_type):
54  return self._split_doc_row_split_doc_row(row, indent)
55  return self._split_row_split_row(row, indent)
56 
57  def _split_empty_row(self):
58  yield []
59 
60  def _get_indent(self, row, table_type):
61  indent = self._get_first_non_empty_index_get_first_non_empty_index(row)
62  min_indent = 1 if table_type in self._indented_tables_indented_tables else 0
63  return max(indent, min_indent)
64 
65  def _get_first_non_empty_index(self, row, indented=False):
66  ignore = ['', '...'] if indented else ['']
67  return len(list(itertools.takewhile(lambda x: x in ignore, row)))
68 
69  def _is_doc_row(self, row, table_type):
70  if table_type == self._setting_table_setting_table:
71  return len(row) > 1 and row[0] == 'Documentation'
72  if table_type in self._indented_tables_indented_tables:
73  return len(row) > 2 and row[1] == '[Documentation]'
74  return False
75 
76  def _split_doc_row(self, row, indent):
77  first, rest = self._split_doc_split_doc(row[indent+1])
78  yield row[:indent+1] + [first] + row[indent+2:]
79  while rest:
80  current, rest = self._split_doc_split_doc(rest)
81  row = [current] if current else []
82  yield self._continue_row_continue_row(row, indent)
83 
84  def _split_doc(self, doc):
85  if '\\n' not in doc:
86  return doc, ''
87  if '\\n ' in doc:
88  doc = doc.replace('\\n ', '\\n')
89  return doc.split('\\n', 1)
90 
91  def _split_row(self, row, indent):
92  while row:
93  current, row = self._split_split(row)
94  yield self._escape_last_cell_if_empty_escape_last_cell_if_empty(current)
95  if row:
96  row = self._continue_row_continue_row(row, indent)
97 
98  def _split(self, data):
99  index = min(self._get_possible_split_indices_get_possible_split_indices(data))
100  current, rest = data[:index], data[index:]
101  rest = self._comment_rest_if_needed_comment_rest_if_needed(current, rest)
102  return current, rest
103 
105  min_index = self._get_first_non_empty_index_get_first_non_empty_index(data, indented=True) + 1
106  for marker in self._split_from_split_from:
107  if marker in data[min_index:]:
108  yield data[min_index:].index(marker) + min_index
109  yield self._cols_cols
110 
111  def _comment_rest_if_needed(self, current, rest):
112  if rest and any(c.startswith(self._comment_mark_comment_mark) for c in current) \
113  and not rest[0].startswith(self._comment_mark_comment_mark):
114  rest = [self._comment_mark_comment_mark + ' ' + rest[0]] + rest[1:]
115  return rest
116 
118  if row and not row[-1].strip():
119  row = row[:-1] + [self._empty_cell_escape_empty_cell_escape]
120  return row
121 
122  def _continue_row(self, row, indent):
123  row = [self._line_continuation_line_continuation] + row
124  if indent + 1 < self._cols_cols:
125  row = [''] * indent + row
126  return row
def _get_first_non_empty_index(self, row, indented=False)
Definition: rowsplitter.py:65
def __init__(self, cols=8, split_multiline_doc=False)
Definition: rowsplitter.py:45