Skip to content

Commit

Permalink
Different approach for finding base path.
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Aug 16, 2016
1 parent 1019102 commit 97d6efe
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions scripts/generate_json_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import json
import os
import shutil
import sys
import types

import pdoc
Expand Down Expand Up @@ -47,17 +46,14 @@ def __init__(self, module_id, name, description=None,
def from_module_name(cls, name, base_path):
module = pdoc.Module(pdoc.import_module(name), allsubmodules=True)
methods = module.functions() + module.variables()

mod = __import__(name)

examples = []

if '__init__' in name:
snippets = get_snippet_examples(name.split('.')[1],
os.path.join(base_path, 'docs'))
examples.extend(snippets)

source_path = clean_source_path(inspect.getsourcefile(mod))
source_path = clean_source_path(module)

return cls(module_id=name,
name=name.split('.')[-1].title(),
Expand Down Expand Up @@ -91,8 +87,7 @@ def from_class_name(cls, module, kls):
methods = kls.methods()

examples = []
source_module = __import__(module.name)
source_path = clean_source_path(inspect.getsourcefile(source_module))
source_path = clean_source_path(module)

return cls(module_id=kls.name,
name=kls.name.split('.')[-1].title(),
Expand All @@ -108,7 +103,7 @@ def to_dict(self):
'description': format_sphinx_doc(self.description),
'examples': self.examples,
'methods': [m.to_dict() for m in self.methods],
'source': self.source}
'source': '%s' % self.source}


class Method(object):
Expand Down Expand Up @@ -260,14 +255,14 @@ def build_link_from_type(type_name, object_type=None):
return type_markup


def build_source(mod, method):
if isinstance(mod, (types.ModuleType, types.ClassType,
types.MethodType, types.FunctionType,
types.TracebackType, types.FrameType,
types.CodeType, types.TypeType)):
def build_source(module, method):
if isinstance(module, (types.ModuleType, types.ClassType,
types.MethodType, types.FunctionType,
types.TracebackType, types.FrameType,
types.CodeType, types.TypeType)):

line = inspect.getsourcelines(mod)[1]
source_path = clean_source_path(inspect.getsourcefile(mod))
line = inspect.getsourcelines(module)[1]
source_path = clean_source_path(module)

if line:
source_path = source_path + '#L' + str(line)
Expand Down Expand Up @@ -295,15 +290,14 @@ def build_type(type_id, title, contents):
}


def clean_source_path(source):

for path in sys.path:
source = source.replace(path, '')

if ABSOLUTE_LIBRARY_PATH in source:
source = source.replace(ABSOLUTE_LIBRARY_PATH, source)

return source[1:]
def clean_source_path(module):
if isinstance(module, str):
source_id = module
elif hasattr(module, 'refname'):
source_id = module.refname
else:
source_id = inspect.getmodule(module).__name__
return '%s.py' % source_id.replace('.', '/')


def process_code_blocks(doc):
Expand Down

0 comments on commit 97d6efe

Please sign in to comment.