blob: e6608f8053c5ee01e748e02a724934dd6f14066c [file] [log] [blame]
Guido van Rossumb00649c1991-12-11 17:29:59 +00001#
2# flp - Module to load fl forms from fd files
3#
4# Jack Jansen, December 1991
5#
6import string
7import path
8import sys
Guido van Rossumb00649c1991-12-11 17:29:59 +00009import FL
10
11SPLITLINE = '--------------------'
12FORMLINE = '=============== FORM ==============='
13ENDLINE = '=============================='
14
15error = 'flp.error'
16
17##################################################################
18# Part 1 - The parsing routines #
19##################################################################
20
21#
22# Externally visible function. Load form.
23#
24def parse_form(filename, formname):
Guido van Rossum465c4991992-02-19 14:50:10 +000025 forms = checkcache(filename)
26 if forms != None:
27 if forms.has_key(formname):
28 return forms[formname]
29 else:
30 forms = {}
Guido van Rossumb00649c1991-12-11 17:29:59 +000031 fp = _open_formfile(filename)
32 nforms = _parse_fd_header(fp)
33 for i in range(nforms):
34 form = _parse_fd_form(fp, formname)
35 if form <> None:
Guido van Rossum465c4991992-02-19 14:50:10 +000036 break
37 else:
38 raise error, 'No such form in fd file'
39 forms[formname] = form
40 writecache(filename, forms)
41 return form
Guido van Rossumb00649c1991-12-11 17:29:59 +000042
43#
44# Externally visible function. Load all forms.
45#
46def parse_forms(filename):
Guido van Rossum465c4991992-02-19 14:50:10 +000047 forms = checkcache(filename)
48 if forms != None: return forms
Guido van Rossumb00649c1991-12-11 17:29:59 +000049 fp = _open_formfile(filename)
50 nforms = _parse_fd_header(fp)
51 forms = {}
52 for i in range(nforms):
53 form = _parse_fd_form(fp, None)
54 forms[form[0].Name] = form
Guido van Rossum465c4991992-02-19 14:50:10 +000055 writecache(filename, forms)
Guido van Rossumb00649c1991-12-11 17:29:59 +000056 return forms
Guido van Rossum465c4991992-02-19 14:50:10 +000057
58#
59# Internal: see if a cached version of the file exists
60#
61MAGIC = '.fdc'
62def checkcache(filename):
63 import marshal
64 fp, filename = _open_formfile2(filename)
65 fp.close()
66 cachename = filename + 'c'
67 try:
68 fp = open(cachename, 'r')
69 except IOError:
Guido van Rossum24e77d41992-03-25 14:53:05 +000070 #print 'flp: no cache file', cachename
Guido van Rossum465c4991992-02-19 14:50:10 +000071 return None
72 try:
73 if fp.read(4) != MAGIC:
74 print 'flp: bad magic word in cache file', cachename
75 return None
76 cache_mtime = rdlong(fp)
77 file_mtime = getmtime(filename)
78 if cache_mtime != file_mtime:
Guido van Rossum24e77d41992-03-25 14:53:05 +000079 #print 'flp: outdated cache file', cachename
Guido van Rossum465c4991992-02-19 14:50:10 +000080 return None
Guido van Rossum24e77d41992-03-25 14:53:05 +000081 #print 'flp: valid cache file', cachename
Guido van Rossum465c4991992-02-19 14:50:10 +000082 altforms = marshal.load(fp)
83 forms = {}
84 for name in altforms.keys():
85 altobj, altlist = altforms[name]
86 obj = _newobj().init()
87 obj.make(altobj)
88 list = []
89 for altobj in altlist:
90 nobj = _newobj().init()
91 nobj.make(altobj)
92 list.append(nobj)
93 forms[name] = obj, list
94 return forms
95 finally:
96 fp.close()
97
98def rdlong(fp):
99 s = fp.read(4)
100 if len(s) != 4: return None
101 a, b, c, d = s[0], s[1], s[2], s[3]
102 return ord(a)<<24 | ord(b)<<16 | ord(c)<<8 | ord(d)
103
104def wrlong(fp, x):
105 a, b, c, d = (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff
106 fp.write(chr(a) + chr(b) + chr(c) + chr(d))
107
108def getmtime(filename):
109 import posix
110 from stat import ST_MTIME
111 try:
112 return posix.stat(filename)[ST_MTIME]
113 except posix.error:
114 return None
115
116#
117# Internal: write cached version of the form (parsing is too slow!)
118#
119def writecache(filename, forms):
120 import marshal
121 fp, filename = _open_formfile2(filename)
122 fp.close()
123 cachename = filename + 'c'
124 try:
125 fp = open(cachename, 'w')
126 except IOError:
127 print 'flp: can\'t create cache file', cachename
128 return # Never mind
129 fp.write('\0\0\0\0') # Seek back and write MAGIC when done
130 wrlong(fp, getmtime(filename))
131 altforms = {}
132 for name in forms.keys():
133 obj, list = forms[name]
134 altobj = obj.__dict__
135 altlist = []
136 for obj in list: altlist.append(obj.__dict__)
137 altforms[name] = altobj, altlist
138 marshal.dump(altforms, fp)
139 fp.seek(0)
140 fp.write(MAGIC)
141 fp.close()
Guido van Rossum24e77d41992-03-25 14:53:05 +0000142 #print 'flp: wrote cache file', cachename
Guido van Rossumb00649c1991-12-11 17:29:59 +0000143
144#
145# Internal: Locate form file (using PYTHONPATH) and open file
146#
147def _open_formfile(filename):
Guido van Rossum465c4991992-02-19 14:50:10 +0000148 return _open_formfile2(filename)[0]
149
150def _open_formfile2(filename):
Guido van Rossumb00649c1991-12-11 17:29:59 +0000151 if filename[-3:] <> '.fd':
152 filename = filename + '.fd'
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000153 if filename[0] == '/':
Guido van Rossumb00649c1991-12-11 17:29:59 +0000154 try:
155 fp = open(filename,'r')
Guido van Rossumb00649c1991-12-11 17:29:59 +0000156 except IOError:
157 fp = None
158 else:
159 for pc in sys.path:
160 pn = path.join(pc, filename)
161 try:
162 fp = open(pn, 'r')
Guido van Rossum465c4991992-02-19 14:50:10 +0000163 filename = pn
Guido van Rossumb00649c1991-12-11 17:29:59 +0000164 break
Guido van Rossumb00649c1991-12-11 17:29:59 +0000165 except IOError:
166 fp = None
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000167 if fp == None:
Guido van Rossumb00649c1991-12-11 17:29:59 +0000168 raise error, 'Cannot find forms file ' + filename
Guido van Rossum465c4991992-02-19 14:50:10 +0000169 return fp, filename
Guido van Rossumb00649c1991-12-11 17:29:59 +0000170
171#
172# Internal: parse the fd file header, return number of forms
173#
174def _parse_fd_header(file):
175 # First read the magic header line
176 datum = _parse_1_line(file)
177 if datum <> ('Magic', 12321):
178 raise error, 'Not a forms definition file'
179 # Now skip until we know number of forms
180 while 1:
181 datum = _parse_1_line(file)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000182 if type(datum) == type(()) and datum[0] == 'Numberofforms':
Guido van Rossumb00649c1991-12-11 17:29:59 +0000183 break
184 return datum[1]
185#
186# Internal: parse fd form, or skip if name doesn't match.
187# the special value None means 'allways parse it'.
188#
189def _parse_fd_form(file, name):
190 datum = _parse_1_line(file)
191 if datum <> FORMLINE:
192 raise error, 'Missing === FORM === line'
193 form = _parse_object(file)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000194 if form.Name == name or name == None:
Guido van Rossumb00649c1991-12-11 17:29:59 +0000195 objs = []
196 for j in range(form.Numberofobjects):
197 obj = _parse_object(file)
198 objs.append(obj)
199 return (form, objs)
200 else:
201 for j in range(form.Numberofobjects):
202 _skip_object(file)
203 return None
204
205#
206# Internal class: a convient place to store object info fields
207#
Guido van Rossum869100a1991-12-26 13:03:39 +0000208class _newobj:
Guido van Rossumb00649c1991-12-11 17:29:59 +0000209 def init(self):
210 return self
211 def add(self, (name, value)):
Guido van Rossum465c4991992-02-19 14:50:10 +0000212 self.__dict__[name] = value
213 def make(self, dict):
214 for name in dict.keys():
215 self.add(name, dict[name])
Guido van Rossumb00649c1991-12-11 17:29:59 +0000216
217#
218# Internal parsing routines.
219#
220def _parse_string(str):
Guido van Rossum24e77d41992-03-25 14:53:05 +0000221 if '\\' in str:
222 s = '\'' + str + '\''
223 try:
224 return eval(s)
225 except:
226 pass
Guido van Rossumbefa2931991-12-16 13:10:14 +0000227 return str
Guido van Rossumb00649c1991-12-11 17:29:59 +0000228
229def _parse_num(str):
Guido van Rossumbefa2931991-12-16 13:10:14 +0000230 return eval(str)
Guido van Rossumb00649c1991-12-11 17:29:59 +0000231
232def _parse_numlist(str):
233 slist = string.split(str)
234 nlist = []
235 for i in slist:
236 nlist.append(_parse_num(i))
237 return nlist
238
239# This dictionary maps item names to parsing routines.
240# If no routine is given '_parse_num' is default.
241_parse_func = { \
242 'Name': _parse_string, \
243 'Box': _parse_numlist, \
244 'Colors': _parse_numlist, \
245 'Label': _parse_string, \
246 'Name': _parse_string, \
247 'Callback': _parse_string, \
248 'Argument': _parse_string }
249
250# This function parses a line, and returns either
251# a string or a tuple (name,value)
252
Guido van Rossumbefa2931991-12-16 13:10:14 +0000253import regexp
254
Guido van Rossumb00649c1991-12-11 17:29:59 +0000255def _parse_line(line):
Guido van Rossumbefa2931991-12-16 13:10:14 +0000256 a = regexp.match('^([^:]*): *(.*)', line)
257 if not a:
258 return line
259 name = line[:a[1][1]]
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000260 if name[0] == 'N':
Guido van Rossumbefa2931991-12-16 13:10:14 +0000261 name = string.joinfields(string.split(name),'')
262 name = string.lower(name)
Guido van Rossumb00649c1991-12-11 17:29:59 +0000263 name = string.upper(name[0]) + name[1:]
Guido van Rossumbefa2931991-12-16 13:10:14 +0000264 value = line[a[2][0]:]
Guido van Rossumb00649c1991-12-11 17:29:59 +0000265 try:
266 pf = _parse_func[name]
Guido van Rossumb00649c1991-12-11 17:29:59 +0000267 except KeyError:
268 pf = _parse_num
269 value = pf(value)
270 return (name, value)
271
272def _readline(file):
273 line = file.readline()
Guido van Rossumbefa2931991-12-16 13:10:14 +0000274 if not line:
275 raise EOFError
276 return line[:-1]
Guido van Rossumb00649c1991-12-11 17:29:59 +0000277
278def _parse_1_line(file):
Guido van Rossumbefa2931991-12-16 13:10:14 +0000279 line = _readline(file)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000280 while line == '':
Guido van Rossumb00649c1991-12-11 17:29:59 +0000281 line = _readline(file)
282 return _parse_line(line)
283
284def _skip_object(file):
285 line = ''
286 while not line in (SPLITLINE, FORMLINE, ENDLINE):
287 pos = file.tell()
288 line = _readline(file)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000289 if line == FORMLINE:
Guido van Rossumb00649c1991-12-11 17:29:59 +0000290 file.seek(pos)
291
292def _parse_object(file):
293 obj = _newobj().init()
294 while 1:
295 pos = file.tell()
296 datum = _parse_1_line(file)
297 if datum in (SPLITLINE, FORMLINE, ENDLINE):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000298 if datum == FORMLINE:
Guido van Rossumb00649c1991-12-11 17:29:59 +0000299 file.seek(pos)
300 return obj
301 if type(datum) <> type(()):
302 raise error, 'Parse error, illegal line in object: '+datum
303 obj.add(datum)
304
305#################################################################
306# Part 2 - High-level object/form creation routines #
307#################################################################
308
309#
310# External - Create a form an link to an instance variable.
311#
312def create_full_form(inst, (fdata, odatalist)):
313 form = create_form(fdata)
314 exec('inst.'+fdata.Name+' = form\n')
315 for odata in odatalist:
316 create_object_instance(inst, form, odata)
317
318#
319# External - Merge a form into an existing form in an instance
320# variable.
321#
322def merge_full_form(inst, form, (fdata, odatalist)):
323 exec('inst.'+fdata.Name+' = form\n')
324 if odatalist[0].Class <> FL.BOX:
325 raise error, 'merge_full_form() expects FL.BOX as first obj'
326 for odata in odatalist[1:]:
327 create_object_instance(inst, form, odata)
328
329
330#################################################################
331# Part 3 - Low-level object/form creation routines #
332#################################################################
333
334#
335# External Create_form - Create form from parameters
336#
337def create_form(fdata):
Guido van Rossum465c4991992-02-19 14:50:10 +0000338 import fl
339 return fl.make_form(FL.NO_BOX, fdata.Width, fdata.Height)
Guido van Rossumb00649c1991-12-11 17:29:59 +0000340
341#
342# External create_object - Create an object. Make sure there are
343# no callbacks. Returns the object created.
344#
345def create_object(form, odata):
346 obj = _create_object(form, odata)
347 if odata.Callback:
348 raise error, 'Creating free object with callback'
349 return obj
350#
351# External create_object_instance - Create object in an instance.
352#
353def create_object_instance(inst, form, odata):
354 obj = _create_object(form, odata)
355 if odata.Callback:
356 cbfunc = eval('inst.'+odata.Callback)
357 obj.set_call_back(cbfunc, odata.Argument)
358 if odata.Name:
359 exec('inst.' + odata.Name + ' = obj\n')
360#
361# Internal _create_object: Create the object and fill options
362#
363def _create_object(form, odata):
364 crfunc = _select_crfunc(form, odata.Class)
365 obj = crfunc(odata.Type, odata.Box[0], odata.Box[1], odata.Box[2], \
366 odata.Box[3], odata.Label)
367 if not odata.Class in (FL.BEGIN_GROUP, FL.END_GROUP):
368 obj.boxtype = odata.Boxtype
369 obj.col1 = odata.Colors[0]
370 obj.col2 = odata.Colors[1]
371 obj.align = odata.Alignment
372 obj.lstyle = odata.Style
373 obj.lsize = odata.Size
374 obj.lcol = odata.Lcol
375 return obj
376#
377# Internal crfunc: helper function that returns correct create function
378#
379def _select_crfunc(fm, cl):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000380 if cl == FL.BEGIN_GROUP: return fm.bgn_group
381 elif cl == FL.END_GROUP: return fm.end_group
382 elif cl == FL.BITMAP: return fm.add_bitmap
383 elif cl == FL.BOX: return fm.add_box
384 elif cl == FL.BROWSER: return fm.add_browser
385 elif cl == FL.BUTTON: return fm.add_button
386 elif cl == FL.CHART: return fm.add_chart
387 elif cl == FL.CHOICE: return fm.add_choice
388 elif cl == FL.CLOCK: return fm.add_clock
389 elif cl == FL.COUNTER: return fm.add_counter
390 elif cl == FL.DIAL: return fm.add_dial
391 elif cl == FL.FREE: return fm.add_free
392 elif cl == FL.INPUT: return fm.add_input
393 elif cl == FL.LIGHTBUTTON: return fm.add_lightbutton
394 elif cl == FL.MENU: return fm.add_menu
395 elif cl == FL.POSITIONER: return fm.add_positioner
396 elif cl == FL.ROUNDBUTTON: return fm.add_roundbutton
397 elif cl == FL.SLIDER: return fm.add_slider
398 elif cl == FL.VALSLIDER: return fm.add_valslider
399 elif cl == FL.TEXT: return fm.add_text
400 elif cl == FL.TIMER: return fm.add_timer
Guido van Rossumb00649c1991-12-11 17:29:59 +0000401 else:
402 raise error, 'Unknown object type: ' + `cl`
403
404
405def test():
Guido van Rossum465c4991992-02-19 14:50:10 +0000406 import time
407 t0 = time.millitimer()
Guido van Rossumb00649c1991-12-11 17:29:59 +0000408 if len(sys.argv) == 2:
409 forms = parse_forms(sys.argv[1])
Guido van Rossum465c4991992-02-19 14:50:10 +0000410 t1 = time.millitimer()
411 print 'parse time:', 0.001*(t1-t0), 'sec.'
412 keys = forms.keys()
413 keys.sort()
414 for i in keys:
Guido van Rossumb00649c1991-12-11 17:29:59 +0000415 _printform(forms[i])
416 elif len(sys.argv) == 3:
417 form = parse_form(sys.argv[1], sys.argv[2])
Guido van Rossum465c4991992-02-19 14:50:10 +0000418 t1 = time.millitimer()
419 print 'parse time:', 0.001*(t1-t0), 'sec.'
Guido van Rossumb00649c1991-12-11 17:29:59 +0000420 _printform(form)
421 else:
422 print 'Usage: test fdfile [form]'
423
424def _printform(form):
425 f = form[0]
426 objs = form[1]
427 print 'Form ', f.Name, ', size: ', f.Width, f.Height, ' Nobj ', f.Numberofobjects
428 for i in objs:
429 print ' Obj ', i.Name, ' type ', i.Class, i.Type
430 print ' Box ', i.Box, ' btype ', i.Boxtype
431 print ' Label ', i.Label, ' size/style/col/align ', i.Size,i.Style, i.Lcol, i.Alignment
432 print ' cols ', i.Colors
433 print ' cback ', i.Callback, i.Argument
Guido van Rossum465c4991992-02-19 14:50:10 +0000434
435# Local variables:
436# py-indent-offset: 4
437# end: