add a conversion class for datetime and time stamps, since I need this all the time.
[monitor.git] / monitor / database / dbpickle.py
1 import os
2 import sys
3 import pickle
4 import inspect
5 import shutil
6 import time
7 from monitor import config
8
9 noserial=False
10 try:
11         from util.PHPSerialize import *
12         from util.PHPUnserialize import *
13 except:
14         #print >>sys.stderr, "PHPSerial db type not allowed."
15         noserial=True
16
17 DEBUG= 0
18 PICKLE_PATH=config.MONITOR_DATA_ROOT
19
20 def lastModified(name, type=None):
21         # TODO: fix for 'debug' mode also.
22         t = SPickle().mtime("production.%s" % name, type)
23         return t
24
25 def escapeName(name):
26         """
27                 automatically escape names passed to the db to make sensible-filenames without
28                 exposing this to users.
29         """
30         return name.replace("/", "_")
31
32 def cachedRecently(name, length=int(config.cachetime), type=None):
33         """
34                 return true or false based on whether the modified time of the cached
35                 file is within 'length' minutes.
36         """
37         name = name.replace("/", "_")
38         if hasattr(config, 'cachecalls') and not config.cachecalls:
39                 # don't use cached calls if cachecalls is false
40                 return False
41
42         try:
43                 t = lastModified(name, type)
44         except:
45                 # file doesn't exist or we can't access it.
46                 return False
47
48         current_time = time.time()
49         if current_time > t + length*60:
50                 return False
51         else:
52                 return True
53
54 def dbLoad(name, type=None):
55         name = escapeName(name)
56         return SPickle().load(name, type)
57
58 def dbExists(name, type=None):
59         name = escapeName(name)
60         return SPickle().exists(name, type)
61
62 def dbDump(name, obj=None, type=None):
63         name = escapeName(name)
64         # depth of the dump is 2 now, since we're redirecting to '.dump'
65         return SPickle().dump(name, obj, type, 2)
66
67 def if_cached_else_refresh(cond, refresh, name, function, type=None):
68         s = SPickle()
69         if refresh:
70                 if not config.debug and s.exists("production.%s" % name, type):
71                         s.remove("production.%s" % name, type)
72                 if config.debug and s.exists("debug.%s" % name, type):
73                         s.remove("debug.%s" % name, type)
74
75         return if_cached_else(cond, name, function, type)
76
77 def if_cached_else(cond, name, function, type=None):
78         s = SPickle()
79         if (cond and s.exists("production.%s" % name, type)) or \
80            (cond and config.debug and s.exists("debug.%s" % name, type)):
81                 o = s.load(name, type)
82         else:
83                 o = function()
84                 if cond:
85                         s.dump(name, o, type)   # cache the object using 'name'
86                         o = s.load(name, type)
87                 # TODO: what if 'o' hasn't been converted...
88         return o
89
90 class SPickle:
91         def __init__(self, path=PICKLE_PATH):
92                 self.path = path
93
94         def if_cached_else(self, cond, name, function, type=None):
95                 if cond and self.exists("production.%s" % name, type):
96                         o = self.load(name, type)
97                 else:
98                         o = function()
99                         if cond:
100                                 self.dump(name, o, type)        # cache the object using 'name'
101                 return o
102
103         def __file(self, name, type=None):
104                 if type == None:
105                         return "%s/%s.pkl" % (self.path, name)
106                 else:
107                         if noserial:
108                                 raise Exception("No PHPSerializer module available")
109
110                         return "%s/%s.phpserial" % (self.path, name)
111
112         def mtime(self, name, type=None):
113                 f = os.stat(self.__file(name, type))
114                 return f[-2]
115                 
116         def exists(self, name, type=None):
117                 return os.path.exists(self.__file(name, type))
118
119         def remove(self, name, type=None):
120                 return os.remove(self.__file(name, type))
121
122         def load(self, name, type=None):
123                 """ 
124                 In debug mode, we should fail if neither file exists.
125                         if the debug file exists, reset name
126                         elif the original file exists, make a copy, reset name
127                         else neither exist, raise an error
128                 Otherwise, it's normal mode, if the file doesn't exist, raise error
129                 Load the file
130                 """
131                 #print "loading %s" % name
132
133                 if config.debug:
134                         if self.exists("debug.%s" % name, type):
135                                 name = "debug.%s" % name
136                         elif self.exists("production.%s" % name, type):
137                                 debugname = "debug.%s" % name
138                                 if not self.exists(debugname, type):
139                                         name = "production.%s" % name
140                                         shutil.copyfile(self.__file(name, type), self.__file(debugname, type))
141                                 name = debugname
142                         else:   # neither exist
143                                 raise Exception, "No such pickle based on %s" % self.__file("debug.%s" % name, type)
144                 else:
145                         if   self.exists("production.%s" % name, type):
146                                 name = "production.%s" % name
147                         elif self.exists(name, type):
148                                 name = name
149                         else:
150                                 raise Exception, "No such file %s" % name
151                                 
152
153                 #import traceback
154                 #print traceback.print_stack()
155                 #print "loading %s" % self.__file(name, type)
156                 #sys.stderr.write("-----------------------------\n")
157                 f = open(self.__file(name, type), 'r')
158                 if type == None:
159                         o = pickle.load(f)
160                 else:
161                         if noserial:
162                                 raise Exception("No PHPSerializer module available")
163                         s = PHPUnserialize()
164                         o = s.unserialize(f.read())
165                 f.close()
166                 return o
167                         
168         
169         # use the environment to extract the data associated with the local
170         # variable 'name'
171         def dump(self, name, obj=None, type=None, depth=1):
172                 if obj == None:
173                         o = inspect.getouterframes(inspect.currentframe())
174                         up1 = o[depth][0] # get the frame one prior to (up from) this frame
175                         argvals = inspect.getargvalues(up1)
176                         # TODO: check that 'name' is a local variable; otherwise this would fail.
177                         obj = argvals[3][name] # extract the local variable name 'name'
178                 if not os.path.isdir("%s/" % self.path):
179                         os.mkdir("%s" % self.path)
180                 if config.debug:
181                         name = "debug.%s" % name
182                 else:
183                         name = "production.%s" % name
184                 f = open(self.__file(name, type), 'w')
185                 if type == None:
186                         pickle.dump(obj, f)
187                 else:
188                         if noserial:
189                                 raise Exception("No PHPSerializer module available")
190                         s = PHPSerialize()
191                         f.write(s.serialize(obj))
192                 f.close()
193                 return