blob: 875b164f01e061a5cdf21526e7c99217f2018027 [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
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060018# TODO: openbmc/openbmc#2994 remove python 2 support
19import sys
20if sys.version_info[0] < 3:
21 import cPickle as pickle
22else:
23 import pickle
Brad Bishop179b39b2016-05-12 16:45:57 -040024import json
25
26CACHE_PATH = '/var/cache/obmc/'
27
28
29def getCacheFilename(obj_path, iface_name):
30 name = obj_path.replace('/', '.')
31 filename = CACHE_PATH+name[1:]+"@"+iface_name+".props"
32 return filename
33
34
35def save(obj_path, iface_name, properties):
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060036 print("Caching: "+obj_path)
Brad Bishopd6372272016-07-26 23:01:42 -040037 filename = getCacheFilename(obj_path, iface_name)
38 parent = os.path.dirname(filename)
Brad Bishop179b39b2016-05-12 16:45:57 -040039 try:
Brad Bishopd6372272016-07-26 23:01:42 -040040 if not os.path.exists(parent):
41 os.makedirs(parent)
Brad Bishop0127d512016-07-28 13:52:29 -040042 with open(filename, 'wb') as output:
43 try:
44 ## use json module to convert dbus datatypes
45 props = json.dumps(properties[iface_name])
46 prop_obj = json.loads(props)
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060047 pickle.dump(prop_obj, output)
Brad Bishop0127d512016-07-28 13:52:29 -040048 except Exception as e:
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060049 print("ERROR: "+str(e))
Brad Bishop179b39b2016-05-12 16:45:57 -040050 except:
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060051 print("ERROR opening cache file: "+filename)
Brad Bishop179b39b2016-05-12 16:45:57 -040052
53
54def load(obj_path, iface_name, properties):
55 ## overlay with pickled data
56 filename = getCacheFilename(obj_path, iface_name)
57 if (os.path.isfile(filename)):
58 if iface_name in properties:
59 properties[iface_name] = {}
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060060 print("Loading from cache: "+filename)
Brad Bishop179b39b2016-05-12 16:45:57 -040061 try:
62 p = open(filename, 'rb')
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060063 data = pickle.load(p)
64 for prop in list(data.keys()):
Brad Bishop179b39b2016-05-12 16:45:57 -040065 properties[iface_name][prop] = data[prop]
66
67 except Exception as e:
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060068 print("ERROR: Loading cache file: " + str(e))
Brad Bishop179b39b2016-05-12 16:45:57 -040069 finally:
70 p.close()