Mercurial > piecrust2
comparison piecrust/cache.py @ 0:a212a3f2e3ee
Initial commit.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 21 Dec 2013 14:44:02 -0800 |
parents | |
children | f485ba500df3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a212a3f2e3ee |
---|---|
1 import os | |
2 import os.path | |
3 import codecs | |
4 | |
5 | |
6 class SimpleCache(object): | |
7 def __init__(self, base_dir): | |
8 if not os.path.isdir(base_dir): | |
9 os.makedirs(base_dir, 0755) | |
10 self.base_dir = base_dir | |
11 | |
12 def isValid(self, path, time): | |
13 cache_time = self.getCacheTime(path) | |
14 if cache_time is None: | |
15 return False | |
16 if isinstance(time, list): | |
17 for t in time: | |
18 if cache_time < t: | |
19 return False | |
20 return True | |
21 return cache_time >= time | |
22 | |
23 def getCacheTime(self, path): | |
24 cache_path = self.getCachePath(path) | |
25 try: | |
26 return os.path.getmtime(cache_path) | |
27 except os.error: | |
28 return None | |
29 | |
30 def has(self, path): | |
31 cache_path = self.getCachePath(path) | |
32 return os.path.isfile(cache_path) | |
33 | |
34 def read(self, path): | |
35 cache_path = self.getCachePath(path) | |
36 with codecs.open(cache_path, 'r', 'utf-8') as fp: | |
37 return fp.read() | |
38 | |
39 def write(self, path, content): | |
40 cache_path = self.getCachePath(path) | |
41 cache_dir = os.path.dirname(cache_path) | |
42 if not os.path.isdir(cache_dir): | |
43 os.makedirs(cache_dir, 0755) | |
44 with codecs.open(cache_path, 'w', 'utf-8') as fp: | |
45 fp.write(content) | |
46 |