1
2
3
4 """
5 | This file is part of the web2py Web Framework
6 | Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
7 | License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
8
9 Functions required to execute app components
10 --------------------------------------------
11
12 Note:
13 FOR INTERNAL USE ONLY
14 """
15
16 from os import stat
17 import thread
18 from gluon.fileutils import read_file
19
20 cfs = {}
21 cfs_lock = thread.allocate_lock()
22
23
24 -def getcfs(key, filename, filter=None):
25 """
26 Caches the *filtered* file `filename` with `key` until the file is
27 modified.
28
29 Args:
30 key(str): the cache key
31 filename: the file to cache
32 filter: is the function used for filtering. Normally `filename` is a
33 .py file and `filter` is a function that bytecode compiles the file.
34 In this way the bytecode compiled file is cached. (Default = None)
35
36 This is used on Google App Engine since pyc files cannot be saved.
37 """
38 try:
39 t = stat(filename).st_mtime
40 except OSError:
41 return filter() if callable(filter) else ''
42 cfs_lock.acquire()
43 item = cfs.get(key, None)
44 cfs_lock.release()
45 if item and item[0] == t:
46 return item[1]
47 if not callable(filter):
48 data = read_file(filename)
49 else:
50 data = filter()
51 cfs_lock.acquire()
52 cfs[key] = (t, data)
53 cfs_lock.release()
54 return data
55