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

Source Code for Module 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  from os import stat 
16  import thread 
17  import logging 
18  from gluon.fileutils import read_file 
19   
20  cfs = {}  # for speed-up 
21  cfs_lock = thread.allocate_lock()  # and thread safety 
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 :param key: the cache key 30 :param filename: the file to cache 31 :param filter: is the function used for filtering. Normally `filename` is a 32 .py file and `filter` is a function that bytecode compiles the file. 33 In this way the bytecode compiled file is cached. (Default = None) 34 35 This is used on Google App Engine since pyc files cannot be saved. 36 """ 37 try: 38 t = stat(filename).st_mtime 39 except OSError: 40 return filter() if callable(filter) else '' 41 cfs_lock.acquire() 42 item = cfs.get(key, None) 43 cfs_lock.release() 44 if item and item[0] == t: 45 return item[1] 46 if not callable(filter): 47 data = read_file(filename) 48 else: 49 data = filter() 50 cfs_lock.acquire() 51 cfs[key] = (t, data) 52 cfs_lock.release() 53 return data
54