|
1 #!/usr/bin/env python3 |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 # Copyright (c) 2003 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
5 # |
|
6 |
|
7 """ |
|
8 Eric4 Documentation Generator |
|
9 |
|
10 This is the main Python script of the documentation generator. It is |
|
11 this script that gets called via the source documentation interface. |
|
12 This script can be used via the commandline as well. |
|
13 """ |
|
14 |
|
15 import glob |
|
16 import os |
|
17 import sys |
|
18 import shutil |
|
19 import fnmatch |
|
20 |
|
21 import Utilities.ModuleParser |
|
22 from DocumentationTools.ModuleDocumentor import ModuleDocument |
|
23 from DocumentationTools.IndexGenerator import IndexGenerator |
|
24 from DocumentationTools.QtHelpGenerator import QtHelpGenerator |
|
25 from DocumentationTools.Config import eric5docDefaultColors |
|
26 from UI.Info import Version |
|
27 import Utilities |
|
28 import Preferences |
|
29 |
|
30 # list of supported filename extensions |
|
31 supportedExtensions = [".py", ".pyw", ".ptl", ".rb"] |
|
32 |
|
33 def usage(): |
|
34 """ |
|
35 Function to print some usage information. |
|
36 |
|
37 It prints a reference of all commandline parameters that may |
|
38 be used and ends the application. |
|
39 """ |
|
40 print("eric5-doc") |
|
41 print() |
|
42 print("Copyright (c) 2003 - 2010 Detlev Offenbach <detlev@die-offenbachs.de>.") |
|
43 print() |
|
44 print("Usage:") |
|
45 print() |
|
46 print(" eric5-doc [options] files...") |
|
47 print() |
|
48 print("where files can be either python modules, package") |
|
49 print("directories or ordinary directories.") |
|
50 print() |
|
51 print("Options:") |
|
52 print() |
|
53 print(" -c filename or --style-sheet=filename") |
|
54 print(" Specify a CSS style sheet file to be used.") |
|
55 print(" -e or --noempty") |
|
56 print(" Don't include empty modules.") |
|
57 print(" -h or --help") |
|
58 print(" Show this help and exit.") |
|
59 print(" -i or --noindex") |
|
60 print(" Don't generate index files.") |
|
61 print(" -o directory or --outdir=directory") |
|
62 print(" Generate files in the named directory.") |
|
63 print(" -R, -r or --recursive") |
|
64 print(" Perform a recursive search for Python files.") |
|
65 print(" -t ext or --extension=ext") |
|
66 print(" Add the given extension to the list of file extensions.") |
|
67 print(" This option may be given multiple times.") |
|
68 print(" -V or --version") |
|
69 print(" Show version information and exit.") |
|
70 print(" -x directory or --exclude=directory") |
|
71 print(" Specify a directory basename to be excluded.") |
|
72 print(" This option may be repeated multiple times.") |
|
73 print(" --exclude-file=pattern") |
|
74 print(" Specify a filename pattern of files to be excluded.") |
|
75 print(" This option may be repeated multiple times.") |
|
76 print() |
|
77 print(" --body-color=color") |
|
78 print(" Specify the text color.") |
|
79 print(" --body-background-color=color") |
|
80 print(" Specify the text background color.") |
|
81 print(" --l1header-color=color") |
|
82 print(" Specify the text color of level 1 headers.") |
|
83 print(" --l1header-background-color=color") |
|
84 print(" Specify the text background color of level 1 headers.") |
|
85 print(" --l2header-color=color") |
|
86 print(" Specify the text color of level 2 headers.") |
|
87 print(" --l2header-background-color=color") |
|
88 print(" Specify the text background color of level 2 headers.") |
|
89 print(" --cfheader-color=color") |
|
90 print(" Specify the text color of class and function headers.") |
|
91 print(" --cfheader-background-color=color") |
|
92 print(" Specify the text background color of class and function headers.") |
|
93 print(" --link-color=color") |
|
94 print(" Specify the text color of hyperlinks.") |
|
95 print() |
|
96 print(" --create-qhp") |
|
97 print(" Enable generation of QtHelp files.") |
|
98 print(" --qhp-outdir=directory") |
|
99 print(" Generate QtHelp files in the named directory.") |
|
100 print(" --qhp-namespace=namespace") |
|
101 print(" Use the given namespace (mandatory).") |
|
102 print(" --qhp-virtualfolder=folder") |
|
103 print(" Use the given virtual folder (mandatory).") |
|
104 print(" The virtual folder must not contain '/'.") |
|
105 print(" --qhp-filtername=name") |
|
106 print(" Use the given name for the custom filter.") |
|
107 print(" --qhp-filterattribs=attributes") |
|
108 print(" Add the given attributes to the filter list.") |
|
109 print(" Attributes must be separated by ':'.") |
|
110 print(" --qhp-title=title") |
|
111 print(" Use this as the title for the generated help (mandatory).") |
|
112 print(" --create-qhc") |
|
113 print(" Enable generation of QtHelp Collection files.") |
|
114 sys.exit(1) |
|
115 |
|
116 def version(): |
|
117 """ |
|
118 Function to show the version information. |
|
119 """ |
|
120 print("""eric5-doc %s |
|
121 |
|
122 Eric4 API documentation generator. |
|
123 |
|
124 Copyright (c) 2003-2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
125 This is free software; see the LICENSE.GPL3 for copying conditions. |
|
126 There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A |
|
127 PARTICULAR PURPOSE.""" % Version) |
|
128 sys.exit(1) |
|
129 |
|
130 def main(): |
|
131 """ |
|
132 Main entry point into the application. |
|
133 """ |
|
134 |
|
135 import getopt |
|
136 |
|
137 try: |
|
138 opts, args = getopt.getopt(sys.argv[1:], "c:ehio:Rrt:Vx:", |
|
139 ["exclude=", "extension=", "help", "noindex", "noempty", "outdir=", |
|
140 "recursive", "style-sheet=", "version", |
|
141 "exclude-file=", |
|
142 "body-color=", "body-background-color=", |
|
143 "l1header-color=", "l1header-background-color=", |
|
144 "l2header-color=", "l2header-background-color=", |
|
145 "cfheader-color=", "cfheader-background-color=", |
|
146 "link-color=", |
|
147 "create-qhp", "qhp-outdir=", "qhp-namespace=", |
|
148 "qhp-virtualfolder=", "qhp-filtername=", "qhp-filterattribs=", |
|
149 "qhp-title=", "create-qhc", |
|
150 ]) |
|
151 except getopt.error as e: |
|
152 usage() |
|
153 |
|
154 excludeDirs = ["CVS", ".svn", "_svn", ".ropeproject", "_ropeproject", |
|
155 ".eric5project", "_eric5project", "dist", "build", "doc", "docs"] |
|
156 excludePatterns = [] |
|
157 outputDir = "doc" |
|
158 recursive = False |
|
159 doIndex = True |
|
160 noempty = False |
|
161 stylesheetFile = "" |
|
162 colors = eric5docDefaultColors.copy() |
|
163 |
|
164 qtHelpCreation = False |
|
165 qtHelpOutputDir = "help" |
|
166 qtHelpNamespace = "" |
|
167 qtHelpFolder = "source" |
|
168 qtHelpFilterName = "unknown" |
|
169 qtHelpFilterAttribs = "" |
|
170 qtHelpTitle = "" |
|
171 qtHelpCreateCollection = False |
|
172 |
|
173 # Set the applications string encoding |
|
174 try: |
|
175 sys.setappdefaultencoding(str(Preferences.getSystem("StringEncoding"))) |
|
176 except AttributeError: |
|
177 pass |
|
178 |
|
179 for k, v in opts: |
|
180 if k in ["-o", "--outdir"]: |
|
181 outputDir = v |
|
182 elif k in ["-R", "-r", "--recursive"]: |
|
183 recursive = True |
|
184 elif k in ["-x", "--exclude"]: |
|
185 excludeDirs.append(v) |
|
186 elif k == "--exclude-file": |
|
187 excludePatterns.append(v) |
|
188 elif k in ["-i", "--noindex"]: |
|
189 doIndex = False |
|
190 elif k in ["-e", "--noempty"]: |
|
191 noempty = True |
|
192 elif k in ["-h", "--help"]: |
|
193 usage() |
|
194 elif k in ["-V", "--version"]: |
|
195 version() |
|
196 elif k in ["-c", "--style-sheet"]: |
|
197 stylesheetFile = v |
|
198 elif k in ["-t", "--extension"]: |
|
199 if not v.startswith("."): |
|
200 v = ".%s" % v |
|
201 supportedExtensions.append(v) |
|
202 |
|
203 elif k == "--body-color": |
|
204 colors['BodyColor'] = v |
|
205 elif k == "--body-background-color": |
|
206 colors['BodyBgColor'] = v |
|
207 elif k == "--l1header-color": |
|
208 colors['Level1HeaderColor'] = v |
|
209 elif k == "--l1header-background-color": |
|
210 colors['Level1HeaderBgColor'] = v |
|
211 elif k == "--l2header-color": |
|
212 colors['Level2HeaderColor'] = v |
|
213 elif k == "--l2header-background-color": |
|
214 colors['Level2HeaderBgColor'] = v |
|
215 elif k == "--cfheader-color": |
|
216 colors['CFColor'] = v |
|
217 elif k == "--cfheader-background-color": |
|
218 colors['CFBgColor'] = v |
|
219 elif k == "--link-color": |
|
220 colors['LinkColor'] = v |
|
221 |
|
222 elif k == "--create-qhp": |
|
223 qtHelpCreation = True |
|
224 elif k == "--qhp-outdir": |
|
225 qtHelpOutputDir = v |
|
226 elif k == "--qhp-namespace": |
|
227 qtHelpNamespace = v |
|
228 elif k == "--qhp-virtualfolder": |
|
229 qtHelpFolder = v |
|
230 elif k == "--qhp-filtername": |
|
231 qtHelpFilterName = v |
|
232 elif k == "--qhp-filterattribs": |
|
233 qtHelpFilterAttribs = v |
|
234 elif k == "--qhp-title": |
|
235 qtHelpTitle = v |
|
236 elif k == "--create-qhc": |
|
237 qtHelpCreateCollection = True |
|
238 |
|
239 if not args: |
|
240 usage() |
|
241 |
|
242 if qtHelpCreation and \ |
|
243 (qtHelpNamespace == "" or \ |
|
244 qtHelpFolder == "" or '/' in qtHelpFolder or \ |
|
245 qtHelpTitle == ""): |
|
246 usage() |
|
247 |
|
248 input = output = 0 |
|
249 basename = "" |
|
250 |
|
251 if outputDir: |
|
252 if not os.path.isdir(outputDir): |
|
253 try: |
|
254 os.makedirs(outputDir) |
|
255 except EnvironmentError: |
|
256 sys.stderr.write("Could not create output directory %s." % outputDir) |
|
257 sys.exit(2) |
|
258 else: |
|
259 outputDir = os.getcwd() |
|
260 outputDir = os.path.abspath(outputDir) |
|
261 |
|
262 if stylesheetFile: |
|
263 try: |
|
264 sf = open(stylesheetFile, "rb") |
|
265 stylesheet = sf.read() |
|
266 sf.close() |
|
267 except IOError: |
|
268 sys.stderr.write("The CSS stylesheet '%s' does not exist\n" % stylesheetFile) |
|
269 sys.stderr.write("Disabling CSS usage.\n") |
|
270 stylesheet = None |
|
271 else: |
|
272 stylesheet = None |
|
273 |
|
274 indexGenerator = IndexGenerator(outputDir, colors, stylesheet) |
|
275 |
|
276 if qtHelpCreation: |
|
277 if qtHelpOutputDir: |
|
278 if not os.path.isdir(qtHelpOutputDir): |
|
279 try: |
|
280 os.makedirs(qtHelpOutputDir) |
|
281 except EnvironmentError: |
|
282 sys.stderr.write("Could not create QtHelp output directory %s." % \ |
|
283 qtHelpOutputDir) |
|
284 sys.exit(2) |
|
285 else: |
|
286 qtHelpOutputDir = os.getcwd() |
|
287 qtHelpOutputDir = os.path.abspath(qtHelpOutputDir) |
|
288 |
|
289 qtHelpGenerator = QtHelpGenerator(outputDir, |
|
290 qtHelpOutputDir, qtHelpNamespace, qtHelpFolder, |
|
291 qtHelpFilterName, qtHelpFilterAttribs, |
|
292 qtHelpTitle, qtHelpCreateCollection) |
|
293 |
|
294 for arg in args: |
|
295 if os.path.isdir(arg): |
|
296 if os.path.exists(os.path.join(arg, Utilities.joinext("__init__", ".py"))): |
|
297 basename = os.path.dirname(arg) |
|
298 if arg == '.': |
|
299 sys.stderr.write("The directory '.' is a package.\n") |
|
300 sys.stderr.write("Please repeat the call giving its real name.\n") |
|
301 sys.stderr.write("Ignoring the directory.\n") |
|
302 continue |
|
303 else: |
|
304 basename = arg |
|
305 if basename: |
|
306 basename = "%s%s" % (basename, os.sep) |
|
307 |
|
308 if recursive and not os.path.islink(arg): |
|
309 names = [arg] + Utilities.getDirs(arg, excludeDirs) |
|
310 else: |
|
311 names = [arg] |
|
312 else: |
|
313 basename = "" |
|
314 names = [arg] |
|
315 |
|
316 for filename in names: |
|
317 inpackage = False |
|
318 if os.path.isdir(filename): |
|
319 files = [] |
|
320 for ext in supportedExtensions: |
|
321 files.extend(glob.glob(os.path.join(filename, |
|
322 Utilities.joinext("*", ext)))) |
|
323 initFile = os.path.join(filename, Utilities.joinext("__init__", ext)) |
|
324 if initFile in files: |
|
325 inpackage = True |
|
326 files.remove(initFile) |
|
327 files.insert(0, initFile) |
|
328 else: |
|
329 if Utilities.isWindowsPlatform() and glob.has_magic(filename): |
|
330 files = glob.glob(filename) |
|
331 else: |
|
332 files = [filename] |
|
333 |
|
334 for file in files: |
|
335 skipIt = False |
|
336 for pattern in excludePatterns: |
|
337 if fnmatch.fnmatch(os.path.basename(file), pattern): |
|
338 skipIt = True |
|
339 break |
|
340 if skipIt: |
|
341 continue |
|
342 |
|
343 try: |
|
344 module = Utilities.ModuleParser.readModule(file, basename = basename, |
|
345 inpackage = inpackage, extensions = supportedExtensions) |
|
346 moduleDocument = ModuleDocument(module, colors, stylesheet) |
|
347 doc = moduleDocument.genDocument() |
|
348 except IOError as v: |
|
349 sys.stderr.write("%s error: %s\n" % (file, v[1])) |
|
350 continue |
|
351 except ImportError as v: |
|
352 sys.stderr.write("%s error: %s\n" % (file, v)) |
|
353 continue |
|
354 |
|
355 input = input + 1 |
|
356 |
|
357 f = Utilities.joinext(os.path.join(outputDir, moduleDocument.name()), |
|
358 ".html") |
|
359 |
|
360 # remember for index file generation |
|
361 indexGenerator.remember(file, moduleDocument, basename) |
|
362 |
|
363 # remember for QtHelp generation |
|
364 if qtHelpCreation: |
|
365 qtHelpGenerator.remember(file, moduleDocument, basename) |
|
366 |
|
367 if (noempty or file.endswith('__init__.py')) \ |
|
368 and moduleDocument.isEmpty(): |
|
369 continue |
|
370 |
|
371 # generate output |
|
372 try: |
|
373 out = open(f, "w") |
|
374 out.write(doc) |
|
375 out.close() |
|
376 except IOError as v: |
|
377 sys.stderr.write("%s error: %s\n" % (file, v[1])) |
|
378 else: |
|
379 sys.stdout.write("%s ok\n" % f) |
|
380 |
|
381 output = output + 1 |
|
382 sys.stdout.flush() |
|
383 sys.stderr.flush() |
|
384 |
|
385 # write index files |
|
386 if doIndex: |
|
387 indexGenerator.writeIndices(basename) |
|
388 |
|
389 # generate the QtHelp files |
|
390 if qtHelpCreation: |
|
391 qtHelpGenerator.generateFiles() |
|
392 |
|
393 sys.exit(0) |
|
394 |
|
395 if __name__ == '__main__': |
|
396 main() |