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