Use py_resource module
[python/dscho.git] / Lib / statcache.py
blob770aef0662f4d502bee54e031d1217f2d34ae05f
1 # Module 'statcache'
3 # Maintain a cache of file stats.
4 # There are functions to reset the cache or to selectively remove items.
6 import os
7 from stat import *
9 # The cache.
10 # Keys are pathnames, values are `os.stat' outcomes.
12 cache = {}
15 # Stat a file, possibly out of the cache.
17 def stat(path):
18 if cache.has_key(path):
19 return cache[path]
20 cache[path] = ret = os.stat(path)
21 return ret
24 # Reset the cache completely.
26 def reset():
27 global cache
28 cache = {}
31 # Remove a given item from the cache, if it exists.
33 def forget(path):
34 if cache.has_key(path):
35 del cache[path]
38 # Remove all pathnames with a given prefix.
40 def forget_prefix(prefix):
41 n = len(prefix)
42 for path in cache.keys():
43 if path[:n] == prefix:
44 del cache[path]
47 # Forget about a directory and all entries in it, but not about
48 # entries in subdirectories.
50 def forget_dir(prefix):
51 if prefix[-1:] == '/' and prefix <> '/':
52 prefix = prefix[:-1]
53 forget(prefix)
54 if prefix[-1:] <> '/':
55 prefix = prefix + '/'
56 n = len(prefix)
57 for path in cache.keys():
58 if path[:n] == prefix:
59 rest = path[n:]
60 if rest[-1:] == '/': rest = rest[:-1]
61 if '/' not in rest:
62 del cache[path]
65 # Remove all pathnames except with a given prefix.
66 # Normally used with prefix = '/' after a chdir().
68 def forget_except_prefix(prefix):
69 n = len(prefix)
70 for path in cache.keys():
71 if path[:n] <> prefix:
72 del cache[path]
75 # Check for directory.
77 def isdir(path):
78 try:
79 st = stat(path)
80 except os.error:
81 return 0
82 return S_ISDIR(st[ST_MODE])