home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / statcache.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  3.3 KB  |  89 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Maintain a cache of stat() information on files.
  5.  
  6. There are functions to reset the cache or to selectively remove items.
  7. '''
  8. import os as _os
  9. from stat import *
  10. __all__ = [
  11.     'stat',
  12.     'reset',
  13.     'forget',
  14.     'forget_prefix',
  15.     'forget_dir',
  16.     'forget_except_prefix',
  17.     'isdir']
  18. cache = { }
  19.  
  20. def stat(path):
  21.     '''Stat a file, possibly out of the cache.'''
  22.     ret = cache.get(path, None)
  23.     if ret is None:
  24.         cache[path] = ret = _os.stat(path)
  25.     
  26.     return ret
  27.  
  28.  
  29. def reset():
  30.     '''Clear the cache.'''
  31.     cache.clear()
  32.  
  33.  
  34. def forget(path):
  35.     '''Remove a given item from the cache, if it exists.'''
  36.     
  37.     try:
  38.         del cache[path]
  39.     except KeyError:
  40.         pass
  41.  
  42.  
  43.  
  44. def forget_prefix(prefix):
  45.     '''Remove all pathnames with a given prefix.'''
  46.     for path in cache.keys():
  47.         if path.startswith(prefix):
  48.             forget(path)
  49.         
  50.     
  51.  
  52.  
  53. def forget_dir(prefix):
  54.     '''Forget a directory and all entries except for entries in subdirs.'''
  55.     split = split
  56.     join = join
  57.     import os.path
  58.     prefix = split(join(prefix, 'xxx'))[0]
  59.     forget(prefix)
  60.     for path in cache.keys():
  61.         if path.startswith(prefix) and split(path)[0] == prefix:
  62.             forget(path)
  63.         
  64.     
  65.  
  66.  
  67. def forget_except_prefix(prefix):
  68.     """Remove all pathnames except with a given prefix.
  69.  
  70.     Normally used with prefix = '/' after a chdir().
  71.     """
  72.     for path in cache.keys():
  73.         if not path.startswith(prefix):
  74.             forget(path)
  75.         
  76.     
  77.  
  78.  
  79. def isdir(path):
  80.     '''Return 1 if directory, else 0.'''
  81.     
  82.     try:
  83.         st = stat(path)
  84.     except _os.error:
  85.         return 0
  86.  
  87.     return S_ISDIR(st[ST_MODE])
  88.  
  89.