3 # Modulator - Generate skeleton modules.
5 # The user fills out some forms with information about what the module
6 # should support (methods, objects), names of these things, prefixes to
7 # use for C code, whether the objects should also support access as numbers,
9 # When the user presses 'Generate code' we generate a complete skeleton
12 # Alternatively, the selections made can be save to a python sourcefile and
13 # this sourcefile can be passed on the command line (resulting in the same
16 # Jack Jansen, CWI, October 1994.
21 sys
.path
.append(os
.path
.join(os
.environ
['HOME'],
22 'src/python/Tools/modulator'))
26 from ScrolledListbox
import ScrolledListbox
33 IDENTSTARTCHARS
= string
.letters
+ '_'
34 IDENTCHARS
= string
.letters
+ string
.digits
+ '_'
36 # Check that string is a legal C identifier
39 if not str[0] in IDENTSTARTCHARS
:
42 if not c
in IDENTCHARS
:
46 def getlistlist(list):
50 rv
.append(list.get(i
))
57 self
.main
.master
.title('Modulator: Module view')
58 self
.cmdframe
= Frame(self
.main
, {'relief':'raised', 'bd':'0.5m',
61 self
.objframe
= Frame(self
.main
, {Pack
:{'side':'top', 'fill':'x',
65 self
.check_button
= Button(self
.cmdframe
,
66 {'text':'Check', 'command':self
.cb_check
,
67 Pack
:{'side':'left', 'padx':'0.5m'}})
68 self
.save_button
= Button(self
.cmdframe
,
69 {'text':'Save...', 'command':self
.cb_save
,
70 Pack
:{'side':'left', 'padx':'0.5m'}})
71 self
.code_button
= Button(self
.cmdframe
,
72 {'text':'Generate code...',
73 'command':self
.cb_gencode
,
74 Pack
:{'side':'left', 'padx':'0.5m'}})
75 self
.quit_button
= Button(self
.cmdframe
,
77 'command':self
.cb_quit
,
78 Pack
:{'side':'right', 'padx':'0.5m'}})
80 self
.module
= UI_module(self
)
87 def cb_quit(self
, *args
):
89 if not askyn('You have not saved\nAre you sure you want to quit?'):
93 def cb_check(self
, *args
):
95 self
.module
.synchronize()
96 for o
in self
.objects
:
101 def cb_save(self
, *args
):
103 pycode
= self
.module
.gencode('m', self
.objects
)
107 fn
= askfile('Python file name: ')
116 def cb_gencode(self
, *args
):
118 pycode
= self
.module
.gencode('m', self
.objects
)
122 fn
= askfile('C file name: ')
131 message('An error occurred:-)')
133 genmodule
.write(fp
, m
)
137 def __init__(self
, parent
):
139 self
.frame
= Frame(parent
.objframe
, {'relief':'raised', 'bd':'0.2m',
142 self
.f1
= Frame(self
.frame
, {Pack
:{'side':'top', 'pady':'0.5m',
144 self
.f2
= Frame(self
.frame
, {Pack
:{'side':'top', 'pady':'0.5m',
146 self
.f3
= Frame(self
.frame
, {Pack
:{'side':'top', 'pady':'0.5m',
148 self
.f4
= Frame(self
.frame
, {Pack
:{'side':'top', 'pady':'0.5m',
151 self
.l1
= Label(self
.f1
, {'text':'Module:', Pack
:{'side':'left',
153 self
.name_entry
= Entry(self
.f1
, {'relief':'sunken',
154 Pack
:{'side':'left', 'padx':'0.5m', 'expand':1}})
155 self
.l2
= Label(self
.f1
, {'text':'Abbrev:', Pack
:{'side':'left',
157 self
.abbrev_entry
= Entry(self
.f1
, {'relief':'sunken', 'width':5,
158 Pack
:{'side':'left', 'padx':'0.5m'}})
160 self
.l3
= Label(self
.f2
, {'text':'Methods:', Pack
:{'side':'left',
162 self
.method_list
= ScrolledListbox(self
.f2
, {'relief':'sunken','bd':2,
163 Pack
:{'side':'left', 'expand':1,
164 'padx':'0.5m', 'fill':'both'}})
166 self
.l4
= Label(self
.f3
, {'text':'Add method:', Pack
:{'side':'left',
168 self
.method_entry
= Entry(self
.f3
, {'relief':'sunken',
169 Pack
:{'side':'left', 'padx':'0.5m', 'expand':1}})
170 self
.method_entry
.bind('<Return>', self
.cb_method
)
171 self
.delete_button
= Button(self
.f3
, {'text':'Delete method',
172 'command':self
.cb_delmethod
,
176 self
.newobj_button
= Button(self
.f4
, {'text':'new object',
177 'command':self
.cb_newobj
,
181 def cb_delmethod(self
, *args
):
182 list = self
.method_list
.curselection()
184 self
.method_list
.delete(i
)
186 def cb_newobj(self
, *arg
):
187 self
.parent
.objects
.append(UI_object(self
.parent
))
189 def cb_method(self
, *arg
):
190 name
= self
.method_entry
.get()
193 self
.method_entry
.delete('0', 'end')
194 self
.method_list
.insert('end', name
)
196 def synchronize(self
):
197 n
= self
.name_entry
.get()
199 message('Module name not set')
202 message('Module name not an identifier:\n'+n
)
204 if not self
.abbrev_entry
.get():
205 self
.abbrev_entry
.insert('end', n
)
206 m
= getlistlist(self
.method_list
)
209 message('Method name not an identifier:\n'+n
)
212 def gencode(self
, name
, objects
):
218 for i
in range(len(objects
)):
219 oname
= 'o%d' % (i
+1)
220 rv
= rv
+ objects
[i
].gencode(oname
)
222 rv
= rv
+ '%s = genmodule.module()\n' % (name
,)
223 rv
= rv
+ '%s.name = %r\n' % (name
, self
.name_entry
.get())
224 rv
= rv
+ '%s.abbrev = %r\n' % (name
, self
.abbrev_entry
.get())
225 rv
= rv
+ '%s.methodlist = %r\n' % (name
, getlistlist(self
.method_list
))
226 rv
= rv
+ '%s.objects = [%s]\n' % (name
, ','.join(onames
))
233 def __init__(self
, parent
):
236 object_number
= object_number
+ 1
237 self
.num
= object_number
238 self
.vpref
= 'o%r_' % self
.num
239 self
.frame
= Toplevel(parent
.objframe
)
241 self
.frame
.title('Modulator: object view')
242 # self.frame = Frame(parent.objframe, {'relief':'raised', 'bd':'0.2m',
243 # Pack:{'side':'top',
245 self
.f1
= Frame(self
.frame
, {Pack
:{'side':'top', 'pady':'0.5m',
247 self
.f2
= Frame(self
.frame
, {Pack
:{'side':'top', 'pady':'0.5m',
249 self
.f3
= Frame(self
.frame
, {Pack
:{'side':'top', 'pady':'0.5m',
251 self
.f4
= Frame(self
.frame
, {Pack
:{'side':'top', 'pady':'0.5m',
255 self
.l1
= Label(self
.f1
, {'text':'Object:', Pack
:{'side':'left',
257 self
.name_entry
= Entry(self
.f1
, {'relief':'sunken',
258 Pack
:{'side':'left', 'padx':'0.5m', 'expand':1}})
259 self
.l2
= Label(self
.f1
, {'text':'Abbrev:', Pack
:{'side':'left',
261 self
.abbrev_entry
= Entry(self
.f1
, {'relief':'sunken', 'width':5,
262 Pack
:{'side':'left', 'padx':'0.5m'}})
264 self
.l3
= Label(self
.f2
, {'text':'Methods:', Pack
:{'side':'left',
266 self
.method_list
= ScrolledListbox(self
.f2
, {'relief':'sunken','bd':2,
267 Pack
:{'side':'left', 'expand':1,
268 'padx':'0.5m', 'fill':'both'}})
270 self
.l4
= Label(self
.f3
, {'text':'Add method:', Pack
:{'side':'left',
272 self
.method_entry
= Entry(self
.f3
, {'relief':'sunken',
273 Pack
:{'side':'left', 'padx':'0.5m', 'expand':1}})
274 self
.method_entry
.bind('<Return>', self
.cb_method
)
275 self
.delete_button
= Button(self
.f3
, {'text':'Delete method',
276 'command':self
.cb_delmethod
,
281 self
.l5
= Label(self
.f4
, {'text':'functions:',
284 self
.f5
= Frame(self
.f4
, {Pack
:{'side':'left', 'pady':'0.5m',
286 self
.l6
= Label(self
.f4
, {'text':'Types:',
287 Pack
:{'side':'left', 'padx':'0.5m'}})
288 self
.f6
= Frame(self
.f4
, {Pack
:{'side':'left', 'pady':'0.5m',
291 for i
in genmodule
.FUNCLIST
:
293 self
.f5
.setvar(vname
, 0)
294 b
= Checkbutton(self
.f5
, {'variable':vname
, 'text':i
,
295 Pack
:{'side':'top', 'pady':'0.5m',
296 'anchor':'w','expand':1}})
298 self
.f5
.setvar(self
.vpref
+'new', 1)
301 for i
in genmodule
.TYPELIST
:
302 vname
= self
.vpref
+ i
303 self
.f6
.setvar(vname
, 0)
304 b
= Checkbutton(self
.f6
, {'variable':vname
, 'text':i
,
305 Pack
:{'side':'top', 'pady':'0.5m',
309 def cb_method(self
, *arg
):
310 name
= self
.method_entry
.get()
313 self
.method_entry
.delete('0', 'end')
314 self
.method_list
.insert('end', name
)
316 def cb_delmethod(self
, *args
):
317 list = self
.method_list
.curselection()
319 self
.method_list
.delete(i
)
321 def synchronize(self
):
322 n
= self
.name_entry
.get()
324 message('Object name not set')
326 if not self
.abbrev_entry
.get():
327 self
.abbrev_entry
.insert('end', n
)
328 n
= self
.abbrev_entry
.get()
330 message('Abbreviation not an identifier:\n'+n
)
332 m
= getlistlist(self
.method_list
)
335 message('Method name not an identifier:\n'+n
)
338 self
.f5
.setvar(self
.vpref
+'tp_getattr', 1)
341 def gencode(self
, name
):
343 rv
= rv
+ '%s = genmodule.object()\n' % (name
,)
344 rv
= rv
+ '%s.name = %r\n' % (name
, self
.name_entry
.get())
345 rv
= rv
+ '%s.abbrev = %r\n' % (name
, self
.abbrev_entry
.get())
346 rv
= rv
+ '%s.methodlist = %r\n' % (name
, getlistlist(self
.method_list
))
348 for fn
in genmodule
.FUNCLIST
:
349 vname
= self
.vpref
+ fn
350 if self
.f5
.getvar(vname
) == '1':
352 rv
= rv
+ '%s.funclist = %r\n' % (name
, fl
)
355 for fn
in genmodule
.TYPELIST
:
356 vname
= self
.vpref
+ fn
357 if self
.f5
.getvar(vname
) == '1':
360 rv
= rv
+ '%s.typelist = %r\n' % (name
, fl
)
367 if len(sys
.argv
) < 2:
370 elif len(sys
.argv
) == 2:
371 fp
= open(sys
.argv
[1])
376 sys
.stderr
.write('An error occurred:-)\n')
378 ##genmodule.write(sys.stdout, m)
380 sys
.stderr
.write('Usage: modulator [file]\n')