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  from 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      t = os.stat(filename)[stat.ST_MTIME]
    38      cfs_lock.acquire()
    39      item = cfs.get(key, None)
    40      cfs_lock.release()
    41      if item and item[0] == t:
    42          return item[1]
    43      if not filter:
    44          data = read_file(filename)
    45      else:
    46          data = filter()
    47      cfs_lock.acquire()
    48      cfs[key] = (t, data)
    49      cfs_lock.release()
    50      return data
    51