(py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
[python/dscho.git] / Lib / statcache.py
blob04381f3861591c0603b9f6455feddf7aa9708cff
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.
25 # Hack: to reset a global variable, we import this module.
27 def reset():
28 import statcache
29 # Check that we really imported the same module
30 if cache is not statcache.cache:
31 raise 'sorry, statcache identity crisis'
32 statcache.cache = {}
35 # Remove a given item from the cache, if it exists.
37 def forget(path):
38 if cache.has_key(path):
39 del cache[path]
42 # Remove all pathnames with a given prefix.
44 def forget_prefix(prefix):
45 n = len(prefix)
46 for path in cache.keys():
47 if path[:n] == prefix:
48 del cache[path]
51 # Forget about a directory and all entries in it, but not about
52 # entries in subdirectories.
54 def forget_dir(prefix):
55 if prefix[-1:] == '/' and prefix <> '/':
56 prefix = prefix[:-1]
57 forget(prefix)
58 if prefix[-1:] <> '/':
59 prefix = prefix + '/'
60 n = len(prefix)
61 for path in cache.keys():
62 if path[:n] == prefix:
63 rest = path[n:]
64 if rest[-1:] == '/': rest = rest[:-1]
65 if '/' not in rest:
66 del cache[path]
69 # Remove all pathnames except with a given prefix.
70 # Normally used with prefix = '/' after a chdir().
72 def forget_except_prefix(prefix):
73 n = len(prefix)
74 for path in cache.keys():
75 if path[:n] <> prefix:
76 del cache[path]
79 # Check for directory.
81 def isdir(path):
82 try:
83 st = stat(path)
84 except os.error:
85 return 0
86 return S_ISDIR(st[ST_MODE])