-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patheric6_api.py
317 lines (284 loc) · 11.3 KB
/
eric6_api.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2003 - 2016 Detlev Offenbach <[email protected]>
#
"""
Eric6 API Generator.
This is the main Python script of the API generator. It is
this script that gets called via the API generation interface.
This script can be used via the commandline as well.
"""
from __future__ import unicode_literals
import Toolbox.PyQt4ImportHook # __IGNORE_WARNING__
try: # Only for Py2
import Globals.compatibility_fixes # __IGNORE_WARNING__
except (ImportError):
pass
import glob
import os
import sys
import fnmatch
import Utilities.ModuleParser
from DocumentationTools.APIGenerator import APIGenerator
from UI.Info import Version
import Utilities
import DocumentationTools
def usage():
"""
Function to print some usage information.
It prints a reference of all commandline parameters that may
be used and ends the application.
"""
print("eric6_api")
print()
print("Copyright (c) 2004 - 2016 Detlev Offenbach"
" <[email protected]>.")
print()
print("Usage:")
print()
print(" eric6_api [options] files...")
print()
print("where files can be either python modules, package")
print("directories or ordinary directories.")
print()
print("Options:")
print()
print(" -b name or --base=name")
print(" Use the given name as the name of the base package.")
print(" -e eol-type or --eol=eol-type")
print(" Use the given eol type to terminate lines.")
print(" Valid values are 'cr', 'lf' and 'crlf'.")
print(" --exclude-file=pattern")
print(" Specify a filename pattern of files to be excluded.")
print(" This option may be repeated multiple times.")
print(" -h or --help")
print(" Show this help and exit.")
print(" -l language or --language=language")
print(" Generate an API file for the given programming language.")
print(" Supported programming languages are:")
for lang in sorted(
DocumentationTools.supportedExtensionsDictForApis.keys()):
print(" * {0}".format(lang))
print(" The default is 'Python3'.")
print(" This option may be repeated multiple times.")
print(" -o filename or --output=filename")
print(" Write the API information to the named file."
" A '%L' placeholder") # __IGNORE_WARNING__
print(" is replaced by the language of the API file"
" (see --language).")
print(" -p or --private")
print(" Include private methods and functions.")
print(" -R, -r or --recursive")
print(" Perform a recursive search for source files.")
print(" -t ext or --extension=ext")
print(" Add the given extension to the list of file extensions.")
print(" This option may be given multiple times.")
print(" -V or --version")
print(" Show version information and exit.")
print(" -x directory or --exclude=directory")
print(" Specify a directory basename to be excluded.")
print(" This option may be repeated multiple times.")
sys.exit(1)
def version():
"""
Function to show the version information.
"""
print(
"""eric6_api {0}\n"""
"""\n"""
"""Pymakr API generator.\n"""
"""\n"""
"""Copyright (c) 2004 - 2016 Detlev Offenbach"""
""" <[email protected]>\n"""
"""This is free software; see the LICENSE.GPL3 for copying"""
""" conditions.\n"""
"""There is NO warranty; not even for MERCHANTABILITY or FITNESS"""
""" FOR A\n"""
"""PARTICULAR PURPOSE.""".format(Version))
sys.exit(1)
def main():
"""
Main entry point into the application.
"""
global supportedExtensions
import getopt
try:
opts, args = getopt.getopt(
sys.argv[1:], "b:e:hl:o:pRrt:Vx:",
["base=", "eol=", "exclude=", "exclude-file=", "extension=",
"help", "language=", "output=", "private", "recursive",
"version", ])
except getopt.error:
usage()
excludeDirs = ["CVS", ".svn", "_svn",
".ropeproject", "_ropeproject",
".eric5project", "_eric5project",
".eric6project", "_eric6project",
"dist", "build", "doc", "docs"
]
excludePatterns = []
outputFileName = ""
recursive = False
basePackage = ""
includePrivate = False
progLanguages = []
extensions = []
newline = None
for k, v in opts:
if k in ["-o", "--output"]:
outputFileName = v
elif k in ["-R", "-r", "--recursive"]:
recursive = True
elif k in ["-x", "--exclude"]:
excludeDirs.append(v)
elif k == "--exclude-file":
excludePatterns.append(v)
elif k in ["-h", "--help"]:
usage()
elif k in ["-V", "--version"]:
version()
elif k in ["-t", "--extension"]:
if not v.startswith("."):
v = ".{0}".format(v)
extensions.append(v)
elif k in ["-b", "--base"]:
basePackage = v
elif k in ["-p", "--private"]:
includePrivate = True
elif k in ["-l", "--language"]:
if v not in progLanguages:
if v not in \
DocumentationTools.supportedExtensionsDictForApis:
sys.stderr.write(
"Wrong language given: {0}. Aborting\n".format(v))
sys.exit(1)
else:
progLanguages.append(v)
elif k in ["-e", "--eol"]:
if v.lower() == "cr":
newline = '\r'
elif v.lower() == "lf":
newline = '\n'
elif v.lower() == "crlf":
newline = '\r\n'
if not args:
usage()
if outputFileName == "":
sys.stderr.write("No output file given. Aborting\n")
sys.exit(1)
if len(progLanguages) == 0:
progLanguages = ["Python3"]
for progLanguage in sorted(progLanguages):
basename = ""
apis = []
basesDict = {}
supportedExtensions = \
DocumentationTools.supportedExtensionsDictForApis[progLanguage]
supportedExtensions.extend(extensions)
if "%L" in outputFileName:
outputFile = outputFileName.replace("%L", progLanguage)
else:
if len(progLanguages) == 1:
outputFile = outputFileName
else:
root, ext = os.path.splitext(outputFileName)
outputFile = "{0}-{1}{2}".format(root, progLanguage.lower(),
ext)
basesFile = os.path.splitext(outputFile)[0] + '.bas'
for arg in args:
if os.path.isdir(arg):
if os.path.exists(os.path.join(
arg, Utilities.joinext("__init__", ".py"))):
basename = os.path.dirname(arg)
if arg == '.':
sys.stderr.write("The directory '.' is a package.\n")
sys.stderr.write(
"Please repeat the call giving its real name.\n")
sys.stderr.write("Ignoring the directory.\n")
continue
else:
basename = arg
if basename:
basename = "{0}{1}".format(basename, os.sep)
if recursive and not os.path.islink(arg):
names = [arg] + Utilities.getDirs(arg, excludeDirs)
else:
names = [arg]
else:
basename = ""
names = [arg]
for filename in sorted(names):
inpackage = False
if os.path.isdir(filename):
files = []
for ext in supportedExtensions:
files.extend(glob.glob(os.path.join(
filename, Utilities.joinext("*", ext))))
initFile = os.path.join(
filename, Utilities.joinext("__init__", ext))
if initFile in files:
inpackage = True
files.remove(initFile)
files.insert(0, initFile)
elif progLanguage != "Python3":
# assume package
inpackage = True
else:
if Utilities.isWindowsPlatform() and \
glob.has_magic(filename):
files = glob.glob(filename)
else:
files = [filename]
for file in files:
skipIt = False
for pattern in excludePatterns:
if fnmatch.fnmatch(os.path.basename(file), pattern):
skipIt = True
break
if skipIt:
continue
try:
module = Utilities.ModuleParser.readModule(
file,
basename=basename, inpackage=inpackage)
apiGenerator = APIGenerator(module)
api = apiGenerator.genAPI(True, basePackage,
includePrivate)
bases = apiGenerator.genBases(includePrivate)
except IOError as v:
sys.stderr.write("{0} error: {1}\n".format(file, v[1]))
continue
except ImportError as v:
sys.stderr.write("{0} error: {1}\n".format(file, v))
continue
for apiEntry in api:
if apiEntry not in apis:
apis.append(apiEntry)
for basesEntry in bases:
if bases[basesEntry]:
basesDict[basesEntry] = bases[basesEntry][:]
sys.stdout.write("-- {0} -- {1} ok\n".format(
progLanguage, file))
outdir = os.path.dirname(outputFile)
if outdir and not os.path.exists(outdir):
os.makedirs(outdir)
try:
out = open(outputFile, "w", encoding="utf-8", newline=newline)
out.write("\n".join(sorted(apis)) + "\n")
out.close()
except IOError as v:
sys.stderr.write("{0} error: {1}\n".format(outputFile, v[1]))
sys.exit(3)
try:
out = open(basesFile, "w", encoding="utf-8", newline=newline)
for baseEntry in sorted(basesDict.keys()):
out.write("{0} {1}\n".format(
baseEntry, " ".join(sorted(basesDict[baseEntry]))))
out.close()
except IOError as v:
sys.stderr.write("{0} error: {1}\n".format(basesFile, v[1]))
sys.exit(3)
sys.stdout.write('\nDone.\n')
sys.exit(0)
if __name__ == '__main__':
main()