-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path__init__.py
More file actions
167 lines (130 loc) · 4.31 KB
/
Copy path__init__.py
File metadata and controls
167 lines (130 loc) · 4.31 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import os.path
import shutil
import tempfile
from docutils.parsers.rst import directives
from docutils.parsers.rst import roles
from sphinx.application import Sphinx
from functools import update_wrapper
# pylint:disable=protected-access,too-few-public-methods
# useless-object-inheritance is version specific
# pylint:disable=bad-option-value,useless-object-inheritance
class Lazy(object):
def __init__(self, func, name=None):
if name is None:
name = func.__name__
self.data = (func, name)
update_wrapper(self, func)
def __get__(self, inst, class_):
if inst is None:
return self
func, name = self.data
value = func(inst)
inst.__dict__[name] = value
inst.addCleanup(delattr, inst, name)
return value
#: conf.py for tests
CONF_PY = """\
extensions = ['sphinxcontrib.programoutput']
source_suffix = '.rst'
master_doc = 'index'
project = u'epydoc-test'
copyright = u'2011, foo'
version = '1'
release = '1'
exclude_patterns = []
pygments_style = 'sphinx'
html_theme = 'default'
"""
class AppMixin(object):
#: The contents of the main 'doc.rst' document.
#:
#: This will be written as a bytestring to the document, allowing for
#: the document to be in an arbitrary encoding.
#:
#: If this object is not a bytestring, it will first be encoded using
#: the encoding named in `self.document_encoding`.
document_content = '=============\ndummy content\n=============\n'
document_encoding = 'utf-8'
def setUp(self):
# Avoid "WARNING: while setting up extension
# sphinxcontrib.programoutput: directive u'program-output' is
# already registered, it will be overridden".
# This may only be needed for Sphinx 1.
self.directives = directives._directives.copy()
# Likewise for 'eq'
self.roles = roles._roles.copy()
def tearDown(self):
directives._directives = self.directives
roles._roles = self.roles
@Lazy
def tmpdir(self):
d = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, d)
return d
@Lazy
def srcdir(self):
"""
Generated source directory for test Sphinx application.
"""
tmpdir = self.tmpdir
srcdir = os.path.join(tmpdir, 'src')
os.mkdir(srcdir)
confpy = os.path.join(srcdir, 'conf.py')
with open(confpy, 'w', encoding='utf-8') as f:
f.write(CONF_PY)
index_document = os.path.join(srcdir, 'index.rst')
with open(index_document, 'w', encoding='utf-8') as f:
f.write("""\
.. toctree::
content/doc""")
content_directory = os.path.join(srcdir, 'content')
os.mkdir(content_directory)
content_document = os.path.join(content_directory, 'doc.rst')
contents = self.document_content
if not isinstance(contents, bytes):
contents = contents.encode(self.document_encoding)
with open(content_document, 'wb') as f:
f.write(b"=====\n")
f.write(b"Title\n")
f.write(b"=====\n\n")
f.write(contents)
return srcdir
@Lazy
def outdir(self):
return os.path.join(self.tmpdir, 'html')
@Lazy
def doctreedir(self):
return os.path.join(self.tmpdir, 'doctrees')
@Lazy
def confoverrides(self):
return {}
@Lazy
def app(self):
"""
Sphinx application for the current test.
"""
srcdir = self.srcdir
outdir = self.outdir
doctreedir = self.doctreedir
confoverrides = self.confoverrides
warningiserror = not self.ignore_warnings
app = Sphinx(str(srcdir), str(srcdir), str(outdir), str(doctreedir), 'html',
status=None, warning=None, freshenv=None,
warningiserror=warningiserror, confoverrides=confoverrides)
if self.build_app:
app.build()
return app
@Lazy
def build_app(self): # pylint:disable=method-hidden
return False
@Lazy
def ignore_warnings(self):
return True
@Lazy
def doctree(self):
getattr(self, 'build_app')
self.build_app = True
app = self.app
return app.env.get_doctree('content/doc')
assert isinstance(AppMixin.app, Lazy) # coverage