Norman James | ef1cbfa | 2015-10-29 06:18:14 -0500 | [diff] [blame] | 1 | import os |
| 2 | import cPickle |
| 3 | import Openbmc |
| 4 | |
| 5 | CACHE_PATH = '/var/cache/obmc/' |
| 6 | |
| 7 | def getCacheFilename(obj_path, iface_name): |
| 8 | name = obj_path.replace('/','.') |
| 9 | filename = CACHE_PATH+name[1:]+"@"+iface_name+".props" |
| 10 | return filename |
| 11 | |
| 12 | def save(obj_path, iface_name, properties): |
| 13 | print "Caching: "+obj_path |
| 14 | try: |
| 15 | output = open(getCacheFilename(obj_path, iface_name), 'wb') |
| 16 | ## save properties |
| 17 | dbus_props = {} |
| 18 | |
| 19 | for p in properties[iface_name].keys(): |
| 20 | dbus_prop = Openbmc.DbusVariable(p,properties[iface_name][p]) |
| 21 | dbus_props[str(p)] = dbus_prop.getBaseValue() |
| 22 | |
| 23 | cPickle.dump(dbus_props,output) |
| 24 | except Exception as e: |
| 25 | print "ERROR: "+str(e) |
| 26 | finally: |
| 27 | output.close() |
| 28 | |
| 29 | def load(obj_path, iface_name, properties): |
| 30 | ## overlay with pickled data |
| 31 | filename=getCacheFilename(obj_path, iface_name) |
| 32 | if (os.path.isfile(filename)): |
| 33 | if (properties.has_key(iface_name) == False): |
| 34 | properties[iface_name] = {} |
| 35 | print "Loading from cache: "+filename |
| 36 | try: |
| 37 | p = open(filename, 'rb') |
| 38 | data = cPickle.load(p) |
| 39 | for prop in data.keys(): |
| 40 | properties[iface_name][prop] = data[prop] |
| 41 | |
| 42 | except Exception as e: |
| 43 | print "ERROR: Loading cache file: " +str(e) |
| 44 | finally: |
| 45 | p.close() |
| 46 | |
| 47 | |