6 from PHPSerialize import *
7 from PHPUnserialize import *
9 #print >>sys.stderr, "PHPSerial db type not allowed."
14 from config import config as cfg
20 PICKLE_PATH=monitorconfig.MONITOR_DATA_ROOT
22 class ExceptionTimeout(Exception): pass
24 def dbLoad(name, type=None):
25 return SPickle().load(name, type)
27 def dbExists(name, type=None):
28 #if self.config.debug:
29 # name = "debug.%s" % name
30 return SPickle().exists(name, type)
32 def dbDump(name, obj=None, type=None):
33 # depth of the dump is 2 now, since we're redirecting to '.dump'
34 return SPickle().dump(name, obj, type, 2)
36 def if_cached_else_refresh(cond, refresh, name, function, type=None):
39 if not config.debug and s.exists("production.%s" % name, type):
40 s.remove("production.%s" % name, type)
41 if config.debug and s.exists("debug.%s" % name, type):
42 s.remove("debug.%s" % name, type)
44 return if_cached_else(cond, name, function, type)
46 def if_cached_else(cond, name, function, type=None):
48 if (cond and s.exists("production.%s" % name, type)) or \
49 (cond and config.debug and s.exists("debug.%s" % name, type)):
50 o = s.load(name, type)
54 s.dump(name, o, type) # cache the object using 'name'
55 o = s.load(name, type)
56 # TODO: what if 'o' hasn't been converted...
60 def __init__(self, path=PICKLE_PATH):
63 def if_cached_else(self, cond, name, function, type=None):
64 if cond and self.exists("production.%s" % name, type):
65 o = self.load(name, type)
69 self.dump(name, o, type) # cache the object using 'name'
72 def __file(self, name, type=None):
74 return "%s/%s.pkl" % (self.path, name)
77 raise Exception("No PHPSerializer module available")
79 return "%s/%s.phpserial" % (self.path, name)
81 def exists(self, name, type=None):
82 return os.path.exists(self.__file(name, type))
84 def remove(self, name, type=None):
85 return os.remove(self.__file(name, type))
87 def load(self, name, type=None):
89 In debug mode, we should fail if neither file exists.
90 if the debug file exists, reset name
91 elif the original file exists, make a copy, reset name
92 else neither exist, raise an error
93 Otherwise, it's normal mode, if the file doesn't exist, raise error
98 if self.exists("debug.%s" % name, type):
99 name = "debug.%s" % name
100 elif self.exists("production.%s" % name, type):
101 debugname = "debug.%s" % name
102 if not self.exists(debugname, type):
103 name = "production.%s" % name
104 shutil.copyfile(self.__file(name, type), self.__file(debugname, type))
106 else: # neither exist
107 raise Exception, "No such pickle based on %s" % self.__file("debug.%s" % name, type)
109 if self.exists("production.%s" % name, type):
110 name = "production.%s" % name
111 elif self.exists(name, type):
114 raise Exception, "No such file %s" % name
117 #print "loading %s" % self.__file(name, type)
118 f = open(self.__file(name, type), 'r')
123 raise Exception("No PHPSerializer module available")
125 o = s.unserialize(f.read())
130 # use the environment to extract the data associated with the local
132 def dump(self, name, obj=None, type=None, depth=1):
134 o = inspect.getouterframes(inspect.currentframe())
135 up1 = o[depth][0] # get the frame one prior to (up from) this frame
136 argvals = inspect.getargvalues(up1)
137 # TODO: check that 'name' is a local variable; otherwise this would fail.
138 obj = argvals[3][name] # extract the local variable name 'name'
139 if not os.path.isdir("%s/" % self.path):
140 os.mkdir("%s" % self.path)
142 name = "debug.%s" % name
144 name = "production.%s" % name
145 f = open(self.__file(name, type), 'w')
150 raise Exception("No PHPSerializer module available")
152 f.write(s.serialize(obj))