blob: ce83195922d7a4b1d3e472ad89b3fc2c3752845f [file] [log] [blame]
Brad Bishop179b39b2016-05-12 16:45:57 -04001# Contributors Listed Below - COPYRIGHT 2016
2# [+] International Business Machines Corp.
3#
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied. See the License for the specific language governing
15# permissions and limitations under the License.
16
17import os
18import cPickle
19import json
20
21CACHE_PATH = '/var/cache/obmc/'
22
23
24def getCacheFilename(obj_path, iface_name):
25 name = obj_path.replace('/', '.')
26 filename = CACHE_PATH+name[1:]+"@"+iface_name+".props"
27 return filename
28
29
30def save(obj_path, iface_name, properties):
31 print "Caching: "+obj_path
Brad Bishopd6372272016-07-26 23:01:42 -040032 filename = getCacheFilename(obj_path, iface_name)
33 parent = os.path.dirname(filename)
Brad Bishop179b39b2016-05-12 16:45:57 -040034 try:
Brad Bishopd6372272016-07-26 23:01:42 -040035 if not os.path.exists(parent):
36 os.makedirs(parent)
Brad Bishop179b39b2016-05-12 16:45:57 -040037 output = open(filename, 'wb')
38 try:
39 ## use json module to convert dbus datatypes
40 props = json.dumps(properties[iface_name])
41 prop_obj = json.loads(props)
42 cPickle.dump(prop_obj, output)
43 except Exception as e:
44 print "ERROR: "+str(e)
45 finally:
46 output.close()
47 except:
48 print "ERROR opening cache file: "+filename
49
50
51def load(obj_path, iface_name, properties):
52 ## overlay with pickled data
53 filename = getCacheFilename(obj_path, iface_name)
54 if (os.path.isfile(filename)):
55 if iface_name in properties:
56 properties[iface_name] = {}
57 print "Loading from cache: "+filename
58 try:
59 p = open(filename, 'rb')
60 data = cPickle.load(p)
61 for prop in data.keys():
62 properties[iface_name][prop] = data[prop]
63
64 except Exception as e:
65 print "ERROR: Loading cache file: " + str(e)
66 finally:
67 p.close()