source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/gluon/cfs.py

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 1.4 KB
Line 
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
9Functions required to execute app components
10--------------------------------------------
11
12Note:
13    FOR INTERNAL USE ONLY
14"""
15
16from os import stat
17from gluon.fileutils import read_file
18from gluon._compat import thread
19
20cfs = {}  # for speed-up
21cfs_lock = thread.allocate_lock()  # and thread safety
22
23
24def 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
Note: See TracBrowser for help on using the repository browser.