source: createpluginscript/anyrelease/create_trac_plugin/create_component.py

Last change on this file was 3945, checked in by Jeff Hammel, 15 years ago

initial import of the script

File size: 3.8 KB
Line 
1#!/usr/bin/env python
2"""
3script to create a trac component skeleton file
4"""
5
6# TODO:  genericize this
7# much of this has nothing to do with trac and could be abstracted
8# to its own package
9
10# TODO: ifaces should be a global;  it doesn't change per run
11
12import inspect
13import sys
14from cStringIO import StringIO
15from trac.core import *
16
17# interfaces live in these modules
18import trac.admin
19import trac.attachment
20import trac.db
21import trac.env
22import trac.mimeview
23import trac.perm
24import trac.prefs
25import trac.resource
26import trac.search
27import trac.ticket
28import trac.timeline
29import trac.versioncontrol
30import trac.versioncontrol.web_ui
31import trac.web
32import trac.web.chrome
33import trac.wiki
34
35def trac_interfaces():
36    """return a dictionary of the trac interfaces:
37    { class.__name__: class }
38    """
39
40    retval = {}
41    for interface in Interface.__subclasses__():
42        retval[interface.__name__] = interface
43    return retval
44
45def check_interfaces(*interfaces):
46    ifaces = trac_interfaces()
47    if not set(interfaces).issubset(ifaces.keys()):
48        # XXX redundant?
49        return set(interfaces).difference(ifaces.keys())
50
51
52def print_interface(interface):   
53    """return a string of the methods given an interface"""
54    # XXX maybe this should use StringIO too?
55
56    # add a section marker
57    retval = [ '    ### methods for %s\n' % interface.__name__ ]
58
59    # add the docstring if it exists (useful?)
60    doc = interface.__doc__
61    if doc:
62        retval.append('    """%s"""\n' % doc)
63
64    # add the methods
65    args = lambda x: inspect.formatargspec(*inspect.getargspec(x))
66    for method_name in [ i for i in dir(interface) if not i.startswith('_') ]:
67        method = getattr(interface, method_name)
68        if not hasattr(method, '__call__'):
69            continue
70        argstring = args(method)[1:-1]
71        if argstring:
72            retval.append('    def %s(self, %s):' % (method_name, argstring))
73        else:
74            retval.append('    def %s(self):' % method_name)
75        doc = method.__doc__
76        if doc:
77            retval.append('        """%s"""\n' % doc)
78
79    # return the string
80    return '\n'.join(retval)
81
82def print_component(name, *interfaces):
83    """
84    prints the skeleton for a new component
85    given its name and the interfaces it uses
86    """
87   
88    # get the interfaces
89    ifaces = trac_interfaces()
90    keys = sorted(ifaces.keys())
91   
92    # check to ensure that the interfaces given actually exist
93    # if not, exit with an error
94    nonexistant = check_interfaces(*interfaces)
95    if nonexistant:
96        print "Error: interfaces specified not found: " + ', '.join(nonexistant)
97        sys.exit(1)
98
99    # print the docstring and core import
100    retval = StringIO()
101    print >> retval, '''"""
102%s:
103a plugin for Trac
104http://trac.edgewall.org
105"""
106
107from trac.core import *
108''' % name
109
110    # print the imports necessary for the interfaces
111    for i in interfaces:
112        print >> retval, 'from %s import %s' % (ifaces[i].__module__, i)
113
114    # print class declaration
115    print >> retval, '\nclass %s(Component):\n'  % name
116    print >> retval, '    implements(%s)\n' % ', '.join(interfaces)
117
118    # print the methods needed by the class for its interfaces
119    for i in interfaces:
120        print >> retval, print_interface(ifaces[i])
121
122    return retval.getvalue()
123
124def parse_args():
125    if len(sys.argv) < 2:
126        import os
127        progname = os.path.split(sys.argv[0])[-1]
128        ifaces = trac_interfaces()
129        print """Usage:
130 %s <name> [interface1] [interface2] [...] # to create a component with name
131 %s # for usage and component list
132
133Interfaces available:
134""" % (progname, progname)
135        for key in sorted(ifaces.keys()):
136            print key
137        sys.exit(0)
138
139def main():
140    parse_args()
141    print print_component(*sys.argv[1:])
142
143if __name__ == '__main__':
144    main()
145   
Note: See TracBrowser for help on using the repository browser.