@@ -59,7 +59,11 @@ def parse_forms(filename):
5959# Internal: see if a cached version of the file exists
6060#
6161MAGIC = '.fdc'
62+ _internal_cache = {} # Used by frozen scripts only
6263def checkcache (filename ):
64+ if _internal_cache .has_key (filename ):
65+ altforms = _internal_cache [filename ]
66+ return _unpack_cache (altforms )
6367 import marshal
6468 fp , filename = _open_formfile2 (filename )
6569 fp .close ()
@@ -80,6 +84,11 @@ def checkcache(filename):
8084 return None
8185 #print 'flp: valid cache file', cachename
8286 altforms = marshal .load (fp )
87+ return _unpack_cache (altforms )
88+ finally :
89+ fp .close ()
90+
91+ def _unpack_cache (altforms ):
8392 forms = {}
8493 for name in altforms .keys ():
8594 altobj , altlist = altforms [name ]
@@ -92,8 +101,6 @@ def checkcache(filename):
92101 list .append (nobj )
93102 forms [name ] = obj , list
94103 return forms
95- finally :
96- fp .close ()
97104
98105def rdlong (fp ):
99106 s = fp .read (4 )
@@ -128,19 +135,41 @@ def writecache(filename, forms):
128135 return # Never mind
129136 fp .write ('\0 \0 \0 \0 ' ) # Seek back and write MAGIC when done
130137 wrlong (fp , getmtime (filename ))
138+ altforms = _pack_cache (forms )
139+ marshal .dump (altforms , fp )
140+ fp .seek (0 )
141+ fp .write (MAGIC )
142+ fp .close ()
143+ #print 'flp: wrote cache file', cachename
144+
145+ #
146+ # External: print some statements that set up the internal cache.
147+ # This is for use with the "freeze" script. You should call
148+ # flp.freeze(filename) for all forms used by the script, and collect
149+ # the output on a file in a module file named "frozenforms.py". Then
150+ # in the main program of the script import frozenforms.
151+ # (Don't forget to take this out when using the unfrozen version of
152+ # the script!)
153+ #
154+ def freeze (filename ):
155+ forms = parse_forms (filename )
156+ altforms = _pack_cache (forms )
157+ print 'import flp'
158+ print 'flp._internal_cache[' , `filename` , '] =' , altforms
159+
160+ #
161+ # Internal: create the data structure to be placed in the cache
162+ #
163+ def _pack_cache (forms ):
131164 altforms = {}
132165 for name in forms .keys ():
133166 obj , list = forms [name ]
134167 altobj = obj .__dict__
135168 altlist = []
136169 for obj in list : altlist .append (obj .__dict__ )
137170 altforms [name ] = altobj , altlist
138- marshal .dump (altforms , fp )
139- fp .seek (0 )
140- fp .write (MAGIC )
141- fp .close ()
142- #print 'flp: wrote cache file', cachename
143-
171+ return altforms
172+
144173#
145174# Internal: Locate form file (using PYTHONPATH) and open file
146175#
0 commit comments