Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 1 | # Module 'maccache' |
| 2 | # |
| 3 | # Maintain a cache of listdir(), isdir(), isfile() or exists() outcomes. |
Guido van Rossum | 50399ae | 1992-03-31 19:05:44 +0000 | [diff] [blame] | 4 | # XXX Should merge with module statcache |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 50399ae | 1992-03-31 19:05:44 +0000 | [diff] [blame] | 6 | import os |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 7 | |
| 8 | |
| 9 | # The cache. |
| 10 | # Keys are absolute pathnames; |
| 11 | # values are 0 (nothing), 1 (file) or [...] (dir). |
| 12 | # |
| 13 | cache = {} |
| 14 | |
| 15 | |
| 16 | # Current working directory. |
| 17 | # |
Guido van Rossum | 50399ae | 1992-03-31 19:05:44 +0000 | [diff] [blame] | 18 | cwd = os.getcwd() |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | # Constants. |
| 22 | # |
| 23 | NONE = 0 |
| 24 | FILE = 1 |
| 25 | LISTTYPE = type([]) |
| 26 | |
| 27 | def _stat(name): |
Guido van Rossum | 50399ae | 1992-03-31 19:05:44 +0000 | [diff] [blame] | 28 | name = os.path.join(cwd, name) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 29 | if cache.has_key(name): |
| 30 | return cache[name] |
Guido van Rossum | 50399ae | 1992-03-31 19:05:44 +0000 | [diff] [blame] | 31 | if os.path.isfile(name): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 32 | cache[name] = FILE |
| 33 | return FILE |
| 34 | try: |
Guido van Rossum | 50399ae | 1992-03-31 19:05:44 +0000 | [diff] [blame] | 35 | list = os.listdir(name) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 36 | except: |
| 37 | cache[name] = NONE |
| 38 | return NONE |
| 39 | cache[name] = list |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 40 | if name[-1:] == ':': cache[name[:-1]] = list |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 41 | else: cache[name+':'] = list |
| 42 | return list |
| 43 | |
| 44 | def isdir(name): |
| 45 | st = _stat(name) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 46 | return type(st) == LISTTYPE |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 47 | |
| 48 | def isfile(name): |
| 49 | st = _stat(name) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 50 | return st == FILE |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 51 | |
| 52 | def exists(name): |
| 53 | st = _stat(name) |
| 54 | return st <> NONE |
| 55 | |
| 56 | def listdir(name): |
| 57 | st = _stat(name) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 58 | if type(st) == LISTTYPE: |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 59 | return st |
| 60 | else: |
| 61 | raise RuntimeError, 'list non-directory' |