Robot Framework Integrated Development Environment (RIDE)
formatters.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 .aligners import FirstColumnAligner, ColumnAligner, NullAligner
19 from .dataextractor import DataExtractor
20 from .rowsplitter import RowSplitter
21 
22 
24 
27  _whitespace = re.compile('\s{2,}')
28 
31  _split_multiline_doc = True
32 
33  def __init__(self, column_count):
34  self._splitter_splitter = RowSplitter(column_count, self._split_multiline_doc_split_multiline_doc)
35  self._column_count_column_count = column_count
36  self._extractor_extractor = DataExtractor(self._want_names_on_first_content_row_want_names_on_first_content_row)
37 
38  def _want_names_on_first_content_row(self, table, name):
39  return True
40 
41  def empty_row_after(self, table):
42  return self._format_row_format_row([], table)
43 
44  def format_header(self, table):
45  header = self._format_row_format_row(table.header)
46  return self._format_header_format_header(header, table)
47 
48  def format_table(self, table):
49  rows = self._extractor_extractor.rows_from_table(table)
50  if self._should_split_rows_should_split_rows(table):
51  rows = self._split_rows_split_rows(rows, table)
52  return (self._format_row_format_row(r, table) for r in rows)
53 
54  def _should_split_rows(self, table):
55  return not self._should_align_columns_should_align_columns(table)
56 
57  def _split_rows(self, original_rows, table):
58  for original in original_rows:
59  for split in self._splitter_splitter.split(original, table.type):
60  yield split
61 
62  def _should_align_columns(self, table):
63  return self._is_indented_table_is_indented_table(table) and bool(table.header[1:])
64 
65  def _is_indented_table(self, table):
66  return table is not None and table.type in ['test case', 'keyword']
67 
69  return [self._whitespace_whitespace.sub(self._whitespace_escaper_whitespace_escaper,
70  cell.replace('\n', ' ')) for cell in row]
71 
72  def _whitespace_escaper(self, match):
73  return '\\'.join(match.group(0))
74 
75  def _format_row(self, row, table=None):
76  raise NotImplementedError
77 
78  def _format_header(self, header, table):
79  raise NotImplementedError
80 
81 
83 
84  def _format_header(self, header, table):
85  return [self._format_header_cell_format_header_cell(cell) for cell in header]
86 
87  def _format_header_cell(self, cell):
88  return '*%s*' % cell if cell else ''
89 
90  def _format_row(self, row, table=None):
91  return self._pad_pad(self._escape_escape(row))
92 
93  def _escape(self, row):
94  return self._escape_consecutive_whitespace_escape_consecutive_whitespace(self._escape_tabs_escape_tabs(row))
95 
96  def _escape_tabs(self, row):
97  return [c.replace('\t', '\\t') for c in row]
98 
99  def _pad(self, row):
100  row = [cell.replace('\n', ' ') for cell in row]
101  return row + [''] * (self._column_count_column_count - len(row))
102 
103 
105 
108  _test_or_keyword_name_width = 18
109 
112  _setting_and_variable_name_width = 14
113 
114  def _format_row(self, row, table=None):
115  row = self._escape_escape(row)
116  aligner = self._aligner_for_aligner_for(table)
117  return aligner.align_row(row)
118 
119  def _aligner_for(self, table):
120  if table and table.type in ['setting', 'variable']:
121  return FirstColumnAligner(self._setting_and_variable_name_width_setting_and_variable_name_width)
122  if self._should_align_columns_should_align_columns(table):
123  return ColumnAligner(self._test_or_keyword_name_width_test_or_keyword_name_width, table)
124  return NullAligner()
125 
126  def _format_header(self, header, table):
127  header = ['*** %s ***' % header[0]] + header[1:]
128  aligner = self._aligner_for_aligner_for(table)
129  return aligner.align_row(header)
130 
131  def _want_names_on_first_content_row(self, table, name):
132  return self._should_align_columns_should_align_columns(table) and \
133  len(name) <= self._test_or_keyword_name_width_test_or_keyword_name_width
134 
135  def _escape(self, row):
136  if not row:
137  return row
138  return list(self._escape_cells_escape_cells(self._escape_consecutive_whitespace_escape_consecutive_whitespace(row)))
139 
140  def _escape_cells(self, row):
141  escape = False
142  for cell in row:
143  if cell:
144  escape = True
145  elif escape:
146  cell = '\\'
147  yield cell
148 
149 
151 
152  def _escape_cells(self, row):
153  return [self._escape_empty_escape_empty(self._escape_pipes_escape_pipes(cell)) for cell in row]
154 
155  def _escape_empty(self, cell):
156  return cell or ' '
157 
158  def _escape_pipes(self, cell):
159  if ' | ' in cell:
160  cell = cell.replace(' | ', ' \\| ')
161  if cell.startswith('| '):
162  cell = '\\' + cell
163  if cell.endswith(' |'):
164  cell = cell[:-1] + '\\|'
165  return cell
Transforms table of a parsed test data file into a list of rows.
def _want_names_on_first_content_row(self, table, name)
Definition: formatters.py:131