forked from tommct/neuronpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_ref_builder.py
166 lines (150 loc) · 6.67 KB
/
auto_ref_builder.py
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
# -*- coding: utf-8 -*-
"""
Script to build the ".rst" files from the source code for the reference documentation.
To use::
python auto_ref_builder.py
Then cd to ``doc`` and::
make html
or::
make latexpdf
"""
# While this software is under the permissive MIT License,
# (http://www.opensource.org/licenses/mit-license.php)
# We ask that you cite the neuronpy package (or tools used in this package)
# in any publications and contact the author with your referenced publication.
#
# Format:
# McTavish, T.S. NeuronPy library, version 0.1, http://bitbucket.org/tommctavish/neuronpy
#
# Copyright (c) 2010 Thomas S. McTavish
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os, imp
import inspect
# Set the directory where .rst files will go.
(file, pathname, description) = imp.find_module('auto_ref_builder')
project_path = os.path.dirname(os.path.abspath(pathname))
build_path=os.path.join(project_path, 'doc', 'source')
# Remove old ".rst" files, but save index and about.
os.chdir(build_path)
try:
os.rename('index.rst', 'index.save')
except:
pass
try:
os.rename('about.rst', 'about.save')
except:
pass
try:
os.system("rm *.rst")
except:
pass
try:
os.rename('index.save', 'index.rst')
except:
pass
try:
os.rename('about.save', 'about.rst')
except:
pass
src_path = os.path.join(project_path, 'src')
def write_api_file():
## Write the api.rst file
filepath = os.path.join(build_path, 'api.rst')
with open(filepath, 'w') as api_file:
api_file.write('.. _api:\n\n')
api_file.write('################\n')
api_file.write('Source Reference\n')
api_file.write('################\n')
api_file.write('.. index::\n')
api_file.write(' single: Source Reference\n\n')
api_file.write('This is the reference documentation for the source code ')
api_file.write('of the neuronpy library.\n\n')
api_file.write('.. toctree::\n :maxdepth: 2\n\n')
for modulename in globals()["__all__"]:
api_file.write(' ' + modulename + '\n')
# Walk the code sources.
for (dirpath, dirnames, filenames) in os.walk(src_path):
parent_dir = os.path.split(os.path.abspath(dirpath))[-1]
for filename in filenames:
if filename == '__init__.py':
# look for our autoref_rst directives in __init.py__
try:
execfile(os.path.join(dirpath,'__init__.py'), globals())
if 'autoref_rst' in globals():
filepath = os.path.join(build_path, parent_dir + '.rst')
with open(filepath, 'w') as fout:
#
# fout=open(build_path + '/' + parent_dir + '.rst', 'w')
# try:
print "Writing", filepath
fout.write(globals().pop('autoref_rst')) # Pop to remove from globals
fout.write('\n.. The first part of this file came from __init__.py.\n')
fout.write('\n.. The remainder is automatically generated by auto_ref_builder.py.\n')
fout.write('\n')
fout.write('.. toctree::\n\n')
for f in filenames:
if f.endswith('.py') and not(f.startswith('__')):
prefix = f[:-3]
fout.write(' ' + prefix + '\n')
# except:
# pass
# fout.close()
if '__all__' in globals():
if parent_dir == 'src':
write_api_file()
for prefix in globals()["__all__"]:
print "prefix =", prefix
if os.path.isdir(os.path.join(dirpath,prefix)): # Don't process directories.
continue
rst_filename = prefix + '.rst'
if os.path.exists(os.path.join(build_path, rst_filename)):
continue
fout=open(os.path.join(build_path, rst_filename), 'w')
try:
print 'writing %s' %prefix
module = __import__('neuronpy.%s'%parent_dir, \
globals(), locals(), ['%s'%prefix], -1)
obj = getattr(module, prefix)
found = False
for i in inspect.getmembers(obj):
if i[0].lower()==prefix:
fout.write(i[0]+'\n')
found = True
break
if found == False:
fout.write(prefix+'\n')
for i in range(len(prefix)):
fout.write('-')
fout.write('\n\n')
fout.write('.. This file has been automatically generated ')
fout.write('by auto_ref_builder.py\n\n')
fout.write('.. automodule :: neuronpy.%s.%s\n' % \
(parent_dir,prefix))
fout.write(' :members:\n\n')
except:
print "could not generate doc for {0}".format(filename)
pass
fout.close()
else:
print "NO __all__ key"
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
raise
break