From 97d6efe8f501992030d24c8f159b6939ad26f89d Mon Sep 17 00:00:00 2001 From: Thomas Schultz Date: Tue, 16 Aug 2016 16:59:42 -0400 Subject: [PATCH] Different approach for finding base path. --- scripts/generate_json_docs.py | 42 +++++++++++++++-------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/scripts/generate_json_docs.py b/scripts/generate_json_docs.py index 5ce53a46c746e..8d7e3e8dffe7b 100644 --- a/scripts/generate_json_docs.py +++ b/scripts/generate_json_docs.py @@ -17,7 +17,6 @@ import json import os import shutil -import sys import types import pdoc @@ -47,9 +46,6 @@ 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: @@ -57,7 +53,7 @@ def from_module_name(cls, name, base_path): 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(), @@ -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(), @@ -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): @@ -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) @@ -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):