Robot Framework
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 import functools
17 
18 from robot.errors import DataError
19 
20 try:
21  from docutils.core import publish_doctree
22  from docutils.parsers.rst import directives
23  from docutils.parsers.rst import roles
24  from docutils.parsers.rst.directives import register_directive
25  from docutils.parsers.rst.directives.body import CodeBlock
26  from docutils.parsers.rst.directives.misc import Include
27 except ImportError:
28  raise DataError("Using reStructuredText test data requires having "
29  "'docutils' module version 0.9 or newer installed.")
30 
31 
33 
34  def __init__(self, doctree):
35  if not hasattr(doctree, '_robot_data'):
36  doctree._robot_data = []
37  self._robot_data_robot_data = doctree._robot_data
38 
39  def add_data(self, rows):
40  self._robot_data_robot_data.extend(rows)
41 
42  def get_data(self):
43  return '\n'.join(self._robot_data_robot_data)
44 
45  def has_data(self):
46  return bool(self._robot_data_robot_data)
47 
48 
49 class RobotCodeBlock(CodeBlock):
50 
51  def run(self):
52  if 'robotframework' in self.arguments:
53  store = RobotDataStorage(self.state_machine.document)
54  store.add_data(self.content)
55  return []
56 
57 
58 register_directive('code', RobotCodeBlock)
59 register_directive('code-block', RobotCodeBlock)
60 register_directive('sourcecode', RobotCodeBlock)
61 
62 
63 relevant_directives = (RobotCodeBlock, Include)
64 
65 
66 @functools.wraps(directives.directive)
67 def directive(*args, **kwargs):
68  directive_class, messages = directive.__wrapped__(*args, **kwargs)
69  if directive_class not in relevant_directives:
70  # Skipping unknown or non-relevant directive entirely
71  directive_class = (lambda *args, **kwargs: [])
72  return directive_class, messages
73 
74 
75 @functools.wraps(roles.role)
76 def role(*args, **kwargs):
77  role_function = role.__wrapped__(*args, **kwargs)
78  if role_function is None: # role is unknown, ignore
79  role_function = (lambda *args, **kwargs: [], [])
80  return role_function
81 
82 
83 directives.directive = directive
84 roles.role = role
85 
86 
87 def read_rest_data(rstfile):
88  doctree = publish_doctree(
89  rstfile.read(),
90  source_path=rstfile.name,
91  settings_overrides={
92  'input_encoding': 'UTF-8',
93  'report_level': 4
94  })
95  store = RobotDataStorage(doctree)
96  return store.get_data()
def directive(*args, **kwargs)
Definition: restreader.py:67
def role(*args, **kwargs)
Definition: restreader.py:76
def read_rest_data(rstfile)
Definition: restreader.py:87