2
# Genmodule - A python program to help you build (template) modules.
6
# o = genmodule.object()
7
# o.name = 'dwarve object'
9
# o.funclist = ['new', 'dealloc', 'getattr', 'setattr']
10
# o.methodlist = ['dig']
12
# m = genmodule.module()
15
# m.methodlist = ['newdwarve']
18
# genmodule.write(sys.stdout, m)
24
error = 'genmodule.error'
27
# Names of functions in the object-description struct.
29
FUNCLIST = ['new', 'tp_dealloc', 'tp_print', 'tp_getattr', 'tp_setattr',
30
'tp_compare', 'tp_repr', 'tp_hash', 'tp_call', 'tp_str']
31
TYPELIST = ['tp_as_number', 'tp_as_sequence', 'tp_as_mapping', 'structure']
34
# writer is a base class for the object and module classes
35
# it contains code common to both.
43
if not self.__dict__.has_key('abbrev'):
44
self.abbrev = self.name
45
self.Abbrev = self.abbrev[0].upper()+self.abbrev[1:]
46
subst = varsubst.Varsubst(self.__dict__)
48
self._subst = subst.subst
50
def addcode(self, name, fp):
51
ifp = self.opentemplate(name)
57
def opentemplate(self, name):
59
fn = os.path.join(p, name)
60
if os.path.exists(fn):
62
fn = os.path.join(p, 'Templates')
63
fn = os.path.join(fn, name)
64
if os.path.exists(fn):
66
raise error, 'Template '+name+' not found for '+self._type+' '+ \
72
def writecode(self, fp):
73
self.addcode('copyright', fp)
74
self.addcode('module_head', fp)
75
for o in self.objects:
77
for o in self.objects:
80
for fn in self.methodlist:
82
self.addcode('module_method', fp)
84
'{"%s",\t(PyCFunction)%s_%s,\tMETH_VARARGS,\t%s_%s__doc__},\n'
85
%(fn, self.abbrev, fn, self.abbrev, fn))
86
self.methodlist = new_ml
87
self.addcode('module_tail', fp)
94
self.funclist = ['new']
97
def writecode(self, fp):
98
self.addcode('copyright', fp)
102
def writehead(self, fp):
103
self.addcode('object_head', fp)
105
def writebody(self, fp):
107
for fn in self.methodlist:
109
self.addcode('object_method', fp)
111
'{"%s",\t(PyCFunction)%s_%s,\tMETH_VARARGS,\t%s_%s__doc__},\n'
112
%(fn, self.abbrev, fn, self.abbrev, fn))
113
self.methodlist = new_ml
114
self.addcode('object_mlist', fp)
116
# Add getattr if we have methods
117
if self.methodlist and not 'tp_getattr' in self.funclist:
118
self.funclist.insert(0, 'tp_getattr')
121
setattr(self, fn, '0')
124
# Special case for structure-access objects: put getattr in the
125
# list of functions but don't generate code for it directly,
126
# the code is obtained from the object_structure template.
127
# The same goes for setattr.
129
if 'structure' in self.typelist:
130
if 'tp_getattr' in self.funclist:
131
self.funclist.remove('tp_getattr')
132
if 'tp_setattr' in self.funclist:
133
self.funclist.remove('tp_setattr')
134
self.tp_getattr = self.abbrev + '_getattr'
135
self.tp_setattr = self.abbrev + '_setattr'
136
for fn in self.funclist:
137
self.addcode('object_'+fn, fp)
138
setattr(self, fn, '%s_%s'%(self.abbrev, fn[3:]))
140
setattr(self, tn, '0')
141
for tn in self.typelist:
142
self.addcode('object_'+tn, fp)
143
setattr(self, tn, '&%s_%s'%(self.abbrev, tn[3:]))
144
self.addcode('object_tail', fp)
149
if __name__ == '__main__':
151
o.name = 'dwarve object'
153
o.funclist = ['new', 'tp_dealloc']
154
o.methodlist = ['dig']
158
m.methodlist = ['newdwarve']