root/createpluginscript/anyrelease/create_trac_plugin/create_component.py

Revision 3945, 3.8 kB (checked in by k0s, 6 months ago)

initial import of the script

  • Property svn:executable set to
Line 
1 #!/usr/bin/env python
2 """
3 script 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
12 import inspect
13 import sys
14 from cStringIO import StringIO
15 from trac.core import *
16
17 # interfaces live in these modules
18 import trac.admin
19 import trac.attachment
20 import trac.db
21 import trac.env
22 import trac.mimeview
23 import trac.perm
24 import trac.prefs
25 import trac.resource
26 import trac.search
27 import trac.ticket
28 import trac.timeline
29 import trac.versioncontrol
30 import trac.versioncontrol.web_ui
31 import trac.web
32 import trac.web.chrome
33 import trac.wiki
34
35 def 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
45 def 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
52 def 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
82 def 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:
103 a plugin for Trac
104 http://trac.edgewall.org
105 """
106
107 from 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
124 def 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
133 Interfaces available:
134 """ % (progname, progname)
135         for key in sorted(ifaces.keys()):
136             print key
137         sys.exit(0)
138
139 def main():
140     parse_args()
141     print print_component(*sys.argv[1:])
142
143 if __name__ == '__main__':
144     main()
145    
Note: See TracBrowser for help on using the browser.