Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sys.path's from source link. #2052

Merged
merged 1 commit into from
Aug 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 23 additions & 29 deletions scripts/generate_json_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
import pdoc
from parinx.parser import parse_docstring
from parinx.errors import MethodParsingException
import six

from verify_included_modules import get_public_modules

import gcloud

ABSOLUTE_LIBRARY_PATH = os.path.dirname(os.path.dirname(os.path.abspath(
gcloud.__file__)))


class Module(object):

Expand All @@ -46,17 +42,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 @@ -90,8 +83,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 Down Expand Up @@ -254,19 +246,19 @@ def build_link_from_type(type_name, object_type=None):
type_markup = '<a data-custom-type="%s"' % doc_path
if object_type == 'instance':
type_markup += ' data-method="%s"' % type_name
type_markup += '>%s</a>' % type_name
type_markup += '>%s</a>' % (type_name,)

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)
source_path = clean_source_path(module)

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


def clean_source_path(source):
source_path = ''
if ABSOLUTE_LIBRARY_PATH in source:
source_path = source.replace(ABSOLUTE_LIBRARY_PATH, source)

return source_path
def clean_source_path(module):
if isinstance(module, six.string_types):
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 All @@ -321,7 +315,7 @@ def process_code_blocks(doc):
is_code = True

if is_code:
block = {'code': '<pre><code>%s</code></pre>' % block}
block = {'code': '<pre><code>%s</code></pre>' % (block,)}

formatted_blocks.append(block)
return formatted_blocks
Expand Down Expand Up @@ -349,11 +343,11 @@ def process_words(line):

if word.startswith('``') and word.endswith('``'):
word = word.replace('``', '')
word = '<code>%s</code>' % word
word = '<code>%s</code>' % (word,)

if word.startswith('**') and word.endswith('**'):
word = word.replace('**', '')
word = '<b>%s</b>' % word
word = '<b>%s</b>' % (word,)

if word.startswith(':class:'):
word = word.replace(':class:', '').replace('`', '')
Expand All @@ -379,7 +373,7 @@ def process_words(line):
if end_sentence:
word += '.'

processed_line += ' %s' % word
processed_line += ' %s' % (word,)

processed_line = processed_line.replace('::', '')

Expand Down