Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def generate_module_docs(modules, docs_path, real_base_path, toc):
for module_name in modules:
module = Module.from_module_name(module_name, real_base_path)
pdoc_module = pdoc.Module(pdoc.import_module(module_name),
allsubmodules=True)
for c in pdoc_module.classes():
generate_class_docs(pdoc_module, c, docs_path, toc)
module_path = (module_name.replace('.', '/')
.replace('__init__', 'index'))
module_docs_path = os.path.join(docs_path, module_path) + '.json'
if pdoc_module.functions():
toc_key = module_name.replace('google.cloud.', '').split('.')[0]
toc_entry = build_toc_entry(module.name, module_path)
toc['services'][toc_key].append(toc_entry)
write_docs_file(module_docs_path,
json.dumps(module.to_dict(),
indent=2, sort_keys=True))
def generate_module_docs(modules, docs_path, real_base_path, toc):
for module_name in modules:
module = Module.from_module_name(module_name, real_base_path)
pdoc_module = pdoc.Module(pdoc.import_module(module_name),
allsubmodules=True)
for c in pdoc_module.classes():
generate_class_docs(pdoc_module, c, docs_path, toc)
module_path = (module_name.replace('.', '/')
.replace('__init__', 'index'))
module_docs_path = os.path.join(docs_path, module_path) + '.json'
if pdoc_module.functions():
toc_key = module_name.replace('gcloud.', '').split('.')[0]
toc_entry = build_toc_entry(module.name, module_path)
toc['services'][toc_key].append(toc_entry)
write_docs_file(module_docs_path,
json.dumps(module.to_dict(),
indent=2, sort_keys=True))
module_contents = (module_name.replace('.', '/')
.replace('__init__', 'index'))
if len(module_name.split('.')) > 2:
module_id = module_name.replace('.', '/')
else:
module_id = (module_name.replace('.', '/'))
module_contents += '.json'
doc_type_object = build_type(module_id.replace('/__init__', ''),
module_title, module_contents)
doc_types_list.append(doc_type_object)
pdoc_module = pdoc.Module(pdoc.import_module(module_name),
allsubmodules=True)
for c in pdoc_module.classes():
generate_doc_types_classes_json(c, doc_types_list)
write_docs_file(types_file_path,
json.dumps(doc_types_list))
@classmethod
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))
return cls(module_id=name,
name=name.split('.')[-1].title(),
description=module.docstring,
fp = path.realpath(args.module_name)
module_name = path.basename(fp)
if isdir:
fp = path.join(fp, '__init__.py')
else:
module_name, _ = path.splitext(module_name)
# Use a special module name to avoid import conflicts.
# It is hidden from view via the `Module` class.
with open(fp) as f:
module = imp.load_source('__pdoc_file_module__', fp, f)
if isdir:
module.__path__ = [path.realpath(args.module_name)]
module.__pdoc_module_name = module_name
else:
module = pdoc.import_module(args.module_name)
module = pdoc.Module(module, docfilter=docfilter,
allsubmodules=args.all_submodules)
# Plain text?
if not args.html and not args.all_submodules:
output = module.text()
try:
print(output)
except IOError as e:
# This seems to happen for long documentation.
# This is obviously a hack. What's the real cause? Dunno.
if e.errno == 32:
pass
else:
raise e
sys.exit(0)
module_contents = (module_name.replace('.', '/')
.replace('__init__', 'index'))
if len(module_name.split('.')) > 2:
module_id = module_name.replace('.', '/')
else:
module_id = (module_name.replace('.', '/'))
module_contents += '.json'
doc_type_object = build_type(module_id.replace('/__init__', ''),
module_title, module_contents)
doc_types_list.append(doc_type_object)
pdoc_module = pdoc.Module(pdoc.import_module(module_name),
allsubmodules=True)
for c in pdoc_module.classes():
generate_doc_types_classes_json(c, doc_types_list)
write_docs_file(types_file_path,
json.dumps(doc_types_list))
search = args.ident_name.strip()
def docfilter(o):
rname = o.refname
if rname.find(search) > -1 or search.find(o.name) > -1:
return True
if isinstance(o, pdoc.Class):
return search in o.doc or search in o.doc_init
return False
# Try to do a real import first. I think it's better to prefer
# import paths over files. If a file is really necessary, then
# specify the absolute path, which is guaranteed not to be a
# Python import path.
try:
module = pdoc.import_module(args.module_name)
except Exception as e:
module = None
# Get the module that we're documenting. Accommodate for import paths,
# files and directories.
if module is None:
print(module)
isdir = path.isdir(args.module_name)
isfile = path.isfile(args.module_name)
if isdir or isfile:
fp = path.realpath(args.module_name)
module_name = path.basename(fp)
if isdir:
fp = path.join(fp, '__init__.py')
else:
module_name, _ = path.splitext(module_name)