xref: /openbmc/pyphosphor/obmc/dbuslib/propertycacher.py (revision 0127d51c137dbe8063b81012763550faf5111d87)
1179b39b5SBrad Bishop# Contributors Listed Below - COPYRIGHT 2016
2179b39b5SBrad Bishop# [+] International Business Machines Corp.
3179b39b5SBrad Bishop#
4179b39b5SBrad Bishop#
5179b39b5SBrad Bishop# Licensed under the Apache License, Version 2.0 (the "License");
6179b39b5SBrad Bishop# you may not use this file except in compliance with the License.
7179b39b5SBrad Bishop# You may obtain a copy of the License at
8179b39b5SBrad Bishop#
9179b39b5SBrad Bishop#     http://www.apache.org/licenses/LICENSE-2.0
10179b39b5SBrad Bishop#
11179b39b5SBrad Bishop# Unless required by applicable law or agreed to in writing, software
12179b39b5SBrad Bishop# distributed under the License is distributed on an "AS IS" BASIS,
13179b39b5SBrad Bishop# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14179b39b5SBrad Bishop# implied. See the License for the specific language governing
15179b39b5SBrad Bishop# permissions and limitations under the License.
16179b39b5SBrad Bishop
17179b39b5SBrad Bishopimport os
18179b39b5SBrad Bishopimport cPickle
19179b39b5SBrad Bishopimport json
20179b39b5SBrad Bishop
21179b39b5SBrad BishopCACHE_PATH = '/var/cache/obmc/'
22179b39b5SBrad Bishop
23179b39b5SBrad Bishop
24179b39b5SBrad Bishopdef getCacheFilename(obj_path, iface_name):
25179b39b5SBrad Bishop    name = obj_path.replace('/', '.')
26179b39b5SBrad Bishop    filename = CACHE_PATH+name[1:]+"@"+iface_name+".props"
27179b39b5SBrad Bishop    return filename
28179b39b5SBrad Bishop
29179b39b5SBrad Bishop
30179b39b5SBrad Bishopdef save(obj_path, iface_name, properties):
31179b39b5SBrad Bishop    print "Caching: "+obj_path
32179b39b5SBrad Bishop    filename = getCacheFilename(obj_path, iface_name)
33d637227aSBrad Bishop    parent = os.path.dirname(filename)
34d637227aSBrad Bishop    try:
35d637227aSBrad Bishop        if not os.path.exists(parent):
36d637227aSBrad Bishop            os.makedirs(parent)
37*0127d51cSBrad Bishop        with open(filename, 'wb') as output:
38179b39b5SBrad Bishop            try:
39179b39b5SBrad Bishop                ## use json module to convert dbus datatypes
40179b39b5SBrad Bishop                props = json.dumps(properties[iface_name])
41179b39b5SBrad Bishop                prop_obj = json.loads(props)
42179b39b5SBrad Bishop                cPickle.dump(prop_obj, output)
43179b39b5SBrad Bishop            except Exception as e:
44179b39b5SBrad Bishop                print "ERROR: "+str(e)
45179b39b5SBrad Bishop    except:
46179b39b5SBrad Bishop        print "ERROR opening cache file: "+filename
47179b39b5SBrad Bishop
48179b39b5SBrad Bishop
49179b39b5SBrad Bishopdef load(obj_path, iface_name, properties):
50179b39b5SBrad Bishop    ## overlay with pickled data
51179b39b5SBrad Bishop    filename = getCacheFilename(obj_path, iface_name)
52179b39b5SBrad Bishop    if (os.path.isfile(filename)):
53179b39b5SBrad Bishop        if iface_name in properties:
54179b39b5SBrad Bishop            properties[iface_name] = {}
55179b39b5SBrad Bishop        print "Loading from cache: "+filename
56179b39b5SBrad Bishop        try:
57179b39b5SBrad Bishop            p = open(filename, 'rb')
58179b39b5SBrad Bishop            data = cPickle.load(p)
59179b39b5SBrad Bishop            for prop in data.keys():
60179b39b5SBrad Bishop                properties[iface_name][prop] = data[prop]
61179b39b5SBrad Bishop
62179b39b5SBrad Bishop        except Exception as e:
63179b39b5SBrad Bishop            print "ERROR: Loading cache file: " + str(e)
64179b39b5SBrad Bishop        finally:
65179b39b5SBrad Bishop            p.close()
66