PageRenderTime 196ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/BDDS_dnaCompleteExome_optimized/pymodules/python2.7/lib/python/Sphinx-1.2.3-py2.7.egg/sphinx/apidoc.py

https://gitlab.com/pooja043/Globus_Docker
Python | 363 lines | 354 code | 0 blank | 9 comment | 0 complexity | d4fe57e62eb18a92ba74dfa0b9395607 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. sphinx.apidoc
  4. ~~~~~~~~~~~~~
  5. Parses a directory tree looking for Python modules and packages and creates
  6. ReST files appropriately to create code documentation with Sphinx. It also
  7. creates a modules index (named modules.<suffix>).
  8. This is derived from the "sphinx-autopackage" script, which is:
  9. Copyright 2008 Société des arts technologiques (SAT),
  10. http://www.sat.qc.ca/
  11. :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
  12. :license: BSD, see LICENSE for details.
  13. """
  14. import os
  15. import sys
  16. import optparse
  17. from os import path
  18. from sphinx.util.osutil import walk
  19. from sphinx import __version__
  20. # automodule options
  21. if 'SPHINX_APIDOC_OPTIONS' in os.environ:
  22. OPTIONS = os.environ['SPHINX_APIDOC_OPTIONS'].split(',')
  23. else:
  24. OPTIONS = [
  25. 'members',
  26. 'undoc-members',
  27. # 'inherited-members', # disabled because there's a bug in sphinx
  28. 'show-inheritance',
  29. ]
  30. INITPY = '__init__.py'
  31. PY_SUFFIXES = set(['.py', '.pyx'])
  32. def makename(package, module):
  33. """Join package and module with a dot."""
  34. # Both package and module can be None/empty.
  35. if package:
  36. name = package
  37. if module:
  38. name += '.' + module
  39. else:
  40. name = module
  41. return name
  42. def write_file(name, text, opts):
  43. """Write the output file for module/package <name>."""
  44. fname = path.join(opts.destdir, '%s.%s' % (name, opts.suffix))
  45. if opts.dryrun:
  46. print 'Would create file %s.' % fname
  47. return
  48. if not opts.force and path.isfile(fname):
  49. print 'File %s already exists, skipping.' % fname
  50. else:
  51. print 'Creating file %s.' % fname
  52. f = open(fname, 'w')
  53. try:
  54. f.write(text)
  55. finally:
  56. f.close()
  57. def format_heading(level, text):
  58. """Create a heading of <level> [1, 2 or 3 supported]."""
  59. underlining = ['=', '-', '~', ][level - 1] * len(text)
  60. return '%s\n%s\n\n' % (text, underlining)
  61. def format_directive(module, package=None):
  62. """Create the automodule directive and add the options."""
  63. directive = '.. automodule:: %s\n' % makename(package, module)
  64. for option in OPTIONS:
  65. directive += ' :%s:\n' % option
  66. return directive
  67. def create_module_file(package, module, opts):
  68. """Build the text of the file and write the file."""
  69. if not opts.noheadings:
  70. text = format_heading(1, '%s module' % module)
  71. else:
  72. text = ''
  73. #text += format_heading(2, ':mod:`%s` Module' % module)
  74. text += format_directive(module, package)
  75. write_file(makename(package, module), text, opts)
  76. def create_package_file(root, master_package, subroot, py_files, opts, subs):
  77. """Build the text of the file and write the file."""
  78. text = format_heading(1, '%s package' % makename(master_package, subroot))
  79. # build a list of directories that are szvpackages (contain an INITPY file)
  80. subs = [sub for sub in subs if path.isfile(path.join(root, sub, INITPY))]
  81. # if there are some package directories, add a TOC for theses subpackages
  82. if subs:
  83. text += format_heading(2, 'Subpackages')
  84. text += '.. toctree::\n\n'
  85. for sub in subs:
  86. text += ' %s.%s\n' % (makename(master_package, subroot), sub)
  87. text += '\n'
  88. submods = [path.splitext(sub)[0] for sub in py_files
  89. if not shall_skip(path.join(root, sub), opts)
  90. and sub != INITPY]
  91. if submods:
  92. text += format_heading(2, 'Submodules')
  93. if opts.separatemodules:
  94. text += '.. toctree::\n\n'
  95. for submod in submods:
  96. modfile = makename(master_package, makename(subroot, submod))
  97. text += ' %s\n' % modfile
  98. # generate separate file for this module
  99. if not opts.noheadings:
  100. filetext = format_heading(1, '%s module' % modfile)
  101. else:
  102. filetext = ''
  103. filetext += format_directive(makename(subroot, submod),
  104. master_package)
  105. write_file(modfile, filetext, opts)
  106. else:
  107. for submod in submods:
  108. modfile = makename(master_package, makename(subroot, submod))
  109. if not opts.noheadings:
  110. text += format_heading(2, '%s module' % modfile)
  111. text += format_directive(makename(subroot, submod),
  112. master_package)
  113. text += '\n'
  114. text += '\n'
  115. text += format_heading(2, 'Module contents')
  116. text += format_directive(subroot, master_package)
  117. write_file(makename(master_package, subroot), text, opts)
  118. def create_modules_toc_file(modules, opts, name='modules'):
  119. """Create the module's index."""
  120. text = format_heading(1, '%s' % opts.header)
  121. text += '.. toctree::\n'
  122. text += ' :maxdepth: %s\n\n' % opts.maxdepth
  123. modules.sort()
  124. prev_module = ''
  125. for module in modules:
  126. # look if the module is a subpackage and, if yes, ignore it
  127. if module.startswith(prev_module + '.'):
  128. continue
  129. prev_module = module
  130. text += ' %s\n' % module
  131. write_file(name, text, opts)
  132. def shall_skip(module, opts):
  133. """Check if we want to skip this module."""
  134. # skip it if there is nothing (or just \n or \r\n) in the file
  135. if path.getsize(module) <= 2:
  136. return True
  137. # skip if it has a "private" name and this is selected
  138. filename = path.basename(module)
  139. if filename != '__init__.py' and filename.startswith('_') and \
  140. not opts.includeprivate:
  141. return True
  142. return False
  143. def recurse_tree(rootpath, excludes, opts):
  144. """
  145. Look for every file in the directory tree and create the corresponding
  146. ReST files.
  147. """
  148. # check if the base directory is a package and get its name
  149. if INITPY in os.listdir(rootpath):
  150. root_package = rootpath.split(path.sep)[-1]
  151. else:
  152. # otherwise, the base is a directory with packages
  153. root_package = None
  154. toplevels = []
  155. followlinks = getattr(opts, 'followlinks', False)
  156. includeprivate = getattr(opts, 'includeprivate', False)
  157. for root, subs, files in walk(rootpath, followlinks=followlinks):
  158. # document only Python module files (that aren't excluded)
  159. py_files = sorted(f for f in files
  160. if path.splitext(f)[1] in PY_SUFFIXES and
  161. not is_excluded(path.join(root, f), excludes))
  162. is_pkg = INITPY in py_files
  163. if is_pkg:
  164. py_files.remove(INITPY)
  165. py_files.insert(0, INITPY)
  166. elif root != rootpath:
  167. # only accept non-package at toplevel
  168. del subs[:]
  169. continue
  170. # remove hidden ('.') and private ('_') directories, as well as
  171. # excluded dirs
  172. if includeprivate:
  173. exclude_prefixes = ('.',)
  174. else:
  175. exclude_prefixes = ('.', '_')
  176. subs[:] = sorted(sub for sub in subs if not sub.startswith(exclude_prefixes)
  177. and not is_excluded(path.join(root, sub), excludes))
  178. if is_pkg:
  179. # we are in a package with something to document
  180. if subs or len(py_files) > 1 or not \
  181. shall_skip(path.join(root, INITPY), opts):
  182. subpackage = root[len(rootpath):].lstrip(path.sep).\
  183. replace(path.sep, '.')
  184. create_package_file(root, root_package, subpackage,
  185. py_files, opts, subs)
  186. toplevels.append(makename(root_package, subpackage))
  187. else:
  188. # if we are at the root level, we don't require it to be a package
  189. assert root == rootpath and root_package is None
  190. for py_file in py_files:
  191. if not shall_skip(path.join(rootpath, py_file), opts):
  192. module = path.splitext(py_file)[0]
  193. create_module_file(root_package, module, opts)
  194. toplevels.append(module)
  195. return toplevels
  196. def normalize_excludes(rootpath, excludes):
  197. """Normalize the excluded directory list."""
  198. return [path.normpath(path.abspath(exclude)) for exclude in excludes]
  199. def is_excluded(root, excludes):
  200. """Check if the directory is in the exclude list.
  201. Note: by having trailing slashes, we avoid common prefix issues, like
  202. e.g. an exlude "foo" also accidentally excluding "foobar".
  203. """
  204. root = path.normpath(root)
  205. for exclude in excludes:
  206. if root == exclude:
  207. return True
  208. return False
  209. def main(argv=sys.argv):
  210. """Parse and check the command line arguments."""
  211. parser = optparse.OptionParser(
  212. usage="""\
  213. usage: %prog [options] -o <output_path> <module_path> [exclude_path, ...]
  214. Look recursively in <module_path> for Python modules and packages and create
  215. one reST file with automodule directives per package in the <output_path>.
  216. The <exclude_path>s can be files and/or directories that will be excluded
  217. from generation.
  218. Note: By default this script will not overwrite already created files.""")
  219. parser.add_option('-o', '--output-dir', action='store', dest='destdir',
  220. help='Directory to place all output', default='')
  221. parser.add_option('-d', '--maxdepth', action='store', dest='maxdepth',
  222. help='Maximum depth of submodules to show in the TOC '
  223. '(default: 4)', type='int', default=4)
  224. parser.add_option('-f', '--force', action='store_true', dest='force',
  225. help='Overwrite existing files')
  226. parser.add_option('-l', '--follow-links', action='store_true',
  227. dest='followlinks', default=False,
  228. help='Follow symbolic links. Powerful when combined '
  229. 'with collective.recipe.omelette.')
  230. parser.add_option('-n', '--dry-run', action='store_true', dest='dryrun',
  231. help='Run the script without creating files')
  232. parser.add_option('-e', '--separate', action='store_true',
  233. dest='separatemodules',
  234. help='Put documentation for each module on its own page')
  235. parser.add_option('-P', '--private', action='store_true',
  236. dest='includeprivate',
  237. help='Include "_private" modules')
  238. parser.add_option('-T', '--no-toc', action='store_true', dest='notoc',
  239. help='Don\'t create a table of contents file')
  240. parser.add_option('-E', '--no-headings', action='store_true',
  241. dest='noheadings',
  242. help='Don\'t create headings for the module/package '
  243. 'packages (e.g. when the docstrings already contain '
  244. 'them)')
  245. parser.add_option('-s', '--suffix', action='store', dest='suffix',
  246. help='file suffix (default: rst)', default='rst')
  247. parser.add_option('-F', '--full', action='store_true', dest='full',
  248. help='Generate a full project with sphinx-quickstart')
  249. parser.add_option('-H', '--doc-project', action='store', dest='header',
  250. help='Project name (default: root module name)')
  251. parser.add_option('-A', '--doc-author', action='store', dest='author',
  252. type='str',
  253. help='Project author(s), used when --full is given')
  254. parser.add_option('-V', '--doc-version', action='store', dest='version',
  255. help='Project version, used when --full is given')
  256. parser.add_option('-R', '--doc-release', action='store', dest='release',
  257. help='Project release, used when --full is given, '
  258. 'defaults to --doc-version')
  259. parser.add_option('--version', action='store_true', dest='show_version',
  260. help='Show version information and exit')
  261. (opts, args) = parser.parse_args(argv[1:])
  262. if opts.show_version:
  263. print 'Sphinx (sphinx-apidoc) %s' % __version__
  264. return 0
  265. if not args:
  266. parser.error('A package path is required.')
  267. rootpath, excludes = args[0], args[1:]
  268. if not opts.destdir:
  269. parser.error('An output directory is required.')
  270. if opts.header is None:
  271. opts.header = path.normpath(rootpath).split(path.sep)[-1]
  272. if opts.suffix.startswith('.'):
  273. opts.suffix = opts.suffix[1:]
  274. if not path.isdir(rootpath):
  275. print >>sys.stderr, '%s is not a directory.' % rootpath
  276. sys.exit(1)
  277. if not path.isdir(opts.destdir):
  278. if not opts.dryrun:
  279. os.makedirs(opts.destdir)
  280. rootpath = path.normpath(path.abspath(rootpath))
  281. excludes = normalize_excludes(rootpath, excludes)
  282. modules = recurse_tree(rootpath, excludes, opts)
  283. if opts.full:
  284. from sphinx import quickstart as qs
  285. modules.sort()
  286. prev_module = ''
  287. text = ''
  288. for module in modules:
  289. if module.startswith(prev_module + '.'):
  290. continue
  291. prev_module = module
  292. text += ' %s\n' % module
  293. d = dict(
  294. path = opts.destdir,
  295. sep = False,
  296. dot = '_',
  297. project = opts.header,
  298. author = opts.author or 'Author',
  299. version = opts.version or '',
  300. release = opts.release or opts.version or '',
  301. suffix = '.' + opts.suffix,
  302. master = 'index',
  303. epub = True,
  304. ext_autodoc = True,
  305. ext_viewcode = True,
  306. makefile = True,
  307. batchfile = True,
  308. mastertocmaxdepth = opts.maxdepth,
  309. mastertoctree = text,
  310. )
  311. if not opts.dryrun:
  312. qs.generate(d, silent=True, overwrite=opts.force)
  313. elif not opts.notoc:
  314. create_modules_toc_file(modules, opts)