blob: b10edcbcb0482367056a8cb353b594062dfb326d [file] [log] [blame]
Norman Jamesef1cbfa2015-10-29 06:18:14 -05001import os
2import cPickle
Norman Jamesfdb1f902015-10-31 17:32:45 -05003import json
Norman Jamesef1cbfa2015-10-29 06:18:14 -05004import Openbmc
5
6CACHE_PATH = '/var/cache/obmc/'
7
8def getCacheFilename(obj_path, iface_name):
9 name = obj_path.replace('/','.')
10 filename = CACHE_PATH+name[1:]+"@"+iface_name+".props"
11 return filename
12
13def save(obj_path, iface_name, properties):
14 print "Caching: "+obj_path
15 try:
Norman Jamesfdb1f902015-10-31 17:32:45 -050016
17 filename = getCacheFilename(obj_path, iface_name)
18 output = open(filename, 'wb')
19 try:
20 ## use json module to convert dbus datatypes
21 props = json.dumps(properties[iface_name])
22 prop_obj = json.loads(props)
23 cPickle.dump(prop_obj,output)
24 except Exception as e:
25 print "ERROR: "+str(e)
26 finally:
27 output.close()
28 except:
29 print "ERROR opening cache file: "+filename
Norman Jamesef1cbfa2015-10-29 06:18:14 -050030
Norman Jamesef1cbfa2015-10-29 06:18:14 -050031
32def load(obj_path, iface_name, properties):
33 ## overlay with pickled data
34 filename=getCacheFilename(obj_path, iface_name)
35 if (os.path.isfile(filename)):
36 if (properties.has_key(iface_name) == False):
37 properties[iface_name] = {}
38 print "Loading from cache: "+filename
39 try:
40 p = open(filename, 'rb')
41 data = cPickle.load(p)
42 for prop in data.keys():
43 properties[iface_name][prop] = data[prop]
44
45 except Exception as e:
46 print "ERROR: Loading cache file: " +str(e)
47 finally:
48 p.close()
49
50