Package web2py :: Package gluon :: Module cfs
[hide private]
[frames] | no frames]

Source Code for Module web2py.gluon.cfs

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 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  FOR INTERNAL USE ONLY 
13  """ 
14   
15  import os 
16  import stat 
17  import thread 
18   
19  cfs = {}  # for speed-up 
20  cfs_lock = thread.allocate_lock()  # and thread safety 
21   
22   
23 -def getcfs(key, filename, filter=None):
24 """ 25 Caches the *filtered* file `filename` with `key` until the file is 26 modified. 27 28 :param key: the cache key 29 :param filename: the file to cache 30 :param filter: is the function used for filtering. Normally `filename` is a 31 .py file and `filter` is a function that bytecode compiles the file. 32 In this way the bytecode compiled file is cached. (Default = None) 33 34 This is used on Google App Engine since pyc files cannot be saved. 35 """ 36 t = os.stat(filename)[stat.ST_MTIME] 37 cfs_lock.acquire() 38 item = cfs.get(key, None) 39 cfs_lock.release() 40 if item and item[0] == t: 41 return item[1] 42 if not filter: 43 fp = open(filename, 'r') 44 data = fp.read() 45 fp.close() 46 else: 47 data = filter() 48 cfs_lock.acquire() 49 cfs[key] = (t, data) 50 cfs_lock.release() 51 return data
52