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
|
#!/usr/bin/python
# Copyright (C) 2007-2008 www.stani.be
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/
# Follows PEP8
import glob
import os
from subprocess import call
TOC = '''
.. toctree::
:maxdepth: 2
%s
'''
AUTO = '''
.. automodule:: %s
:members:
:undoc-members:
:show-inheritance:
'''
def title(header):
"""Formats a header title.
:param header: header text
:type header: str
:returns: header text
:rtype: str
>>> title('header')
'header\\n******\\n'
"""
return '%s\n%s\n' % (header, '*' * len(header))
def auto(module):
"""Returns sphinx code to automatically document the module.
:param module: module name
:type module: str
:returns: sphinx code
:rtype: str
>>> print(auto('module'))
<BLANKLINE>
.. automodule:: module
:members:
:undoc-members:
:show-inheritance:
<BLANKLINE>
"""
return AUTO % module
def toc(modules):
"""Return sphinx code for module table of contents
:param modules: module names
:type modules: list of str
:returns: sphinx code
:rtype: str
>>> toc([])
''
>>> print(toc(['foo', 'bar']))
<BLANKLINE>
.. toctree::
:maxdepth: 2
<BLANKLINE>
bar
foo
<BLANKLINE>
<BLANKLINE>
"""
modules.sort()
if not modules:
return ''
contents = '\n'.join([' ' + m for m in modules])
return TOC % contents
def write(filename, module, children):
"""Write sphinx code for auto documented module to a file
:param filename: filename for the sphinx code
:type filename: str
:param module: name of the module
:type module: str
:param children: package modules
:type children: list of str
"""
f = open(filename, 'w')
f.write(title(module.split('.')[-1]) + auto(module) + toc(children))
f.close()
def update(py_root, rst_root='source', not_overwrite=['index.rst'],
index='index2'):
"""Generate sphinx rst files from python source files.
:param py_root: root folder of python source files
:type py_root: str
:param rst_root: root folder of rst source files
:type rst_root: str
:param not_overwrite: list of rst files which can't be overwritten
:type not_overwrite: list of str
:param index: base filename for the rst index
:type index: str
"""
def get_rst_file(module):
return os.path.join(rst_root, module + '.rst')
for f in [f for f in glob.glob(os.path.join(rst_root, '*.rst'))
if not os.path.basename(f) in not_overwrite]:
os.remove(f)
root = py_root
n = len(root)
for parent, dirs, files in os.walk(root):
modules = []
rel_path = parent[n:].strip(os.path.sep)
def get_module(path, append=True):
"""Transforms a path in a modulename and its rst location.
"""
if append:
parent = rel_path
else:
parent = ''
module = os.path.join(parent, os.path.splitext(path)[0])\
.replace(os.path.sep, '.')
if append:
modules.append(module)
rst_file = get_rst_file(module)
return module, rst_file
#add subpackage dirs
for d in dirs:
module, rst_file = get_module(d)
#add module files
if '__init__.py' in files:
files.remove('__init__.py')
for file in files:
if not (file.endswith('.py') and rel_path):
continue
module, rst_file = get_module(file)
write(rst_file, module, [])
# is this the index or the root module?
if rel_path:
module, rst_file = get_module(rel_path, append=False)
else:
module = index
rst_file = get_rst_file(module)
write(rst_file, module, modules)
def switch_colors(css, colors):
#open
f = open(css, 'rb')
source = f.read()
f.close()
#switch
for color_couple in colors:
source = source.replace(*color_couple)
#close
f = open(css, 'wb')
f.write(source)
f.close()
def main(py_root, rst_root, not_overwrite, index, colors):
"""Generate sphinx rst files and html documentation.
:param py_root: root folder of python source files
:type py_root: str
:param rst_root: root folder of rst source files
:type rst_root: str
:param not_overwrite: list of rst files which can't be overwritten
:type not_overwrite: list of str
:param index: base filename for the rst index
:type index: str
"""
update(py_root, rst_root, not_overwrite, index)
call('make html', shell=True)
switch_colors(os.path.join('build', 'html', '_static', 'default.css'),
colors)
switch_colors(os.path.join('build', 'html', '_static', 'pygments.css'),
colors)
if __name__ == '__main__':
main(
py_root=os.path.abspath('../phatch'),
rst_root='source',
not_overwrite=['index.rst', 'bazaar.rst', 'pep8.rst', 'lico.rst',
'testing.rst', 'release.rst'],
index='index2',
colors=(
('background-color: #eee;',
'background-color: #252527; color: #ffa;'),
('background-color: #ffe4e4;',
'background-color: #252527; color: #ffe4e4;'),
('#ecf0f3', '#1f1f21'),
('#303030', '#eee'),
('#f2f2f2', '#000'),
)
)
#update(py_root)
|