| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # flp - Module to load fl forms from fd files | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Jack Jansen, December 1991 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | import string | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import FL | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SPLITLINE = '--------------------' | 
					
						
							|  |  |  | FORMLINE = '=============== FORM ===============' | 
					
						
							|  |  |  | ENDLINE = '==============================' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error = 'flp.error' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ################################################################## | 
					
						
							|  |  |  | #    Part 1 - The parsing routines                               # | 
					
						
							|  |  |  | ################################################################## | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Externally visible function. Load form. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def parse_form(filename, formname): | 
					
						
							|  |  |  |     forms = checkcache(filename) | 
					
						
							|  |  |  |     if forms is None: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         forms = parse_forms(filename) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     if forms.has_key(formname): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         return forms[formname] | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         raise error, 'No such form in fd file' | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Externally visible function. Load all forms. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def parse_forms(filename): | 
					
						
							|  |  |  |     forms = checkcache(filename) | 
					
						
							|  |  |  |     if forms != None: return forms | 
					
						
							|  |  |  |     fp = _open_formfile(filename) | 
					
						
							|  |  |  |     nforms = _parse_fd_header(fp) | 
					
						
							|  |  |  |     forms = {} | 
					
						
							|  |  |  |     for i in range(nforms): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         form = _parse_fd_form(fp, None) | 
					
						
							|  |  |  |         forms[form[0].Name] = form | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     writecache(filename, forms) | 
					
						
							|  |  |  |     return forms | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal: see if a cached version of the file exists | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | MAGIC = '.fdc' | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  | _internal_cache = {}                    # Used by frozen scripts only | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | def checkcache(filename): | 
					
						
							|  |  |  |     if _internal_cache.has_key(filename): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         altforms = _internal_cache[filename] | 
					
						
							|  |  |  |         return _unpack_cache(altforms) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     import marshal | 
					
						
							|  |  |  |     fp, filename = _open_formfile2(filename) | 
					
						
							|  |  |  |     fp.close() | 
					
						
							|  |  |  |     cachename = filename + 'c' | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         fp = open(cachename, 'r') | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     except IOError: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         #print 'flp: no cache file', cachename | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         if fp.read(4) != MAGIC: | 
					
						
							|  |  |  |             print 'flp: bad magic word in cache file', cachename | 
					
						
							|  |  |  |             return None | 
					
						
							|  |  |  |         cache_mtime = rdlong(fp) | 
					
						
							|  |  |  |         file_mtime = getmtime(filename) | 
					
						
							|  |  |  |         if cache_mtime != file_mtime: | 
					
						
							|  |  |  |             #print 'flp: outdated cache file', cachename | 
					
						
							|  |  |  |             return None | 
					
						
							|  |  |  |         #print 'flp: valid cache file', cachename | 
					
						
							|  |  |  |         altforms = marshal.load(fp) | 
					
						
							|  |  |  |         return _unpack_cache(altforms) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     finally: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         fp.close() | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _unpack_cache(altforms): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         forms = {} | 
					
						
							|  |  |  |         for name in altforms.keys(): | 
					
						
							|  |  |  |             altobj, altlist = altforms[name] | 
					
						
							|  |  |  |             obj = _newobj() | 
					
						
							|  |  |  |             obj.make(altobj) | 
					
						
							|  |  |  |             list = [] | 
					
						
							|  |  |  |             for altobj in altlist: | 
					
						
							|  |  |  |                 nobj = _newobj() | 
					
						
							|  |  |  |                 nobj.make(altobj) | 
					
						
							|  |  |  |                 list.append(nobj) | 
					
						
							|  |  |  |             forms[name] = obj, list | 
					
						
							|  |  |  |         return forms | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def rdlong(fp): | 
					
						
							|  |  |  |     s = fp.read(4) | 
					
						
							|  |  |  |     if len(s) != 4: return None | 
					
						
							|  |  |  |     a, b, c, d = s[0], s[1], s[2], s[3] | 
					
						
							|  |  |  |     return ord(a)<<24 | ord(b)<<16 | ord(c)<<8 | ord(d) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def wrlong(fp, x): | 
					
						
							|  |  |  |     a, b, c, d = (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff | 
					
						
							|  |  |  |     fp.write(chr(a) + chr(b) + chr(c) + chr(d)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def getmtime(filename): | 
					
						
							|  |  |  |     import os | 
					
						
							|  |  |  |     from stat import ST_MTIME | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         return os.stat(filename)[ST_MTIME] | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     except os.error: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal: write cached version of the form (parsing is too slow!) | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def writecache(filename, forms): | 
					
						
							|  |  |  |     import marshal | 
					
						
							|  |  |  |     fp, filename = _open_formfile2(filename) | 
					
						
							|  |  |  |     fp.close() | 
					
						
							|  |  |  |     cachename = filename + 'c' | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         fp = open(cachename, 'w') | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     except IOError: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         print 'flp: can\'t create cache file', cachename | 
					
						
							|  |  |  |         return # Never mind | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     fp.write('\0\0\0\0') # Seek back and write MAGIC when done | 
					
						
							|  |  |  |     wrlong(fp, getmtime(filename)) | 
					
						
							|  |  |  |     altforms = _pack_cache(forms) | 
					
						
							|  |  |  |     marshal.dump(altforms, fp) | 
					
						
							|  |  |  |     fp.seek(0) | 
					
						
							|  |  |  |     fp.write(MAGIC) | 
					
						
							|  |  |  |     fp.close() | 
					
						
							|  |  |  |     #print 'flp: wrote cache file', cachename | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # External: print some statements that set up the internal cache. | 
					
						
							|  |  |  | # This is for use with the "freeze" script.  You should call | 
					
						
							|  |  |  | # flp.freeze(filename) for all forms used by the script, and collect | 
					
						
							|  |  |  | # the output on a file in a module file named "frozenforms.py".  Then | 
					
						
							|  |  |  | # in the main program of the script import frozenforms. | 
					
						
							|  |  |  | # (Don't forget to take this out when using the unfrozen version of | 
					
						
							|  |  |  | # the script!) | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def freeze(filename): | 
					
						
							|  |  |  |     forms = parse_forms(filename) | 
					
						
							|  |  |  |     altforms = _pack_cache(forms) | 
					
						
							|  |  |  |     print 'import flp' | 
					
						
							|  |  |  |     print 'flp._internal_cache[', `filename`, '] =', altforms | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal: create the data structure to be placed in the cache | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def _pack_cache(forms): | 
					
						
							|  |  |  |     altforms = {} | 
					
						
							|  |  |  |     for name in forms.keys(): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         obj, list = forms[name] | 
					
						
							|  |  |  |         altobj = obj.__dict__ | 
					
						
							|  |  |  |         altlist = [] | 
					
						
							|  |  |  |         for obj in list: altlist.append(obj.__dict__) | 
					
						
							|  |  |  |         altforms[name] = altobj, altlist | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return altforms | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal: Locate form file (using PYTHONPATH) and open file | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def _open_formfile(filename): | 
					
						
							|  |  |  |     return _open_formfile2(filename)[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _open_formfile2(filename): | 
					
						
							|  |  |  |     if filename[-3:] <> '.fd': | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         filename = filename + '.fd' | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     if filename[0] == '/': | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             fp = open(filename,'r') | 
					
						
							|  |  |  |         except IOError: | 
					
						
							|  |  |  |             fp = None | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         for pc in sys.path: | 
					
						
							|  |  |  |             pn = os.path.join(pc, filename) | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 fp = open(pn, 'r') | 
					
						
							|  |  |  |                 filename = pn | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             except IOError: | 
					
						
							|  |  |  |                 fp = None | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     if fp == None: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         raise error, 'Cannot find forms file ' + filename | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return fp, filename | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal: parse the fd file header, return number of forms | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def _parse_fd_header(file): | 
					
						
							|  |  |  |     # First read the magic header line | 
					
						
							|  |  |  |     datum = _parse_1_line(file) | 
					
						
							|  |  |  |     if datum <> ('Magic', 12321): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         raise error, 'Not a forms definition file' | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     # Now skip until we know number of forms | 
					
						
							|  |  |  |     while 1: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         datum = _parse_1_line(file) | 
					
						
							|  |  |  |         if type(datum) == type(()) and datum[0] == 'Numberofforms': | 
					
						
							|  |  |  |             break | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return datum[1] | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal: parse fd form, or skip if name doesn't match. | 
					
						
							|  |  |  | # the special value None means 'allways parse it'. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def _parse_fd_form(file, name): | 
					
						
							|  |  |  |     datum = _parse_1_line(file) | 
					
						
							|  |  |  |     if datum <> FORMLINE: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         raise error, 'Missing === FORM === line' | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     form = _parse_object(file) | 
					
						
							|  |  |  |     if form.Name == name or name == None: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         objs = [] | 
					
						
							|  |  |  |         for j in range(form.Numberofobjects): | 
					
						
							|  |  |  |             obj = _parse_object(file) | 
					
						
							|  |  |  |             objs.append(obj) | 
					
						
							|  |  |  |         return (form, objs) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         for j in range(form.Numberofobjects): | 
					
						
							|  |  |  |             _skip_object(file) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal class: a convient place to store object info fields | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | class _newobj: | 
					
						
							|  |  |  |     def add(self, name, value): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         self.__dict__[name] = value | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     def make(self, dict): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         for name in dict.keys(): | 
					
						
							|  |  |  |             self.add(name, dict[name]) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal parsing routines. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def _parse_string(str): | 
					
						
							|  |  |  |     if '\\' in str: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         s = '\'' + str + '\'' | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             return eval(s) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return str | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _parse_num(str): | 
					
						
							|  |  |  |     return eval(str) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _parse_numlist(str): | 
					
						
							|  |  |  |     slist = string.split(str) | 
					
						
							|  |  |  |     nlist = [] | 
					
						
							|  |  |  |     for i in slist: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         nlist.append(_parse_num(i)) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return nlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # This dictionary maps item names to parsing routines. | 
					
						
							|  |  |  | # If no routine is given '_parse_num' is default. | 
					
						
							|  |  |  | _parse_func = { \ | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         'Name':         _parse_string, \ | 
					
						
							|  |  |  |         'Box':          _parse_numlist, \ | 
					
						
							|  |  |  |         'Colors':       _parse_numlist, \ | 
					
						
							|  |  |  |         'Label':        _parse_string, \ | 
					
						
							|  |  |  |         'Name':         _parse_string, \ | 
					
						
							|  |  |  |         'Callback':     _parse_string, \ | 
					
						
							|  |  |  |         'Argument':     _parse_string } | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # This function parses a line, and returns either | 
					
						
							|  |  |  | # a string or a tuple (name,value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-10-22 21:00:49 +00:00
										 |  |  | import re | 
					
						
							|  |  |  | prog = re.compile('^([^:]*): *(.*)') | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _parse_line(line): | 
					
						
							| 
									
										
										
										
											1997-10-22 21:00:49 +00:00
										 |  |  |     match = prog.match(line) | 
					
						
							|  |  |  |     if not match: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         return line | 
					
						
							| 
									
										
										
										
											1997-10-22 21:00:49 +00:00
										 |  |  |     name, value = match.group(1, 2) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     if name[0] == 'N': | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |             name = string.join(string.split(name),'') | 
					
						
							|  |  |  |             name = string.lower(name) | 
					
						
							| 
									
										
										
										
											1997-10-22 21:00:49 +00:00
										 |  |  |     name = string.capitalize(name) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         pf = _parse_func[name] | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     except KeyError: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         pf = _parse_num | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     value = pf(value) | 
					
						
							|  |  |  |     return (name, value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _readline(file): | 
					
						
							|  |  |  |     line = file.readline() | 
					
						
							|  |  |  |     if not line: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         raise EOFError | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return line[:-1] | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | def _parse_1_line(file): | 
					
						
							|  |  |  |     line = _readline(file) | 
					
						
							|  |  |  |     while line == '': | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         line = _readline(file) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return _parse_line(line) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _skip_object(file): | 
					
						
							|  |  |  |     line = '' | 
					
						
							|  |  |  |     while not line in (SPLITLINE, FORMLINE, ENDLINE): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         pos = file.tell() | 
					
						
							|  |  |  |         line = _readline(file) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     if line == FORMLINE: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         file.seek(pos) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _parse_object(file): | 
					
						
							|  |  |  |     obj = _newobj() | 
					
						
							|  |  |  |     while 1: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         pos = file.tell() | 
					
						
							|  |  |  |         datum = _parse_1_line(file) | 
					
						
							|  |  |  |         if datum in (SPLITLINE, FORMLINE, ENDLINE): | 
					
						
							|  |  |  |             if datum == FORMLINE: | 
					
						
							|  |  |  |                 file.seek(pos) | 
					
						
							|  |  |  |             return obj | 
					
						
							|  |  |  |         if type(datum) <> type(()) or len(datum) <> 2: | 
					
						
							|  |  |  |             raise error, 'Parse error, illegal line in object: '+datum | 
					
						
							|  |  |  |         obj.add(datum[0], datum[1]) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ################################################################# | 
					
						
							|  |  |  | #   Part 2 - High-level object/form creation routines            # | 
					
						
							|  |  |  | ################################################################# | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # External - Create a form an link to an instance variable. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def create_full_form(inst, (fdata, odatalist)): | 
					
						
							|  |  |  |     form = create_form(fdata) | 
					
						
							|  |  |  |     exec 'inst.'+fdata.Name+' = form\n' | 
					
						
							|  |  |  |     for odata in odatalist: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         create_object_instance(inst, form, odata) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # External - Merge a form into an existing form in an instance | 
					
						
							|  |  |  | # variable. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def merge_full_form(inst, form, (fdata, odatalist)): | 
					
						
							|  |  |  |     exec 'inst.'+fdata.Name+' = form\n' | 
					
						
							|  |  |  |     if odatalist[0].Class <> FL.BOX: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         raise error, 'merge_full_form() expects FL.BOX as first obj' | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     for odata in odatalist[1:]: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         create_object_instance(inst, form, odata) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ################################################################# | 
					
						
							|  |  |  | #   Part 3 - Low-level object/form creation routines            # | 
					
						
							|  |  |  | ################################################################# | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # External Create_form - Create form from parameters | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def create_form(fdata): | 
					
						
							|  |  |  |     import fl | 
					
						
							|  |  |  |     return fl.make_form(FL.NO_BOX, fdata.Width, fdata.Height) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # External create_object - Create an object. Make sure there are | 
					
						
							|  |  |  | # no callbacks. Returns the object created. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def create_object(form, odata): | 
					
						
							|  |  |  |     obj = _create_object(form, odata) | 
					
						
							|  |  |  |     if odata.Callback: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         raise error, 'Creating free object with callback' | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return obj | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # External create_object_instance - Create object in an instance. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def create_object_instance(inst, form, odata): | 
					
						
							|  |  |  |     obj = _create_object(form, odata) | 
					
						
							|  |  |  |     if odata.Callback: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         cbfunc = eval('inst.'+odata.Callback) | 
					
						
							|  |  |  |         obj.set_call_back(cbfunc, odata.Argument) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     if odata.Name: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         exec 'inst.' + odata.Name + ' = obj\n' | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # Internal _create_object: Create the object and fill options | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def _create_object(form, odata): | 
					
						
							|  |  |  |     crfunc = _select_crfunc(form, odata.Class) | 
					
						
							|  |  |  |     obj = crfunc(odata.Type, odata.Box[0], odata.Box[1], odata.Box[2], \ | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |             odata.Box[3], odata.Label) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     if not odata.Class in (FL.BEGIN_GROUP, FL.END_GROUP): | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         obj.boxtype = odata.Boxtype | 
					
						
							|  |  |  |         obj.col1 = odata.Colors[0] | 
					
						
							|  |  |  |         obj.col2 = odata.Colors[1] | 
					
						
							|  |  |  |         obj.align = odata.Alignment | 
					
						
							|  |  |  |         obj.lstyle = odata.Style | 
					
						
							|  |  |  |         obj.lsize = odata.Size | 
					
						
							|  |  |  |         obj.lcol = odata.Lcol | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     return obj | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Internal crfunc: helper function that returns correct create function | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def _select_crfunc(fm, cl): | 
					
						
							|  |  |  |     if cl == FL.BEGIN_GROUP: return fm.bgn_group | 
					
						
							|  |  |  |     elif cl == FL.END_GROUP: return fm.end_group | 
					
						
							|  |  |  |     elif cl == FL.BITMAP: return fm.add_bitmap | 
					
						
							|  |  |  |     elif cl == FL.BOX: return fm.add_box | 
					
						
							|  |  |  |     elif cl == FL.BROWSER: return fm.add_browser | 
					
						
							|  |  |  |     elif cl == FL.BUTTON: return fm.add_button | 
					
						
							|  |  |  |     elif cl == FL.CHART: return fm.add_chart | 
					
						
							|  |  |  |     elif cl == FL.CHOICE: return fm.add_choice | 
					
						
							|  |  |  |     elif cl == FL.CLOCK: return fm.add_clock | 
					
						
							|  |  |  |     elif cl == FL.COUNTER: return fm.add_counter | 
					
						
							|  |  |  |     elif cl == FL.DIAL: return fm.add_dial | 
					
						
							|  |  |  |     elif cl == FL.FREE: return fm.add_free | 
					
						
							|  |  |  |     elif cl == FL.INPUT: return fm.add_input | 
					
						
							|  |  |  |     elif cl == FL.LIGHTBUTTON: return fm.add_lightbutton | 
					
						
							|  |  |  |     elif cl == FL.MENU: return fm.add_menu | 
					
						
							|  |  |  |     elif cl == FL.POSITIONER: return fm.add_positioner | 
					
						
							|  |  |  |     elif cl == FL.ROUNDBUTTON: return fm.add_roundbutton | 
					
						
							|  |  |  |     elif cl == FL.SLIDER: return fm.add_slider | 
					
						
							|  |  |  |     elif cl == FL.VALSLIDER: return fm.add_valslider | 
					
						
							|  |  |  |     elif cl == FL.TEXT: return fm.add_text | 
					
						
							|  |  |  |     elif cl == FL.TIMER: return fm.add_timer | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         raise error, 'Unknown object type: ' + `cl` | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test(): | 
					
						
							|  |  |  |     import time | 
					
						
							|  |  |  |     t0 = time.time() | 
					
						
							|  |  |  |     if len(sys.argv) == 2: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         forms = parse_forms(sys.argv[1]) | 
					
						
							|  |  |  |         t1 = time.time() | 
					
						
							|  |  |  |         print 'parse time:', 0.001*(t1-t0), 'sec.' | 
					
						
							|  |  |  |         keys = forms.keys() | 
					
						
							|  |  |  |         keys.sort() | 
					
						
							|  |  |  |         for i in keys: | 
					
						
							|  |  |  |             _printform(forms[i]) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     elif len(sys.argv) == 3: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         form = parse_form(sys.argv[1], sys.argv[2]) | 
					
						
							|  |  |  |         t1 = time.time() | 
					
						
							|  |  |  |         print 'parse time:', round(t1-t0, 3), 'sec.' | 
					
						
							|  |  |  |         _printform(form) | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         print 'Usage: test fdfile [form]' | 
					
						
							| 
									
										
										
										
											1997-01-15 19:19:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _printform(form): | 
					
						
							|  |  |  |     f = form[0] | 
					
						
							|  |  |  |     objs = form[1] | 
					
						
							|  |  |  |     print 'Form ', f.Name, ', size: ', f.Width, f.Height, ' Nobj ', f.Numberofobjects | 
					
						
							|  |  |  |     for i in objs: | 
					
						
							| 
									
										
										
										
											1998-03-26 20:23:01 +00:00
										 |  |  |         print '  Obj ', i.Name, ' type ', i.Class, i.Type | 
					
						
							|  |  |  |         print '    Box ', i.Box, ' btype ', i.Boxtype | 
					
						
							|  |  |  |         print '    Label ', i.Label, ' size/style/col/align ', i.Size,i.Style, i.Lcol, i.Alignment | 
					
						
							|  |  |  |         print '    cols ', i.Colors | 
					
						
							|  |  |  |         print '    cback ', i.Callback, i.Argument |