Robot Framework Integrated Development Environment (RIDE)
datafilewriter.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 os
17 
18 from robotide.lib.robot.errors import DataError
19 from robotide.lib.robot.utils import binary_file_writer, file_writer, PY2
20 
21 from .filewriters import FileWriter
22 
23 
24 
26 
27 
30  def __init__(self, **options):
31  self._options_options = options
32 
33 
40  def write(self, datafile):
41  with WritingContext(datafile, **self._options_options) as ctx:
42  FileWriter(ctx).write(datafile)
43 
44 
45 
47  txt_format = 'txt'
48  html_format = 'html'
49  tsv_format = 'tsv'
50  robot_format = 'robot'
51  txt_column_count = 18
52  html_column_count = 5
53  tsv_column_count = 8
54 
57  _formats = [txt_format, html_format, tsv_format, robot_format]
58 
59 
87  def __init__(self, datafile, format='', output=None, pipe_separated=False,
88  txt_separating_spaces=4, line_separator='\n'):
89  self.datafiledatafile = datafile
90  self.pipe_separatedpipe_separated = pipe_separated
91  self.line_separatorline_separator = line_separator
92  self._given_output_given_output = output
93  self.formatformat = self._validate_format_validate_format(format) or self._format_from_file_format_from_file()
94  self.txt_separating_spacestxt_separating_spaces = txt_separating_spaces
95  self.outputoutput = output
96 
97  def __enter__(self):
98  if not self.outputoutput:
99  path = self._output_path_output_path()
100  if PY2 and self.formatformat == self.tsv_formattsv_format:
101  self.outputoutput = binary_file_writer(path)
102  else:
103  self.outputoutput = file_writer(path, newline=self.line_separatorline_separator)
104  return self
105 
106  def __exit__(self, *exc_info):
107  if self._given_output_given_output is None:
108  self.outputoutput.close()
109 
110  def _validate_format(self, format):
111  format = format.lower() if format else ''
112  if format and format not in self._formats_formats:
113  raise DataError('Invalid format: %s' % format)
114  return format
115 
116  def _format_from_file(self):
117  return self._format_from_extension_format_from_extension(self._source_from_file_source_from_file())
118 
119  def _format_from_extension(self, path):
120  return os.path.splitext(path)[1][1:].lower()
121 
122  def _output_path(self):
123  return '%s.%s' % (self._base_name_base_name(), self.formatformat)
124 
125  def _base_name(self):
126  return os.path.splitext(self._source_from_file_source_from_file())[0]
127 
128  def _source_from_file(self):
129  return getattr(self.datafiledatafile, 'initfile', self.datafiledatafile.source)
Used when variable does not exist.
Definition: errors.py:67
Object to write parsed test data file objects back to disk.
def __init__(self, **options)
:param **options: A :class:.WritingContext is created based on these.
def write(self, datafile)
Writes given datafile using **options.
Contains configuration used in writing a test data file to disk.
def __init__(self, datafile, format='', output=None, pipe_separated=False, txt_separating_spaces=4, line_separator='\n')
:param datafile: The datafile to be written.
def file_writer(path=None, encoding='UTF-8', newline=None)
Definition: robotio.py:21
def binary_file_writer(path=None)
Definition: robotio.py:37
def FileWriter(context)
Creates and returns a FileWriter object.
Definition: filewriters.py:35