| # Contributors Listed Below - COPYRIGHT 2016 |
| # [+] International Business Machines Corp. |
| # |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| # implied. See the License for the specific language governing |
| # permissions and limitations under the License. |
| |
| import os |
| import cPickle |
| import json |
| |
| CACHE_PATH = '/var/cache/obmc/' |
| |
| |
| def getCacheFilename(obj_path, iface_name): |
| name = obj_path.replace('/', '.') |
| filename = CACHE_PATH+name[1:]+"@"+iface_name+".props" |
| return filename |
| |
| |
| def save(obj_path, iface_name, properties): |
| print "Caching: "+obj_path |
| filename = getCacheFilename(obj_path, iface_name) |
| parent = os.path.dirname(filename) |
| try: |
| if not os.path.exists(parent): |
| os.makedirs(parent) |
| with open(filename, 'wb') as output: |
| try: |
| ## use json module to convert dbus datatypes |
| props = json.dumps(properties[iface_name]) |
| prop_obj = json.loads(props) |
| cPickle.dump(prop_obj, output) |
| except Exception as e: |
| print "ERROR: "+str(e) |
| except: |
| print "ERROR opening cache file: "+filename |
| |
| |
| def load(obj_path, iface_name, properties): |
| ## overlay with pickled data |
| filename = getCacheFilename(obj_path, iface_name) |
| if (os.path.isfile(filename)): |
| if iface_name in properties: |
| properties[iface_name] = {} |
| print "Loading from cache: "+filename |
| try: |
| p = open(filename, 'rb') |
| data = cPickle.load(p) |
| for prop in data.keys(): |
| properties[iface_name][prop] = data[prop] |
| |
| except Exception as e: |
| print "ERROR: Loading cache file: " + str(e) |
| finally: |
| p.close() |