Norman James | ef1cbfa | 2015-10-29 06:18:14 -0500 | [diff] [blame] | 1 | import os |
| 2 | import cPickle |
Norman James | fdb1f90 | 2015-10-31 17:32:45 -0500 | [diff] [blame] | 3 | import json |
Norman James | ef1cbfa | 2015-10-29 06:18:14 -0500 | [diff] [blame] | 4 | import Openbmc |
| 5 | |
| 6 | CACHE_PATH = '/var/cache/obmc/' |
| 7 | |
| 8 | def getCacheFilename(obj_path, iface_name): |
| 9 | name = obj_path.replace('/','.') |
| 10 | filename = CACHE_PATH+name[1:]+"@"+iface_name+".props" |
| 11 | return filename |
| 12 | |
| 13 | def save(obj_path, iface_name, properties): |
| 14 | print "Caching: "+obj_path |
| 15 | try: |
Norman James | fdb1f90 | 2015-10-31 17:32:45 -0500 | [diff] [blame] | 16 | |
| 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 James | ef1cbfa | 2015-10-29 06:18:14 -0500 | [diff] [blame] | 30 | |
Norman James | ef1cbfa | 2015-10-29 06:18:14 -0500 | [diff] [blame] | 31 | |
| 32 | def 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 | |