blob: 701a413e4b511262d6c10b8c06f1e1e45e38a6bc [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
32 try:
33 filename = getCacheFilename(obj_path, iface_name)
34 output = open(filename, 'wb')
35 try:
36 ## use json module to convert dbus datatypes
37 props = json.dumps(properties[iface_name])
38 prop_obj = json.loads(props)
39 cPickle.dump(prop_obj, output)
40 except Exception as e:
41 print "ERROR: "+str(e)
42 finally:
43 output.close()
44 except:
45 print "ERROR opening cache file: "+filename
46
47
48def load(obj_path, iface_name, properties):
49 ## overlay with pickled data
50 filename = getCacheFilename(obj_path, iface_name)
51 if (os.path.isfile(filename)):
52 if iface_name in properties:
53 properties[iface_name] = {}
54 print "Loading from cache: "+filename
55 try:
56 p = open(filename, 'rb')
57 data = cPickle.load(p)
58 for prop in data.keys():
59 properties[iface_name][prop] = data[prop]
60
61 except Exception as e:
62 print "ERROR: Loading cache file: " + str(e)
63 finally:
64 p.close()