Skip to content

Commit ad8fbe2

Browse files
committed
Refactoring
1 parent bd0684d commit ad8fbe2

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

scripts/generate_json_docs.py

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# -*- coding: utf-8 -*-
22

3+
# Copyright 2016 Google Inc. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
317
import argparse
418
import json
519
import inspect
@@ -76,7 +90,6 @@ def to_dict(self):
7690
'params': [p.to_dict() for p in self.params],
7791
'exceptions': self.exceptions, 'returns': self.returns}
7892

79-
8093
@classmethod
8194
def from_pdoc(cls, element):
8295
is_class = isinstance(element, pdoc.Class)
@@ -98,7 +111,7 @@ def from_pdoc(cls, element):
98111
source_path = source_path + "#L" + str(line)
99112
method.add_source_line(source_path)
100113

101-
# Get examples
114+
# Sketchy get examples from method docstring.
102115
mod_doctest = doctest.DocTestFinder().find(mod)
103116
for md in mod_doctest:
104117
for example in md.examples:
@@ -115,6 +128,7 @@ def from_pdoc(cls, element):
115128
# Hack for old-style classes
116129
if str(cls)[0] != '<':
117130
cls = "<class '" + str(cls) + "'>"
131+
118132
method_info = parse_docstring(element.docstring, cls)
119133

120134
for name, data in method_info['arguments'].items():
@@ -154,49 +168,61 @@ def from_docstring_section(cls, name, data):
154168
types=[data['type_name']])
155169

156170

157-
def main():
158-
BASE_DIR = os.path.abspath(
159-
os.path.join(os.path.dirname(__file__), '..'))
160-
DOCS_DIR = os.path.join(BASE_DIR, 'docs') + '/json/'
171+
def write_docs_file(path, contents):
172+
if not os.path.exists(os.path.dirname(path)):
173+
try:
174+
os.makedirs(os.path.dirname(path))
175+
except OSError:
176+
raise
177+
output_file = open(path, 'w')
178+
output_file.write(contents)
161179

162-
parser = argparse.ArgumentParser(description='Document Python modules.')
163-
parser.add_argument('tag', help='The version of the documentation.')
164-
args = parser.parse_args()
165-
166-
library_dir = os.path.join(BASE_DIR, 'gcloud')
167-
public_mods = get_public_modules(library_dir,
168-
base_package='gcloud')
169180

181+
def generate_doc_types_json(modules, types_file_path):
170182
doc_types_list = [{
171183
"id": "gcloud",
172184
"contents": "__init__.json",
173185
"title": "__Init__"
174186
}]
175187

176-
for module_name in public_mods:
177-
module = Module.from_module_name(module_name)
188+
for module_name in modules:
178189
module_path = module_name.replace('.', '/').replace('gcloud/', '')
179-
module_docs_path = (DOCS_DIR + args.tag +
180-
'/' + module_path + '.json')
181-
print module_docs_path
182-
183190
doc_type_object = {
184191
"id": module_path,
185192
"title": " » ".join(module_path.split('/')).title(),
186193
"contents": module_path + '.json'
187194
}
188195
doc_types_list.append(doc_type_object)
189196

190-
if not os.path.exists(os.path.dirname(module_docs_path)):
191-
try:
192-
os.makedirs(os.path.dirname(module_docs_path))
193-
except OSError:
194-
raise
195-
output_file = open(module_docs_path, 'w')
196-
output_file.write(json.dumps(module.to_dict(),
197-
indent=2, sort_keys=True))
198-
types_file = open(DOCS_DIR + args.tag + '/types.json', 'w')
199-
types_file.write(json.dumps(doc_types_list))
197+
write_docs_file(types_file_path,
198+
json.dumps(doc_types_list))
199+
200+
201+
def generate_module_docs(modules, base_path):
202+
for module_name in modules:
203+
module = Module.from_module_name(module_name)
204+
module_path = module_name.replace('.', '/').replace('gcloud/', '')
205+
module_docs_path = (base_path +
206+
'/' + module_path + '.json')
207+
208+
write_docs_file(module_docs_path, json.dumps(module.to_dict(),
209+
indent=2, sort_keys=True))
210+
211+
212+
def main():
213+
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
214+
DOCS_DIR = os.path.join(BASE_DIR, 'docs') + '/json/'
215+
216+
parser = argparse.ArgumentParser(description='Document Python modules.')
217+
parser.add_argument('tag', help='The version of the documentation.')
218+
args = parser.parse_args()
219+
220+
library_dir = os.path.join(BASE_DIR, 'gcloud')
221+
public_mods = get_public_modules(library_dir,
222+
base_package='gcloud')
223+
224+
generate_module_docs(public_mods, DOCS_DIR + args.tag)
225+
generate_doc_types_json(public_mods, DOCS_DIR + args.tag + '/types.json')
200226

201227
copyfile(DOCS_DIR + 'toc.json', DOCS_DIR + args.tag + '/toc.json')
202228

0 commit comments

Comments
 (0)