forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestreader.py
More file actions
96 lines (72 loc) · 2.89 KB
/
restreader.py
File metadata and controls
96 lines (72 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
from robot.errors import DataError
try:
from docutils.core import publish_doctree
from docutils.parsers.rst import directives
from docutils.parsers.rst import roles
from docutils.parsers.rst.directives import register_directive
from docutils.parsers.rst.directives.body import CodeBlock
from docutils.parsers.rst.directives.misc import Include
except ImportError:
raise DataError("Using reStructuredText test data requires having "
"'docutils' module version 0.9 or newer installed.")
class RobotDataStorage:
def __init__(self, doctree):
if not hasattr(doctree, '_robot_data'):
doctree._robot_data = []
self._robot_data = doctree._robot_data
def add_data(self, rows):
self._robot_data.extend(rows)
def get_data(self):
return '\n'.join(self._robot_data)
def has_data(self):
return bool(self._robot_data)
class RobotCodeBlock(CodeBlock):
def run(self):
if 'robotframework' in self.arguments:
store = RobotDataStorage(self.state_machine.document)
store.add_data(self.content)
return []
register_directive('code', RobotCodeBlock)
register_directive('code-block', RobotCodeBlock)
register_directive('sourcecode', RobotCodeBlock)
relevant_directives = (RobotCodeBlock, Include)
@functools.wraps(directives.directive)
def directive(*args, **kwargs):
directive_class, messages = directive.__wrapped__(*args, **kwargs)
if directive_class not in relevant_directives:
# Skipping unknown or non-relevant directive entirely
directive_class = (lambda *args, **kwargs: [])
return directive_class, messages
@functools.wraps(roles.role)
def role(*args, **kwargs):
role_function = role.__wrapped__(*args, **kwargs)
if role_function is None: # role is unknown, ignore
role_function = (lambda *args, **kwargs: [], [])
return role_function
directives.directive = directive
roles.role = role
def read_rest_data(rstfile):
doctree = publish_doctree(
rstfile.read(),
source_path=rstfile.name,
settings_overrides={
'input_encoding': 'UTF-8',
'report_level': 4
})
store = RobotDataStorage(doctree)
return store.get_data()