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)
25 error
= 'genmodule.error'
28 # Names of functions in the object-description struct.
30 FUNCLIST
= ['new', 'tp_dealloc', 'tp_print', 'tp_getattr', 'tp_setattr',
31 'tp_compare', 'tp_repr', 'tp_hash', 'tp_call', 'tp_str']
32 TYPELIST
= ['tp_as_number', 'tp_as_sequence', 'tp_as_mapping', 'structure']
35 # writer is a base class for the object and module classes
36 # it contains code common to both.
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
__)
49 self
._subst
= subst
.subst
51 def addcode(self
, name
, fp
):
52 ifp
= self
.opentemplate(name
)
58 def opentemplate(self
, name
):
60 fn
= os
.path
.join(p
, name
)
61 if os
.path
.exists(fn
):
63 fn
= os
.path
.join(p
, 'Templates')
64 fn
= os
.path
.join(fn
, name
)
65 if os
.path
.exists(fn
):
67 raise error
, 'Template '+name
+' not found for '+self
._type
+' '+ \
73 def writecode(self
, fp
):
74 self
.addcode('copyright', fp
)
75 self
.addcode('module_head', fp
)
76 for o
in self
.objects
:
78 for o
in self
.objects
:
81 for fn
in self
.methodlist
:
83 self
.addcode('module_method', fp
)
85 '{"%s",\t(PyCFunction)%s_%s,\tMETH_VARARGS,\t%s_%s__doc__},\n'
86 %(fn
, self
.abbrev
, fn
, self
.abbrev
, fn
))
87 self
.methodlist
= new_ml
88 self
.addcode('module_tail', fp
)
95 self
.funclist
= ['new']
98 def writecode(self
, fp
):
99 self
.addcode('copyright', fp
)
103 def writehead(self
, fp
):
104 self
.addcode('object_head', fp
)
106 def writebody(self
, fp
):
108 for fn
in self
.methodlist
:
110 self
.addcode('object_method', fp
)
112 '{"%s",\t(PyCFunction)%s_%s,\tMETH_VARARGS,\t%s_%s__doc__},\n'
113 %(fn
, self
.abbrev
, fn
, self
.abbrev
, fn
))
114 self
.methodlist
= new_ml
115 self
.addcode('object_mlist', fp
)
117 # Add getattr if we have methods
118 if self
.methodlist
and not 'tp_getattr' in self
.funclist
:
119 self
.funclist
.insert(0, 'tp_getattr')
122 setattr(self
, fn
, '0')
125 # Special case for structure-access objects: put getattr in the
126 # list of functions but don't generate code for it directly,
127 # the code is obtained from the object_structure template.
128 # The same goes for setattr.
130 if 'structure' in self
.typelist
:
131 if 'tp_getattr' in self
.funclist
:
132 self
.funclist
.remove('tp_getattr')
133 if 'tp_setattr' in self
.funclist
:
134 self
.funclist
.remove('tp_setattr')
135 self
.tp_getattr
= self
.abbrev
+ '_getattr'
136 self
.tp_setattr
= self
.abbrev
+ '_setattr'
137 for fn
in self
.funclist
:
138 self
.addcode('object_'+fn
, fp
)
139 setattr(self
, fn
, '%s_%s'%(self
.abbrev
, fn
[3:]))
141 setattr(self
, tn
, '0')
142 for tn
in self
.typelist
:
143 self
.addcode('object_'+tn
, fp
)
144 setattr(self
, tn
, '&%s_%s'%(self
.abbrev
, tn
[3:]))
145 self
.addcode('object_tail', fp
)
150 if __name__
== '__main__':
152 o
.name
= 'dwarve object'
154 o
.funclist
= ['new', 'tp_dealloc']
155 o
.methodlist
= ['dig']
159 m
.methodlist
= ['newdwarve']