| 1 |
from os.path import join |
| 2 |
try: |
| 3 |
from itertools import zip_longest |
| 4 |
except ImportError: |
| 5 |
# Python 2.7 support. |
| 6 |
from itertools import izip_longest as zip_longest |
| 7 |
import io |
| 8 |
|
| 9 |
import docutils |
| 10 |
from docutils.frontend import OptionParser |
| 11 |
from docutils.nodes import Text, Element, system_message |
| 12 |
from docutils.parsers.rst import Parser |
| 13 |
from docutils.utils import new_document |
| 14 |
from docutils.core import publish_from_doctree |
| 15 |
from sphinx.application import Sphinx |
| 16 |
|
| 17 |
# sphinx.util.docutils requires Sphinx 1.5 and up. |
| 18 |
try: |
| 19 |
from sphinx.util.docutils import docutils_namespace |
| 20 |
except ImportError: |
| 21 |
# Attempt to support Sphinx 1.4 and thus the old Debian Stretch (oldstable) |
| 22 |
from copy import copy |
| 23 |
from contextlib import contextmanager |
| 24 |
from docutils.parsers.rst import directives, roles |
| 25 |
@contextmanager |
| 26 |
def docutils_namespace(): |
| 27 |
"""Create namespace for reST parsers.""" |
| 28 |
try: |
| 29 |
_directives = copy(directives._directives) |
| 30 |
_roles = copy(roles._roles) |
| 31 |
yield |
| 32 |
finally: |
| 33 |
directives._directives = _directives |
| 34 |
roles._roles = _roles |
| 35 |
|
| 36 |
|
| 37 |
def build_sphinx(src_dir, output_dir, files=None, config={}): |
| 38 |
doctrees_dir = join(output_dir, '.doctrees') |
| 39 |
|
| 40 |
filenames = [] |
| 41 |
force_all = True |
| 42 |
|
| 43 |
default_config = { |
| 44 |
'extensions': ['sphinxcontrib.restbuilder'], |
| 45 |
'master_doc': 'index', |
| 46 |
} |
| 47 |
default_config.update(config) |
| 48 |
config = default_config |
| 49 |
|
| 50 |
if files: |
| 51 |
force_all = False |
| 52 |
filenames = [join(src_dir, file + '.rst') for file in files] |
| 53 |
config['master_doc'] = files[0] |
| 54 |
|
| 55 |
with docutils_namespace(): |
| 56 |
app = Sphinx( |
| 57 |
src_dir, |
| 58 |
None, |
| 59 |
output_dir, |
| 60 |
doctrees_dir, |
| 61 |
'rst', |
| 62 |
confoverrides=config, |
| 63 |
verbosity=0, |
| 64 |
) |
| 65 |
|
| 66 |
app.build(force_all=force_all, filenames=filenames) |
| 67 |
|
| 68 |
|
| 69 |
def assert_node_equal(output, expected): |
| 70 |
assert type(output) == type(expected) |
| 71 |
if isinstance(output, Text): |
| 72 |
output_text = output.replace('\r\n', ' ') |
| 73 |
output_text = output_text.replace('\n', ' ') |
| 74 |
expected_text = expected.replace('\r\n', ' ') |
| 75 |
expected_text = expected_text.replace('\n', ' ') |
| 76 |
assert output_text == expected_text |
| 77 |
elif isinstance(output, system_message): |
| 78 |
assert len(output.children) == len(expected.children) |
| 79 |
# Don't check specifics of system_messages (warnings) |
| 80 |
# E.g. the line number may be off |
| 81 |
elif isinstance(output, Element): |
| 82 |
assert len(output.children) == len(expected.children) |
| 83 |
assert output.attributes == expected.attributes |
| 84 |
else: |
| 85 |
raise AssertionError |
| 86 |
|
| 87 |
|
| 88 |
def assert_doc_equal(output_doc, expected_doc): |
| 89 |
""" |
| 90 |
Can be used to compare two documents, ignoring any whitespace changes |
| 91 |
""" |
| 92 |
for output, expected in zip_longest( |
| 93 |
output_doc.traverse(include_self=False), expected_doc.traverse(include_self=False) |
| 94 |
): |
| 95 |
assert_node_equal(output, expected) |
| 96 |
|
| 97 |
|
| 98 |
def parse_doc(dir, file): |
| 99 |
parser = Parser() |
| 100 |
with io.open(join(dir, file + '.rst'), encoding='utf-8') as fh: |
| 101 |
doc = new_document( |
| 102 |
file, |
| 103 |
OptionParser( |
| 104 |
components=(docutils.parsers.rst.Parser,) |
| 105 |
).get_default_values(), |
| 106 |
) |
| 107 |
parser.parse( |
| 108 |
fh.read(), |
| 109 |
doc, |
| 110 |
) |
| 111 |
return doc |
| 112 |
|
| 113 |
|
| 114 |
def run_parse_test(src_dir, expected_dir, output_dir, subdir, files): |
| 115 |
src_dir = join(src_dir, subdir) |
| 116 |
expected_dir = join(expected_dir, subdir) |
| 117 |
output_dir = join(output_dir, subdir) |
| 118 |
build_sphinx(src_dir, output_dir, files) |
| 119 |
|
| 120 |
for file in files: |
| 121 |
output_doc = parse_doc(output_dir, file) |
| 122 |
expected_doc = parse_doc(expected_dir, file) |
| 123 |
try: |
| 124 |
assert_doc_equal(output_doc, expected_doc) |
| 125 |
except AssertionError: |
| 126 |
# output XML version of doctree for easier debugging |
| 127 |
with open(join(output_dir, file + '.output.xml'), 'wb') as fw: |
| 128 |
fw.write(publish_from_doctree(output_doc, writer_name='xml')) |
| 129 |
with open(join(output_dir, file + '.expected.xml'), 'wb') as fw: |
| 130 |
fw.write(publish_from_doctree(expected_doc, writer_name='xml')) |
| 131 |
raise |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
if __name__ == '__main__': |
| 136 |
pass |