Mercurial > piecrust2
comparison piecrust/cache.py @ 3:f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
- Serving works, with debug window.
- Baking works, multi-threading, with dependency handling.
- Various things not implemented yet.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 10 Aug 2014 23:43:16 -0700 |
parents | a212a3f2e3ee |
children | 474c9882decf |
comparison
equal
deleted
inserted
replaced
2:40fa08b261b9 | 3:f485ba500df3 |
---|---|
1 import os | 1 import os |
2 import os.path | 2 import os.path |
3 import codecs | 3 import codecs |
4 import logging | |
5 import threading | |
6 | |
7 | |
8 logger = logging.getLogger(__name__) | |
9 | |
10 | |
11 class ExtensibleCache(object): | |
12 def __init__(self, base_dir): | |
13 self.base_dir = base_dir | |
14 self.lock = threading.Lock() | |
15 self.caches = {} | |
16 | |
17 @property | |
18 def enabled(self): | |
19 return True | |
20 | |
21 def getCache(self, name): | |
22 c = self.caches.get(name) | |
23 if c is None: | |
24 with self.lock: | |
25 c = self.caches.get(name) | |
26 if c is None: | |
27 c_dir = os.path.join(self.base_dir, name) | |
28 if not os.path.isdir(c_dir): | |
29 os.makedirs(c_dir, 0755) | |
30 | |
31 c = SimpleCache(c_dir) | |
32 self.caches[name] = c | |
33 return c | |
4 | 34 |
5 | 35 |
6 class SimpleCache(object): | 36 class SimpleCache(object): |
7 def __init__(self, base_dir): | 37 def __init__(self, base_dir): |
38 self.base_dir = base_dir | |
8 if not os.path.isdir(base_dir): | 39 if not os.path.isdir(base_dir): |
9 os.makedirs(base_dir, 0755) | 40 raise Exception("Cache directory doesn't exist: %s" % base_dir) |
10 self.base_dir = base_dir | |
11 | 41 |
12 def isValid(self, path, time): | 42 def isValid(self, path, time): |
13 cache_time = self.getCacheTime(path) | 43 cache_time = self.getCacheTime(path) |
14 if cache_time is None: | 44 if cache_time is None: |
15 return False | 45 return False |
31 cache_path = self.getCachePath(path) | 61 cache_path = self.getCachePath(path) |
32 return os.path.isfile(cache_path) | 62 return os.path.isfile(cache_path) |
33 | 63 |
34 def read(self, path): | 64 def read(self, path): |
35 cache_path = self.getCachePath(path) | 65 cache_path = self.getCachePath(path) |
66 logger.debug("Reading cache: %s" % cache_path) | |
36 with codecs.open(cache_path, 'r', 'utf-8') as fp: | 67 with codecs.open(cache_path, 'r', 'utf-8') as fp: |
37 return fp.read() | 68 return fp.read() |
38 | 69 |
39 def write(self, path, content): | 70 def write(self, path, content): |
40 cache_path = self.getCachePath(path) | 71 cache_path = self.getCachePath(path) |
42 if not os.path.isdir(cache_dir): | 73 if not os.path.isdir(cache_dir): |
43 os.makedirs(cache_dir, 0755) | 74 os.makedirs(cache_dir, 0755) |
44 with codecs.open(cache_path, 'w', 'utf-8') as fp: | 75 with codecs.open(cache_path, 'w', 'utf-8') as fp: |
45 fp.write(content) | 76 fp.write(content) |
46 | 77 |
78 def getCachePath(self, path): | |
79 if path.startswith('.'): | |
80 path = '__index__' + path | |
81 return os.path.join(self.base_dir, path) | |
82 | |
83 | |
84 class NullCache(object): | |
85 def isValid(self, path, time): | |
86 return False | |
87 | |
88 def getCacheTime(self, path): | |
89 return None | |
90 | |
91 def has(self, path): | |
92 return False | |
93 | |
94 def read(self, path): | |
95 raise Exception("Null cache has no data.") | |
96 | |
97 def write(self, path, content): | |
98 pass | |
99 | |
100 def getCachePath(self, path): | |
101 raise Exception("Null cache can't make paths.") | |
102 | |
103 | |
104 class NullExtensibleCache(object): | |
105 def __init__(self): | |
106 self.null_cache = NullCache() | |
107 | |
108 @property | |
109 def enabled(self): | |
110 return False | |
111 | |
112 def getCache(self, name): | |
113 return self.null_cache | |
114 |