/usr/lib/python2.7/dist-packages/cartopy/sphinxext/summarise_package.py is in python-cartopy 0.14.2+dfsg1-2build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | # (C) British Crown Copyright 2011 - 2016, Met Office
#
# This file is part of cartopy.
#
# cartopy is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cartopy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with cartopy. If not, see <https://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
import inspect
import itertools
import os
import sys
import warnings
import six
import cartopy.tests
def walk_module(mod_name, exclude_folders=None):
"""
Recursively walks the given module name.
Returns:
A generator of::
(fully_qualified_import_name,
root_directory_of_subpackage,
fname_in_root_directory,
sub_folders_in_root_directory)
"""
__import__(mod_name)
mod = sys.modules[mod_name]
mod_dir = os.path.dirname(mod.__file__)
exclude_folders = exclude_folders or []
for root, folders, files in os.walk(mod_dir):
for folder in exclude_folders:
try:
folders.remove(folder)
except ValueError:
pass
# only allow python packages
if '__init__.py' not in files:
del folders[:]
continue
# Sort by filename and folder name.
files.sort()
folders.sort()
def is_py_src(fname):
root, ext = os.path.splitext(fname)
return ext in ('.py', '.so')
files = filter(is_py_src, files)
for fname in files:
sub_mod_name = mod_name
relpath = os.path.relpath(root, mod_dir)
if relpath == '.':
relpath = ''
for sub_mod in [f for f in relpath.split(os.path.sep) if f]:
sub_mod_name += '.' + sub_mod
if fname != '__init__.py':
sub_mod_name += '.' + os.path.splitext(fname)[0]
yield sub_mod_name, root, fname, folders
def objects_to_document(module_name):
"""
Creates a generator of (obj_name, obj) that the given module of the
given name should document.
The module name may be any importable, including submodules
(e.g. ``cartopy.io``)
"""
try:
__import__(module_name)
except ImportError:
warnings.warn('Failed to document {}'.format(module_name))
return []
module = sys.modules[module_name]
elems = dir(module)
if '__all__' in elems:
document_these = [(obj, getattr(module, obj))
for obj in module.__all__]
else:
document_these = [(obj, getattr(module, obj)) for obj in elems
if not inspect.ismodule(getattr(module, obj)) and
not obj.startswith('_')]
def is_from_this_module(x):
return getattr(x[1], '__module__', '') == module_name
document_these = filter(is_from_this_module, document_these)
document_these = sorted(document_these,
key=lambda x: (str(type(x[1])),
not x[0].isupper(),
x[0]))
# allow a developer to add other things to the documentation
if hasattr(module, '__document_these__'):
extra_objects_to_document = tuple((obj, getattr(module, obj))
for obj in module.__document_these__)
document_these = extra_objects_to_document + tuple(document_these)
return document_these
def main(package_name, exclude_folders=None):
"""
Return a string containing the rst that documents the given package name.
"""
result = ''
mod_walk = walk_module(package_name, exclude_folders=exclude_folders)
for mod, _, fname, folders in mod_walk:
for folder in folders:
if folder.startswith('_'):
folders.remove(folder)
if fname.startswith('_') and fname != '__init__.py':
continue
result += '\n'
result += mod + '\n'
result += '*' * len(mod) + '\n'
result += '\n'
result += '.. currentmodule:: {}\n'.format(mod) + '\n'
mod_objects = objects_to_document(mod)
if mod_objects:
result += '.. csv-table::\n' + '\n'
table_elements = itertools.cycle(('\n\t', ) + (', ', ) * 3)
for table_elem, (obj_name, _) in zip(table_elements, mod_objects):
result += '{}:py:obj:`{}`'.format(table_elem, obj_name)
result += '\n'
return result
def gen_summary_rst(app):
"""
Creates the rst file to summarise the desired packages.
"""
package_names = app.config.summarise_package_names
exclude_dirs = app.config.summarise_package_exclude_directories
fnames = app.config.summarise_package_fnames
if isinstance(package_names, six.string_types):
package_names = [package_names]
if package_names is None:
raise ValueError('Please define a config value containing a list '
'of packages to summarise.')
if exclude_dirs is None:
exclude_dirs = [None] * len(package_names)
else:
exception = ValueError('Please provide a list of exclude directory '
'lists (one list for each package to '
'summarise).')
if len(exclude_dirs) != len(package_names):
raise exception
for exclude_dirs_individual in exclude_dirs:
if isinstance(exclude_dirs_individual, six.string_types):
raise exception
if fnames is None:
fnames = ['outline_of_{}.rst'.format(package_name)
for package_name in package_names]
else:
if isinstance(fnames, six.string_types) or \
len(fnames) != len(package_names):
raise TypeError('Please provide a list of filenames for each of '
'the packages which are to be summarised.')
outdir = app.builder.srcdir
for package_name, out_fname, exclude_folders in zip(package_names,
fnames,
exclude_dirs):
out_fpath = os.path.join(outdir, out_fname)
content = main(package_name, exclude_folders=exclude_folders)
with open(out_fpath, 'w') as fh:
fh.write(content)
@cartopy.tests.not_a_nose_fixture
def setup(app):
"""
Defined the Sphinx application interface for the summary generation.
"""
app.connect('builder-inited', gen_summary_rst)
# Allow users to define a config value to determine the names to summarise
app.add_config_value('summarise_package_names', None, 'env')
# Allow users to define a config value to determine the folders to exclude
app.add_config_value('summarise_package_exclude_directories', None, 'env')
# Allow users to define a config value to determine name of the output file
app.add_config_value('summarise_package_fnames', None, 'env')
|