comparison MoinMoin/caching.py @ 0:77665d8e2254

tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0 (automatically generated log message) imported from: moin--main--1.5--base-0
author Thomas Waldmann <tw-public@gmx.de>
date Thu, 22 Sep 2005 15:09:50 +0000
parents
children c04edbe29fac
comparison
equal deleted inserted replaced
-1:000000000000 0:77665d8e2254
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin caching module
4
5 @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>
6 @license: GNU GPL, see COPYING for details.
7 """
8
9 import os
10 from MoinMoin import config
11 from MoinMoin.util import filesys
12
13 class CacheEntry:
14 def __init__(self, request, arena, key):
15 """ init a cache entry
16 @param request: the request object
17 @param arena: either a string or a page object, when we want to use
18 page local cache area
19 @param key: under which key we access the cache content
20 """
21 if isinstance(arena, str):
22 self.arena_dir = os.path.join(request.cfg.cache_dir, arena)
23 filesys.makeDirs(self.arena_dir)
24 else: # arena is in fact a page object
25 cache_dir = None
26 self.arena_dir = arena.getPagePath('cache', check_create=1)
27 self.key = key
28
29 def _filename(self):
30 return os.path.join(self.arena_dir, self.key)
31
32 def exists(self):
33 return os.path.exists(self._filename())
34
35 def mtime(self):
36 try:
37 return os.path.getmtime(self._filename())
38 except (IOError, OSError):
39 return 0
40
41 def needsUpdate(self, filename, attachdir=None):
42 if not self.exists(): return 1
43
44 try:
45 ctime = os.path.getmtime(self._filename())
46 ftime = os.path.getmtime(filename)
47 except os.error:
48 return 1
49
50 needsupdate = ftime > ctime
51
52 # if a page depends on the attachment dir, we check this, too:
53 if not needsupdate and attachdir:
54 try:
55 ftime2 = os.path.getmtime(attachdir)
56 except os.error:
57 ftime2 = 0
58 needsupdate = ftime2 > ctime
59
60 return needsupdate
61
62 def copyto(self, filename):
63 import shutil
64 shutil.copyfile(filename, self._filename())
65
66 try:
67 os.chmod(self._filename(), 0666 & config.umask)
68 except OSError:
69 pass
70
71 def update(self, content, encode=False):
72 if encode:
73 content = content.encode(config.charset)
74 open(self._filename(), 'wb').write(content)
75
76 try:
77 os.chmod(self._filename(), 0666 & config.umask)
78 except OSError:
79 pass
80
81 def remove(self):
82 try:
83 os.remove(self._filename())
84 except OSError:
85 pass
86
87 def content(self, decode=False):
88 data = open(self._filename(), 'rb').read()
89 if decode:
90 data = data.decode(config.charset)
91 return data
92