blob: 7888b46b82a140f0dd51edc45cfb998dd7424afb [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 Bishop0127d512016-07-28 13:52:29 -040037 with open(filename, 'wb') as output:
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)
Brad Bishop179b39b2016-05-12 16:45:57 -040045 except:
46 print "ERROR opening cache file: "+filename
47
48
49def load(obj_path, iface_name, properties):
50 ## overlay with pickled data
51 filename = getCacheFilename(obj_path, iface_name)
52 if (os.path.isfile(filename)):
53 if iface_name in properties:
54 properties[iface_name] = {}
55 print "Loading from cache: "+filename
56 try:
57 p = open(filename, 'rb')
58 data = cPickle.load(p)
59 for prop in data.keys():
60 properties[iface_name][prop] = data[prop]
61
62 except Exception as e:
63 print "ERROR: Loading cache file: " + str(e)
64 finally:
65 p.close()