22
22
import pdoc
23
23
from parinx .parser import parse_docstring
24
24
from parinx .errors import MethodParsingException
25
+ import six
25
26
26
27
from verify_included_modules import get_public_modules
27
28
28
- import gcloud
29
-
30
- ABSOLUTE_LIBRARY_PATH = os .path .dirname (os .path .dirname (os .path .abspath (
31
- gcloud .__file__ )))
32
-
33
29
34
30
class Module (object ):
35
31
@@ -46,17 +42,14 @@ def __init__(self, module_id, name, description=None,
46
42
def from_module_name (cls , name , base_path ):
47
43
module = pdoc .Module (pdoc .import_module (name ), allsubmodules = True )
48
44
methods = module .functions () + module .variables ()
49
-
50
- mod = __import__ (name )
51
-
52
45
examples = []
53
46
54
47
if '__init__' in name :
55
48
snippets = get_snippet_examples (name .split ('.' )[1 ],
56
49
os .path .join (base_path , 'docs' ))
57
50
examples .extend (snippets )
58
51
59
- source_path = clean_source_path (inspect . getsourcefile ( mod ) )
52
+ source_path = clean_source_path (module )
60
53
61
54
return cls (module_id = name ,
62
55
name = name .split ('.' )[- 1 ].title (),
@@ -90,8 +83,7 @@ def from_class_name(cls, module, kls):
90
83
methods = kls .methods ()
91
84
92
85
examples = []
93
- source_module = __import__ (module .name )
94
- source_path = clean_source_path (inspect .getsourcefile (source_module ))
86
+ source_path = clean_source_path (module )
95
87
96
88
return cls (module_id = kls .name ,
97
89
name = kls .name .split ('.' )[- 1 ].title (),
@@ -254,19 +246,19 @@ def build_link_from_type(type_name, object_type=None):
254
246
type_markup = '<a data-custom-type="%s"' % doc_path
255
247
if object_type == 'instance' :
256
248
type_markup += ' data-method="%s"' % type_name
257
- type_markup += '>%s</a>' % type_name
249
+ type_markup += '>%s</a>' % ( type_name ,)
258
250
259
251
return type_markup
260
252
261
253
262
- def build_source (mod , method ):
263
- if isinstance (mod , (types .ModuleType , types .ClassType ,
264
- types .MethodType , types .FunctionType ,
265
- types .TracebackType , types .FrameType ,
266
- types .CodeType , types .TypeType )):
254
+ def build_source (module , method ):
255
+ if isinstance (module , (types .ModuleType , types .ClassType ,
256
+ types .MethodType , types .FunctionType ,
257
+ types .TracebackType , types .FrameType ,
258
+ types .CodeType , types .TypeType )):
267
259
268
- line = inspect .getsourcelines (mod )[ 1 ]
269
- source_path = clean_source_path (inspect . getsourcefile ( mod ) )
260
+ _ , line = inspect .getsourcelines (module )
261
+ source_path = clean_source_path (module )
270
262
271
263
if line :
272
264
source_path = source_path + '#L' + str (line )
@@ -294,12 +286,14 @@ def build_type(type_id, title, contents):
294
286
}
295
287
296
288
297
- def clean_source_path (source ):
298
- source_path = ''
299
- if ABSOLUTE_LIBRARY_PATH in source :
300
- source_path = source .replace (ABSOLUTE_LIBRARY_PATH , source )
301
-
302
- return source_path
289
+ def clean_source_path (module ):
290
+ if isinstance (module , six .string_types ):
291
+ source_id = module
292
+ elif hasattr (module , 'refname' ):
293
+ source_id = module .refname
294
+ else :
295
+ source_id = inspect .getmodule (module ).__name__
296
+ return '%s.py' % (source_id .replace ('.' , '/' ),)
303
297
304
298
305
299
def process_code_blocks (doc ):
@@ -321,7 +315,7 @@ def process_code_blocks(doc):
321
315
is_code = True
322
316
323
317
if is_code :
324
- block = {'code' : '<pre><code>%s</code></pre>' % block }
318
+ block = {'code' : '<pre><code>%s</code></pre>' % ( block ,) }
325
319
326
320
formatted_blocks .append (block )
327
321
return formatted_blocks
@@ -349,11 +343,11 @@ def process_words(line):
349
343
350
344
if word .startswith ('``' ) and word .endswith ('``' ):
351
345
word = word .replace ('``' , '' )
352
- word = '<code>%s</code>' % word
346
+ word = '<code>%s</code>' % ( word ,)
353
347
354
348
if word .startswith ('**' ) and word .endswith ('**' ):
355
349
word = word .replace ('**' , '' )
356
- word = '<b>%s</b>' % word
350
+ word = '<b>%s</b>' % ( word ,)
357
351
358
352
if word .startswith (':class:' ):
359
353
word = word .replace (':class:' , '' ).replace ('`' , '' )
@@ -379,7 +373,7 @@ def process_words(line):
379
373
if end_sentence :
380
374
word += '.'
381
375
382
- processed_line += ' %s' % word
376
+ processed_line += ' %s' % ( word ,)
383
377
384
378
processed_line = processed_line .replace ('::' , '' )
385
379
0 commit comments