Robot Framework Integrated Development Environment (RIDE)
restreader.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 io import BytesIO
17 
18 from .htmlreader import HtmlReader
19 from .robotreader import RobotReader
20 
21 
22 def RestReader():
23  from .restsupport import (publish_doctree, publish_from_doctree,
24  RobotDataStorage)
25 
26  class RestReader():
27 
28  def read(self, rstfile, rawdata):
29  doctree = publish_doctree(
30  rstfile.read(), source_path=rstfile.name,
31  settings_overrides={
32  'input_encoding': 'UTF-8',
33  'report_level': 4
34  })
35  store = RobotDataStorage(doctree)
36  if store.has_data():
37  return self._read_text(store.get_data(), rawdata, rstfile.name)
38  return self._read_html(doctree, rawdata, rstfile.name)
39 
40  def _read_text(self, data, rawdata, path):
41  robotfile = BytesIO(data.encode('UTF-8'))
42  return RobotReader().read(robotfile, rawdata, path)
43 
44  def _read_html(self, doctree, rawdata, path):
45  htmlfile = BytesIO()
46  htmlfile.write(publish_from_doctree(
47  doctree, writer_name='html',
48  settings_overrides={'output_encoding': 'UTF-8'}))
49  htmlfile.seek(0)
50  return HtmlReader().read(htmlfile, rawdata, path)
51 
52  return RestReader()