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