-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmakedoc.py
120 lines (97 loc) · 4.41 KB
/
makedoc.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
#!/usr/bin/env python
"""
Generate epydoc documentation of IvS repository and insert images.
The documentation will be stored in the subdirectory "doc/html" of the IvS root
directory (i.e. where this file is located). If previously generated
documentation exists, it will be overwriten.
The main page of the documentation can be found in "doc/html/index.html".
Updating the documentation is a simple as rerunning the script, you do not need
to delete the results from the previous run.
For more command-line options, see
$:> python makedoc.py -h
"""
# -- import necessary modules
import os
import subprocess
import shutil
import glob
import webbrowser
import argparse
from ivs.aux import termtools
# -- skip documentation generation of the following third party modules:
skip = ['lmfit']
# -- do you want to immediately show the contents in your default web browser
# each time the documentation is generated
# (False, 'current tab' or 'new tab')?
parser = argparse.ArgumentParser(
description='Build IvS Python repository documentation')
parser.add_argument('-o', '--open', action='store_true',
help='Open documentation in webbrowser after creation')
parser.add_argument('-v', '--verbose', action='store_true',
help='Increase output verbosity in Epydoc')
parser.add_argument('-w', '--width', type=int, default=75,
help='Width of images (percentage)')
args = parser.parse_args()
# -- remember the current working directory; you can generate the documentation
# in any directory if you really want to.
this_dir = os.path.dirname(os.path.abspath(__file__))
# -- collect all files and directories for which to generate documentation
output = subprocess.check_output('git ls-files', shell=True).decode()
alldirs = output.strip().split('\n')
# -- we do a quick selection here since we don't want to generate documentation
# of third-party modules that are installed alongside the repository
alldirs = [ff for ff in alldirs if os.path.splitext(ff)[1] == '.py'
and not os.path.basename(ff)[:4] == 'test']
for iskip in skip:
alldirs = [ff for ff in alldirs if iskip not in ff]
# -- build documentation
cmd = 'epydoc --html '+" ".join(alldirs) + ' -o doc/html --parse-only' + \
' --graph all {}'.format('-v' if args.verbose else '')
print("Building documentation using the command:\n {}".format(cmd))
flag = subprocess.call(cmd, shell=True)
# -- check if all went well
if flag:
print("Could not execute command, do you have epydoc installed?")
raise SystemExit
# -- Epydoc cannot insert images in the HTML code itself, we do this manually.
# for this, we look into the HTML code and replace all ]include figure]
# occurrences with the HTML code for image insertion. We copy the html file
# to a temporary copy, change the html code, save it and replace the old file
if not os.path.isdir('doc/images'):
print("You don't have an image directory; images are not inserted")
raise SystemExit
# -- now run through all. We need to explicitly treat line breaks.
files = sorted(glob.glob('doc/html/*module.html'))
files += sorted(glob.glob('doc/html/*class.html'))
for myfile in files:
shutil.move(myfile, myfile+'_')
ff = open(myfile+'_', 'r')
oo = open(myfile, 'w')
line_break = False
for line in ff.readlines():
if ']include figure]' in line or line_break:
filename = line.split(']')[-2].strip()
oo.write(r"<center><img src='../images/{}' alt='[image example]'" +
"width='{}%'/></center>".format(filename,
args.width)+'\n\n')
# -- save cursor, remove line, print and reset cursor
message = 'Added image {} to {}\r'.format(filename, myfile)
termtools.overwrite_line(message)
oo.write('\n\n')
line_break = False
elif ']include' in line:
line_break = True
else:
oo.write(line)
ff.close()
oo.close()
os.remove(myfile+'_')
# -- that's it! Open the documentation if requested
if os.path.isfile(os.path.join(this_dir, 'doc/html/index.html')):
print("HTML documentation is now available in doc/html")
else:
print("Something went wrong during documentation generation")
raise SystemExit
if args.open:
print("Documentation will be opened in your default webbrowser")
webbrowser.open_new_tab('doc/html/index.html')