Home | History | Annotate | Download | only in plat-irix6
      1 #
      2 # flp - Module to load fl forms from fd files
      3 #
      4 # Jack Jansen, December 1991
      5 #
      6 from warnings import warnpy3k
      7 warnpy3k("the flp module has been removed in Python 3.0", stacklevel=2)
      8 del warnpy3k
      9 
     10 import os
     11 import sys
     12 import FL
     13 
     14 SPLITLINE = '--------------------'
     15 FORMLINE = '=============== FORM ==============='
     16 ENDLINE = '=============================='
     17 
     18 class error(Exception):
     19     pass
     20 
     21 ##################################################################
     22 #    Part 1 - The parsing routines                               #
     23 ##################################################################
     24 
     25 #
     26 # Externally visible function. Load form.
     27 #
     28 def parse_form(filename, formname):
     29     forms = checkcache(filename)
     30     if forms is None:
     31         forms = parse_forms(filename)
     32     if forms.has_key(formname):
     33         return forms[formname]
     34     else:
     35         raise error, 'No such form in fd file'
     36 
     37 #
     38 # Externally visible function. Load all forms.
     39 #
     40 def parse_forms(filename):
     41     forms = checkcache(filename)
     42     if forms is not None: return forms
     43     fp = _open_formfile(filename)
     44     nforms = _parse_fd_header(fp)
     45     forms = {}
     46     for i in range(nforms):
     47         form = _parse_fd_form(fp, None)
     48         forms[form[0].Name] = form
     49     writecache(filename, forms)
     50     return forms
     51 
     52 #
     53 # Internal: see if a cached version of the file exists
     54 #
     55 MAGIC = '.fdc'
     56 _internal_cache = {}                    # Used by frozen scripts only
     57 def checkcache(filename):
     58     if _internal_cache.has_key(filename):
     59         altforms = _internal_cache[filename]
     60         return _unpack_cache(altforms)
     61     import marshal
     62     fp, filename = _open_formfile2(filename)
     63     fp.close()
     64     cachename = filename + 'c'
     65     try:
     66         fp = open(cachename, 'r')
     67     except IOError:
     68         #print 'flp: no cache file', cachename
     69         return None
     70     try:
     71         if fp.read(4) != MAGIC:
     72             print 'flp: bad magic word in cache file', cachename
     73             return None
     74         cache_mtime = rdlong(fp)
     75         file_mtime = getmtime(filename)
     76         if cache_mtime != file_mtime:
     77             #print 'flp: outdated cache file', cachename
     78             return None
     79         #print 'flp: valid cache file', cachename
     80         altforms = marshal.load(fp)
     81         return _unpack_cache(altforms)
     82     finally:
     83         fp.close()
     84 
     85 def _unpack_cache(altforms):
     86     forms = {}
     87     for name in altforms.keys():
     88         altobj, altlist = altforms[name]
     89         obj = _newobj()
     90         obj.make(altobj)
     91         list = []
     92         for altobj in altlist:
     93             nobj = _newobj()
     94             nobj.make(altobj)
     95             list.append(nobj)
     96         forms[name] = obj, list
     97     return forms
     98 
     99 def rdlong(fp):
    100     s = fp.read(4)
    101     if len(s) != 4: return None
    102     a, b, c, d = s[0], s[1], s[2], s[3]
    103     return ord(a)<<24 | ord(b)<<16 | ord(c)<<8 | ord(d)
    104 
    105 def wrlong(fp, x):
    106     a, b, c, d = (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff
    107     fp.write(chr(a) + chr(b) + chr(c) + chr(d))
    108 
    109 def getmtime(filename):
    110     import os
    111     from stat import ST_MTIME
    112     try:
    113         return os.stat(filename)[ST_MTIME]
    114     except os.error:
    115         return None
    116 
    117 #
    118 # Internal: write cached version of the form (parsing is too slow!)
    119 #
    120 def writecache(filename, forms):
    121     import marshal
    122     fp, filename = _open_formfile2(filename)
    123     fp.close()
    124     cachename = filename + 'c'
    125     try:
    126         fp = open(cachename, 'w')
    127     except IOError:
    128         print 'flp: can\'t create cache file', cachename
    129         return # Never mind
    130     fp.write('\0\0\0\0') # Seek back and write MAGIC when done
    131     wrlong(fp, getmtime(filename))
    132     altforms = _pack_cache(forms)
    133     marshal.dump(altforms, fp)
    134     fp.seek(0)
    135     fp.write(MAGIC)
    136     fp.close()
    137     #print 'flp: wrote cache file', cachename
    138 
    139 #
    140 # External: print some statements that set up the internal cache.
    141 # This is for use with the "freeze" script.  You should call
    142 # flp.freeze(filename) for all forms used by the script, and collect
    143 # the output on a file in a module file named "frozenforms.py".  Then
    144 # in the main program of the script import frozenforms.
    145 # (Don't forget to take this out when using the unfrozen version of
    146 # the script!)
    147 #
    148 def freeze(filename):
    149     forms = parse_forms(filename)
    150     altforms = _pack_cache(forms)
    151     print 'import flp'
    152     print 'flp._internal_cache[', repr(filename), '] =', altforms
    153 
    154 #
    155 # Internal: create the data structure to be placed in the cache
    156 #
    157 def _pack_cache(forms):
    158     altforms = {}
    159     for name in forms.keys():
    160         obj, list = forms[name]
    161         altobj = obj.__dict__
    162         altlist = []
    163         for obj in list: altlist.append(obj.__dict__)
    164         altforms[name] = altobj, altlist
    165     return altforms
    166 
    167 #
    168 # Internal: Locate form file (using PYTHONPATH) and open file
    169 #
    170 def _open_formfile(filename):
    171     return _open_formfile2(filename)[0]
    172 
    173 def _open_formfile2(filename):
    174     if filename[-3:] != '.fd':
    175         filename = filename + '.fd'
    176     if filename[0] == '/':
    177         try:
    178             fp = open(filename,'r')
    179         except IOError:
    180             fp = None
    181     else:
    182         for pc in sys.path:
    183             pn = os.path.join(pc, filename)
    184             try:
    185                 fp = open(pn, 'r')
    186                 filename = pn
    187                 break
    188             except IOError:
    189                 fp = None
    190     if fp is None:
    191         raise error, 'Cannot find forms file ' + filename
    192     return fp, filename
    193 
    194 #
    195 # Internal: parse the fd file header, return number of forms
    196 #
    197 def _parse_fd_header(file):
    198     # First read the magic header line
    199     datum = _parse_1_line(file)
    200     if datum != ('Magic', 12321):
    201         raise error, 'Not a forms definition file'
    202     # Now skip until we know number of forms
    203     while 1:
    204         datum = _parse_1_line(file)
    205         if type(datum) == type(()) and datum[0] == 'Numberofforms':
    206             break
    207     return datum[1]
    208 #
    209 # Internal: parse fd form, or skip if name doesn't match.
    210 # the special value None means 'always parse it'.
    211 #
    212 def _parse_fd_form(file, name):
    213     datum = _parse_1_line(file)
    214     if datum != FORMLINE:
    215         raise error, 'Missing === FORM === line'
    216     form = _parse_object(file)
    217     if form.Name == name or name is None:
    218         objs = []
    219         for j in range(form.Numberofobjects):
    220             obj = _parse_object(file)
    221             objs.append(obj)
    222         return (form, objs)
    223     else:
    224         for j in range(form.Numberofobjects):
    225             _skip_object(file)
    226     return None
    227 
    228 #
    229 # Internal class: a convenient place to store object info fields
    230 #
    231 class _newobj:
    232     def add(self, name, value):
    233         self.__dict__[name] = value
    234     def make(self, dict):
    235         for name in dict.keys():
    236             self.add(name, dict[name])
    237 
    238 #
    239 # Internal parsing routines.
    240 #
    241 def _parse_string(str):
    242     if '\\' in str:
    243         s = '\'' + str + '\''
    244         try:
    245             return eval(s)
    246         except:
    247             pass
    248     return str
    249 
    250 def _parse_num(str):
    251     return eval(str)
    252 
    253 def _parse_numlist(str):
    254     slist = str.split()
    255     nlist = []
    256     for i in slist:
    257         nlist.append(_parse_num(i))
    258     return nlist
    259 
    260 # This dictionary maps item names to parsing routines.
    261 # If no routine is given '_parse_num' is default.
    262 _parse_func = { \
    263         'Name':         _parse_string, \
    264         'Box':          _parse_numlist, \
    265         'Colors':       _parse_numlist, \
    266         'Label':        _parse_string, \
    267         'Name':         _parse_string, \
    268         'Callback':     _parse_string, \
    269         'Argument':     _parse_string }
    270 
    271 # This function parses a line, and returns either
    272 # a string or a tuple (name,value)
    273 
    274 import re
    275 prog = re.compile('^([^:]*): *(.*)')
    276 
    277 def _parse_line(line):
    278     match = prog.match(line)
    279     if not match:
    280         return line
    281     name, value = match.group(1, 2)
    282     if name[0] == 'N':
    283         name = ''.join(name.split())
    284         name = name.lower()
    285     name = name.capitalize()
    286     try:
    287         pf = _parse_func[name]
    288     except KeyError:
    289         pf = _parse_num
    290     value = pf(value)
    291     return (name, value)
    292 
    293 def _readline(file):
    294     line = file.readline()
    295     if not line:
    296         raise EOFError
    297     return line[:-1]
    298 
    299 def _parse_1_line(file):
    300     line = _readline(file)
    301     while line == '':
    302         line = _readline(file)
    303     return _parse_line(line)
    304 
    305 def _skip_object(file):
    306     line = ''
    307     while not line in (SPLITLINE, FORMLINE, ENDLINE):
    308         pos = file.tell()
    309         line = _readline(file)
    310     if line == FORMLINE:
    311         file.seek(pos)
    312 
    313 def _parse_object(file):
    314     obj = _newobj()
    315     while 1:
    316         pos = file.tell()
    317         datum = _parse_1_line(file)
    318         if datum in (SPLITLINE, FORMLINE, ENDLINE):
    319             if datum == FORMLINE:
    320                 file.seek(pos)
    321             return obj
    322         if type(datum) is not type(()) or len(datum) != 2:
    323             raise error, 'Parse error, illegal line in object: '+datum
    324         obj.add(datum[0], datum[1])
    325 
    326 #################################################################
    327 #   Part 2 - High-level object/form creation routines            #
    328 #################################################################
    329 
    330 #
    331 # External - Create a form and link to an instance variable.
    332 #
    333 def create_full_form(inst, (fdata, odatalist)):
    334     form = create_form(fdata)
    335     exec 'inst.'+fdata.Name+' = form\n'
    336     for odata in odatalist:
    337         create_object_instance(inst, form, odata)
    338 
    339 #
    340 # External - Merge a form into an existing form in an instance
    341 # variable.
    342 #
    343 def merge_full_form(inst, form, (fdata, odatalist)):
    344     exec 'inst.'+fdata.Name+' = form\n'
    345     if odatalist[0].Class != FL.BOX:
    346         raise error, 'merge_full_form() expects FL.BOX as first obj'
    347     for odata in odatalist[1:]:
    348         create_object_instance(inst, form, odata)
    349 
    350 
    351 #################################################################
    352 #   Part 3 - Low-level object/form creation routines            #
    353 #################################################################
    354 
    355 #
    356 # External Create_form - Create form from parameters
    357 #
    358 def create_form(fdata):
    359     import fl
    360     return fl.make_form(FL.NO_BOX, fdata.Width, fdata.Height)
    361 
    362 #
    363 # External create_object - Create an object. Make sure there are
    364 # no callbacks. Returns the object created.
    365 #
    366 def create_object(form, odata):
    367     obj = _create_object(form, odata)
    368     if odata.Callback:
    369         raise error, 'Creating free object with callback'
    370     return obj
    371 #
    372 # External create_object_instance - Create object in an instance.
    373 #
    374 def create_object_instance(inst, form, odata):
    375     obj = _create_object(form, odata)
    376     if odata.Callback:
    377         cbfunc = eval('inst.'+odata.Callback)
    378         obj.set_call_back(cbfunc, odata.Argument)
    379     if odata.Name:
    380         exec 'inst.' + odata.Name + ' = obj\n'
    381 #
    382 # Internal _create_object: Create the object and fill options
    383 #
    384 def _create_object(form, odata):
    385     crfunc = _select_crfunc(form, odata.Class)
    386     obj = crfunc(odata.Type, odata.Box[0], odata.Box[1], odata.Box[2], \
    387             odata.Box[3], odata.Label)
    388     if not odata.Class in (FL.BEGIN_GROUP, FL.END_GROUP):
    389         obj.boxtype = odata.Boxtype
    390         obj.col1 = odata.Colors[0]
    391         obj.col2 = odata.Colors[1]
    392         obj.align = odata.Alignment
    393         obj.lstyle = odata.Style
    394         obj.lsize = odata.Size
    395         obj.lcol = odata.Lcol
    396     return obj
    397 #
    398 # Internal crfunc: helper function that returns correct create function
    399 #
    400 def _select_crfunc(fm, cl):
    401     if cl == FL.BEGIN_GROUP: return fm.bgn_group
    402     elif cl == FL.END_GROUP: return fm.end_group
    403     elif cl == FL.BITMAP: return fm.add_bitmap
    404     elif cl == FL.BOX: return fm.add_box
    405     elif cl == FL.BROWSER: return fm.add_browser
    406     elif cl == FL.BUTTON: return fm.add_button
    407     elif cl == FL.CHART: return fm.add_chart
    408     elif cl == FL.CHOICE: return fm.add_choice
    409     elif cl == FL.CLOCK: return fm.add_clock
    410     elif cl == FL.COUNTER: return fm.add_counter
    411     elif cl == FL.DIAL: return fm.add_dial
    412     elif cl == FL.FREE: return fm.add_free
    413     elif cl == FL.INPUT: return fm.add_input
    414     elif cl == FL.LIGHTBUTTON: return fm.add_lightbutton
    415     elif cl == FL.MENU: return fm.add_menu
    416     elif cl == FL.POSITIONER: return fm.add_positioner
    417     elif cl == FL.ROUNDBUTTON: return fm.add_roundbutton
    418     elif cl == FL.SLIDER: return fm.add_slider
    419     elif cl == FL.VALSLIDER: return fm.add_valslider
    420     elif cl == FL.TEXT: return fm.add_text
    421     elif cl == FL.TIMER: return fm.add_timer
    422     else:
    423         raise error, 'Unknown object type: %r' % (cl,)
    424 
    425 
    426 def test():
    427     import time
    428     t0 = time.time()
    429     if len(sys.argv) == 2:
    430         forms = parse_forms(sys.argv[1])
    431         t1 = time.time()
    432         print 'parse time:', 0.001*(t1-t0), 'sec.'
    433         keys = forms.keys()
    434         keys.sort()
    435         for i in keys:
    436             _printform(forms[i])
    437     elif len(sys.argv) == 3:
    438         form = parse_form(sys.argv[1], sys.argv[2])
    439         t1 = time.time()
    440         print 'parse time:', round(t1-t0, 3), 'sec.'
    441         _printform(form)
    442     else:
    443         print 'Usage: test fdfile [form]'
    444 
    445 def _printform(form):
    446     f = form[0]
    447     objs = form[1]
    448     print 'Form ', f.Name, ', size: ', f.Width, f.Height, ' Nobj ', f.Numberofobjects
    449     for i in objs:
    450         print '  Obj ', i.Name, ' type ', i.Class, i.Type
    451         print '    Box ', i.Box, ' btype ', i.Boxtype
    452         print '    Label ', i.Label, ' size/style/col/align ', i.Size,i.Style, i.Lcol, i.Alignment
    453         print '    cols ', i.Colors
    454         print '    cback ', i.Callback, i.Argument
    455