1
1
# -*- coding: utf-8 -*-
2
2
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
+
3
17
import argparse
4
18
import json
5
19
import inspect
@@ -76,7 +90,6 @@ def to_dict(self):
76
90
'params' : [p .to_dict () for p in self .params ],
77
91
'exceptions' : self .exceptions , 'returns' : self .returns }
78
92
79
-
80
93
@classmethod
81
94
def from_pdoc (cls , element ):
82
95
is_class = isinstance (element , pdoc .Class )
@@ -98,7 +111,7 @@ def from_pdoc(cls, element):
98
111
source_path = source_path + "#L" + str (line )
99
112
method .add_source_line (source_path )
100
113
101
- # Get examples
114
+ # Sketchy get examples from method docstring.
102
115
mod_doctest = doctest .DocTestFinder ().find (mod )
103
116
for md in mod_doctest :
104
117
for example in md .examples :
@@ -115,6 +128,7 @@ def from_pdoc(cls, element):
115
128
# Hack for old-style classes
116
129
if str (cls )[0 ] != '<' :
117
130
cls = "<class '" + str (cls ) + "'>"
131
+
118
132
method_info = parse_docstring (element .docstring , cls )
119
133
120
134
for name , data in method_info ['arguments' ].items ():
@@ -154,49 +168,61 @@ def from_docstring_section(cls, name, data):
154
168
types = [data ['type_name' ]])
155
169
156
170
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 )
161
179
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' )
169
180
181
+ def generate_doc_types_json (modules , types_file_path ):
170
182
doc_types_list = [{
171
183
"id" : "gcloud" ,
172
184
"contents" : "__init__.json" ,
173
185
"title" : "__Init__"
174
186
}]
175
187
176
- for module_name in public_mods :
177
- module = Module .from_module_name (module_name )
188
+ for module_name in modules :
178
189
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
-
183
190
doc_type_object = {
184
191
"id" : module_path ,
185
192
"title" : " » " .join (module_path .split ('/' )).title (),
186
193
"contents" : module_path + '.json'
187
194
}
188
195
doc_types_list .append (doc_type_object )
189
196
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' )
200
226
201
227
copyfile (DOCS_DIR + 'toc.json' , DOCS_DIR + args .tag + '/toc.json' )
202
228
0 commit comments