File: eric3-api.py

package info (click to toggle)
eric 3.6.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,308 kB
  • ctags: 8,668
  • sloc: python: 52,713; sh: 265; makefile: 47
file content (176 lines) | stat: -rw-r--r-- 5,426 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2003 - 2005 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Eric3 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.
"""

import glob
import os
import sys

import Utilities.ModuleParser
from DocumentationTools.APIGenerator import APIGenerator
from UI.Info import Version
import Utilities

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 "eric3-api (c) 2004 by Detlev Offenbach <detlev@die-offenbachs.de>."
    print
    print "Usage:"
    print
    print "  eric3-api [options] files..."
    print
    print "where files can be either python modules, package"
    print "directories or ordinary directories."
    print
    print "Options:"
    print
    print "  -o filename or --output=filename"
    print "        Write the API information to the named file."
    print "  -R, -r or --recursive"
    print "        Perform a recursive search for Python files."
    print "  -x directory or --exclude=directory"
    print "        Specify a directory basename to be excluded."
    print "        This option may be repeated multiple times."
    print "  -V or --version"
    print "        Show version information and exit."
    print "  -h or --help"
    print "        Show this help and exit."
    sys.exit(1)

def version():
    """
    Function to show the version information.
    """
    print \
"""eric3-doc  %s

Eric3 API generator.

Copyright (c) 2004 - 2005 Detlev Offenbach <detlev@die-offenbachs.de>
This is free software; see the LICENSE.GPL for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.""" % Version
    sys.exit(1)

def main():
    """
    Main entry point into the application.
    """

    import getopt

    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:RrVx:",
            ["exclude=", "help", "output=", "recursive", "version"])
    except getopt.error:
        usage()

    excludeDirs = ["CVS", ".svn", "dist", "build", "doc", "docs"]
    outputFile = ""
    recursive = 0

    for k, v in opts:
        if k in ["-o", "--output"]:
            outputFile = v
        elif k in ["-R", "-r", "--recursive"]:
            recursive = 1
        elif k in ["-x", "--exclude"]:
            excludeDirs.append(v)
        elif k in ["-h", "--help"]:
            usage()
        elif k in ["-V", "--version"]:
            version()

    if not args:
        usage()

    basename = ""
    apis = []

    if outputFile == "":
        sys.stderr.write("No output file given. Aborting\n")
        sys.exit(1)
        
    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 = "%s%s" % (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 names:
    
            if os.path.isdir(filename):
                files = glob.glob(os.path.join(filename, Utilities.joinext("*", ".py"))) + \
                        glob.glob(os.path.join(filename, Utilities.joinext("*", ".ptl")))
                initFile = os.path.join(filename, Utilities.joinext("__init__", ".py"))
                if initFile in files:
                    files.remove(initFile)
                    files.insert(0, initFile)
            else:
                if sys.platform == "win32" and glob.has_magic(filename):
                    files = glob.glob(filename)
                else:
                    files = [filename]
    
            for file in files:
    
                try:
                    module = Utilities.ModuleParser.readModule(file, basename=basename)
                    apiGenerator = APIGenerator(module)
                    api = apiGenerator.genAPI()
                except IOError, v:
                    sys.stderr.write("%s error: %s\n" % (file, v[1]))
                    continue
                except ImportError, v:
                    sys.stderr.write("%s error: %s\n" % (file, v))
                    continue
                
                for apiEntry in api:
                    if not apiEntry in apis:
                        apis.append(apiEntry)
                sys.stdout.write("%s ok\n" % file)

    try:
        out = open(outputFile, "wb")
        out.write(os.linesep.join(apis))
        out.close()
    except IOError, v:
        sys.stderr.write("%s error: %s\n" % (outputFile, v[1]))
        sys.exit(3)
        
    sys.stdout.write('\nDone.\n')
    sys.exit(0)

if __name__ == '__main__':
    main()