Robot Framework Integrated Development Environment (RIDE)
filewriters.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 try:
17  import csv
18 except ImportError:
19  # csv module is missing from IronPython < 2.7.1
20  csv = None
21 
22 from robotide.lib.robot.utils import HtmlWriter, PY2
23 
24 from .formatters import TsvFormatter, TxtFormatter, PipeFormatter
25 from .htmlformatter import HtmlFormatter
26 from .htmltemplate import TEMPLATE_START, TEMPLATE_END
27 
28 
29 
35 def FileWriter(context):
36  if context.format == context.html_format:
37  return HtmlFileWriter(context)
38  if context.format == context.tsv_format:
39  return TsvFileWriter(context)
40  if context.pipe_separated:
41  return PipeSeparatedTxtWriter(context)
42  return SpaceSeparatedTxtWriter(context)
43 
44 
46 
47  def __init__(self, formatter, configuration):
48  self._formatter_formatter = formatter
49  self._output_output = configuration.output
50 
51  def write(self, datafile):
52  tables = [table for table in datafile if table]
53  if datafile.has_preamble:
54  self._write_preamble_write_preamble(datafile.preamble)
55  for table in tables:
56  self._write_table_write_table(table, is_last=table is tables[-1])
57 
58  def _write_table(self, table, is_last):
59  self._write_header_write_header(table)
60  self._write_rows_write_rows(self._formatter_formatter.format_table(table))
61  # if not is_last:
62  # print(f"DEBUG: writing table {self._formatter.format_header(table)}")
63  # self._write_empty_row(table)
64 
65  def _write_header(self, table):
66  self._write_row_write_row(self._formatter_formatter.format_header(table))
67 
68  def _write_rows(self, rows):
69  for row in rows:
70  self._write_row_write_row(row)
71 
72  def _write_empty_row(self, table):
73  self._write_row_write_row(self._formatter_formatter.empty_row_after(table))
74 
75  def _write_row(self, row):
76  raise NotImplementedError
77 
78  def _write_preamble(self, rows):
79  for line in rows:
80  self._output_output.write(line)
81 
82 
84 
85  def __init__(self, configuration):
86  formatter = TxtFormatter(configuration.txt_column_count)
87  self._separator_separator = ' ' * configuration.txt_separating_spaces
88  _DataFileWriter.__init__(self, formatter, configuration)
89 
90  def _write_row(self, row):
91  line = self._separator_separator.join(row).rstrip() + '\n'
92  self._output_output.write(line)
93 
94 
96 
99  _separator = ' | '
100 
101  def __init__(self, configuration):
102  formatter = PipeFormatter(configuration.txt_column_count)
103  _DataFileWriter.__init__(self, formatter, configuration)
104 
105  def _write_row(self, row):
106  row = self._separator_separator.join(row)
107  if row:
108  row = '| ' + row + ' |'
109  self._output_output.write(row + '\n')
110 
111 
113 
114  def __init__(self, configuration):
115  if not csv:
116  raise RuntimeError('No csv module found. '
117  'Writing tab separated format is not possible.')
118  formatter = TsvFormatter(configuration.tsv_column_count)
119  _DataFileWriter.__init__(self, formatter, configuration)
120  self._writer_writer = self._get_writer_get_writer(configuration)
121 
122  def _get_writer(self, configuration):
123  # Custom dialect needed as a workaround for
124  # http://ironpython.codeplex.com/workitem/33627
125  dialect = csv.excel_tab()
126  dialect.lineterminator = configuration.line_separator if PY2 else '\n'
127  return csv.writer(configuration.output, dialect=dialect)
128 
129  def _write_row(self, row):
130  if PY2:
131  row = [c.encode('UTF-8') for c in row]
132  self._writer_writer.writerow(row)
133 
134 
136 
137  def __init__(self, configuration):
138  formatter = HtmlFormatter(configuration.html_column_count)
139  _DataFileWriter.__init__(self, formatter, configuration)
140  self._name_name = configuration.datafile.name
141  self._writer_writer = HtmlWriter(configuration.output)
142 
143  def write(self, datafile):
144  self._writer_writer.content(TEMPLATE_START % {'NAME': self._name_name}, escape=False)
145  _DataFileWriter.write(self, datafile)
146  self._writer_writer.content(TEMPLATE_END, escape=False)
147 
148  def _write_table(self, table, is_last):
149  self._writer_writer.start('table', {'id': table.type.replace(' ', ''),
150  'border': '1'})
151  _DataFileWriter._write_table(self, table, is_last)
152  self._writer_writer.end('table')
153 
154  def _write_row(self, row):
155  self._writer_writer.start('tr')
156  for cell in row:
157  self._writer_writer.element(cell.tag, cell.content, cell.attributes,
158  escape=False)
159  self._writer_writer.end('tr')
def __init__(self, formatter, configuration)
Definition: filewriters.py:47
def FileWriter(context)
Creates and returns a FileWriter object.
Definition: filewriters.py:35