Skip to content

Commit 4db8723

Browse files
committed
Refactoring from feedback. Add tox support
1 parent 6d64ddb commit 4db8723

3 files changed

Lines changed: 57 additions & 25 deletions

File tree

scripts/generate_json_docs.py

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
import os
2020
import sys
2121
import types
22-
2322
from shutil import copyfile
23+
2424
from parinx.parser import parse_docstring
2525
import pdoc
26+
2627
from verify_included_modules import get_public_modules
2728

2829

@@ -41,9 +42,13 @@ def from_module_name(cls, name):
4142
for c in module.classes():
4243
methods.extend(c.doc.values())
4344

45+
mod = __import__(name)
46+
examples = json.dumps(get_module_examples(mod))
47+
4448
return cls(module_id=name,
4549
name=name.split('.')[-1].title(),
4650
description=module.docstring,
51+
examples=examples,
4752
methods=[Method.from_pdoc(m) for m in methods])
4853

4954
def to_dict(self):
@@ -103,17 +108,19 @@ def from_pdoc(cls, element):
103108

104109
line = inspect.getsourcelines(mod)[1]
105110
source_path = inspect.getsourcefile(mod)
111+
112+
# Cleanup path so it routes for site builder.
106113
for path in sys.path:
107114
source_path = source_path.replace(path, '')
115+
source_path = source_path.replace('/site-packages/gcloud', '')
108116

109-
source_path = source_path + "#L" + str(line)
117+
source_path = source_path + '#L' + str(line)
110118
method.add_source_line(source_path)
111119

112-
# Sketchy get examples from method docstring.
113-
mod_doctest = doctest.DocTestFinder().find(mod)
114-
for md in mod_doctest:
115-
for example in md.examples:
116-
method.add_example(example.source)
120+
# Sketchy get examples from method docstring.\
121+
examples = get_module_examples(mod)
122+
for example in examples:
123+
method.add_example(example)
117124

118125
if element.docstring:
119126
if not isinstance(element, pdoc.Class) and element.cls:
@@ -125,9 +132,14 @@ def from_pdoc(cls, element):
125132

126133
# Hack for old-style classes
127134
if str(cls)[0] != '<':
128-
cls = "<class '" + str(cls) + "'>"
135+
cls = '<class \'' + str(cls) + '\'>'
129136

130-
method_info = parse_docstring(element.docstring, cls)
137+
try:
138+
method_info = parse_docstring(element.docstring, cls)
139+
except Exception as err:
140+
print err.message
141+
print method.name
142+
return method
131143

132144
for name, data in method_info['arguments'].items():
133145
param = Param.from_docstring_section(name, data)
@@ -166,6 +178,16 @@ def from_docstring_section(cls, name, data):
166178
types=[data['type_name']])
167179

168180

181+
def get_module_examples(module):
182+
examples = []
183+
module_doctest = doctest.DocTestFinder().find(module)
184+
for doctest_obj in module_doctest:
185+
for example in doctest_obj.examples:
186+
example_str = "%s\n%s" % (example.source, example.want)
187+
examples.append(example_str)
188+
return examples
189+
190+
169191
def write_docs_file(path, contents):
170192
if not os.path.exists(os.path.dirname(path)):
171193
try:
@@ -178,17 +200,17 @@ def write_docs_file(path, contents):
178200

179201
def generate_doc_types_json(modules, types_file_path):
180202
doc_types_list = [{
181-
"id": "gcloud",
182-
"contents": "__init__.json",
183-
"title": "__Init__"
203+
'id': 'gcloud',
204+
'contents': '__init__.json',
205+
'title': '__Init__'
184206
}]
185207

186208
for module_name in modules:
187209
module_path = module_name.replace('.', '/').replace('gcloud/', '')
188210
doc_type_object = {
189-
"id": module_path,
190-
"title": u' \xbb '.join(module_path.split('/')).title(),
191-
"contents": module_path + '.json'
211+
'id': module_path,
212+
'title': u' \xbb '.join(module_path.split('/')).title(),
213+
'contents': module_path + '.json'
192214
}
193215
doc_types_list.append(doc_type_object)
194216

@@ -200,29 +222,27 @@ def generate_module_docs(modules, base_path):
200222
for module_name in modules:
201223
module = Module.from_module_name(module_name)
202224
module_path = module_name.replace('.', '/').replace('gcloud/', '')
203-
module_docs_path = (base_path +
204-
'/' + module_path + '.json')
225+
module_docs_path = os.path.join(base_path, module_path) + '.json'
205226

206227
write_docs_file(module_docs_path, json.dumps(module.to_dict(),
207228
indent=2, sort_keys=True))
208229

209230

210231
def main():
211-
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
212-
DOCS_DIR = os.path.join(BASE_DIR, 'docs') + '/json/'
213-
214232
parser = argparse.ArgumentParser(description='Document Python modules.')
215233
parser.add_argument('tag', help='The version of the documentation.')
216234
args = parser.parse_args()
217235

236+
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
237+
DOCS_DIR = os.path.join(BASE_DIR, 'docs', 'json', args.tag)
238+
218239
library_dir = os.path.join(BASE_DIR, 'gcloud')
219-
public_mods = get_public_modules(library_dir,
220-
base_package='gcloud')
240+
public_mods = get_public_modules(library_dir, base_package='gcloud')
221241

222-
generate_module_docs(public_mods, DOCS_DIR + args.tag)
223-
generate_doc_types_json(public_mods, DOCS_DIR + args.tag + '/types.json')
242+
generate_module_docs(public_mods, DOCS_DIR)
243+
generate_doc_types_json(public_mods, DOCS_DIR + '/types.json')
224244

225-
copyfile(DOCS_DIR + 'toc.json', DOCS_DIR + args.tag + '/toc.json')
245+
copyfile(DOCS_DIR + 'toc.json', DOCS_DIR + '/toc.json')
226246

227247
if __name__ == '__main__':
228248
main()

tox.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ deps =
7373
sphinx_rtd_theme
7474
passenv = {[testenv:system-tests]passenv} SPHINX_RELEASE READTHEDOCS LOCAL_RTD
7575

76+
[testenv:json-docs]
77+
basepython =
78+
python2.7
79+
commands =
80+
python scripts/generate_json_docs.py master
81+
deps =
82+
parinx
83+
pdoc
84+
Sphinx
85+
setenv =
86+
PYTHONPATH = {toxinidir}/_testing
87+
7688
[testenv:docs-rtd]
7789
setenv =
7890
PYTHONPATH = {toxinidir}/_testing

0 commit comments

Comments
 (0)