blob: 1c4cd1f3914677110f4a8a36bb04eccf4a38b962 [file] [log] [blame]
Guido van Rossumd2112201995-03-02 14:05:29 +00001#
2# Genmodule - A python program to help you build (template) modules.
3#
4# Usage:
5#
6# o = genmodule.object()
7# o.name = 'dwarve object'
8# o.abbrev = 'dw'
9# o.funclist = ['new', 'dealloc', 'getattr', 'setattr']
10# o.methodlist = ['dig']
11#
12# m = genmodule.module()
13# m.name = 'beings'
14# m.abbrev = 'be'
15# m.methodlist = ['newdwarve']
16# m.objects = [o]
17#
18# genmodule.write(sys.stdout, m)
19#
20import sys
21import os
22import varsubst
23import string
24
25error = 'genmodule.error'
26
27#
28# Names of functions in the object-description struct.
29#
30FUNCLIST = ['new', 'tp_dealloc', 'tp_print', 'tp_getattr', 'tp_setattr',
31 'tp_compare', 'tp_repr', 'tp_hash']
32TYPELIST = ['tp_as_number', 'tp_as_sequence', 'tp_as_mapping', 'structure']
33
34#
35# writer is a base class for the object and module classes
36# it contains code common to both.
37#
38class writer:
39 def __init__(self):
40 self._subst = None
41
42 def makesubst(self):
43 if not self._subst:
44 if not self.__dict__.has_key('abbrev'):
45 self.abbrev = self.name
46 self.Abbrev = string.upper(self.abbrev[0])+self.abbrev[1:]
47 subst = varsubst.Varsubst(self.__dict__)
48 subst.useindent(1)
49 self._subst = subst.subst
50
51 def addcode(self, name, fp):
52 ifp = self.opentemplate(name)
53 self.makesubst()
54 d = ifp.read()
55 d = self._subst(d)
56 fp.write(d)
57
58 def opentemplate(self, name):
59 for p in sys.path:
60 fn = os.path.join(p, name)
61 if os.path.exists(fn):
62 return open(fn, 'r')
63 fn = os.path.join(p, 'Templates')
64 fn = os.path.join(fn, name)
65 if os.path.exists(fn):
66 return open(fn, 'r')
67 raise error, 'Template '+name+' not found for '+self._type+' '+ \
68 self.name
69
70class module(writer):
71 _type = 'module'
72
73 def writecode(self, fp):
74 self.addcode('copyright', fp)
75 self.addcode('module_head', fp)
76 for o in self.objects:
77 o.writehead(fp)
78 for o in self.objects:
79 o.writebody(fp)
80 new_ml = ''
81 for fn in self.methodlist:
82 self.method = fn
83 self.addcode('module_method', fp)
84 new_ml = new_ml + ('{"%s",\t%s_%s,\t1},\n'%(fn, self.abbrev, fn))
85 self.methodlist = new_ml
86 self.addcode('module_tail', fp)
87
88class object(writer):
89 _type = 'object'
90 def __init__(self):
91 self.typelist = []
92 self.methodlist = []
93 self.funclist = ['new']
94 writer.__init__(self)
95
96 def writecode(self, fp):
97 self.addcode('copyright', fp)
98 self.writehead(fp)
99 self.writebody(fp)
100
101 def writehead(self, fp):
102 self.addcode('object_head', fp)
103
104 def writebody(self, fp):
105 new_ml = ''
106 for fn in self.methodlist:
107 self.method = fn
108 self.addcode('object_method', fp)
109 new_ml = new_ml + ('{"%s",\t%s_%s,\t1},\n'%(fn, self.abbrev, fn))
110 self.methodlist = new_ml
111 self.addcode('object_mlist', fp)
112
113 # Add getattr if we have methods
114 if self.methodlist and not 'tp_getattr' in self.funclist:
115 self.funclist.insert(0, 'tp_getattr')
116
117 for fn in FUNCLIST:
118 setattr(self, fn, '0')
119
120 #
121 # Special case for structure-access objects: put getattr in the
122 # list of functions but don't generate code for it directly,
123 # the code is obtained from the object_structure template.
124 # The same goes for setattr.
125 #
126 if 'structure' in self.typelist:
127 if 'tp_getattr' in self.funclist:
128 self.funclist.remove('tp_getattr')
129 if 'tp_setattr' in self.funclist:
130 self.funclist.remove('tp_setattr')
131 self.tp_getattr = self.abbrev + '_getattr'
132 self.tp_setattr = self.abbrev + '_setattr'
133 for fn in self.funclist:
134 self.addcode('object_'+fn, fp)
135 setattr(self, fn, '%s_%s'%(self.abbrev, fn[3:]))
136 for tn in TYPELIST:
137 setattr(self, tn, '0')
138 for tn in self.typelist:
139 self.addcode('object_'+tn, fp)
140 setattr(self, tn, '&%s_%s'%(self.abbrev, tn[3:]))
141 self.addcode('object_tail', fp)
142
143def write(fp, obj):
144 obj.writecode(fp)
145
146if __name__ == '__main__':
147 o = object()
148 o.name = 'dwarve object'
149 o.abbrev = 'dw'
150 o.funclist = ['new', 'tp_dealloc']
151 o.methodlist = ['dig']
152 m = module()
153 m.name = 'beings'
154 m.abbrev = 'be'
155 m.methodlist = ['newdwarve']
156 m.objects = [o]
157 write(sys.stdout, m)