xref: /openbmc/pyphosphor/obmc/dbuslib/propertycacher.py (revision aea38c650b8554e8f6293483286eba18a5547cd9)
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
17*aea38c65SBrad Bishopimport json
18179b39b5SBrad Bishopimport os
199172f3e7SCamVan Nguyen# TODO: openbmc/openbmc#2994 remove python 2 support
209172f3e7SCamVan Nguyenimport sys
219172f3e7SCamVan Nguyenif sys.version_info[0] < 3:
229172f3e7SCamVan Nguyen    import cPickle as pickle
239172f3e7SCamVan Nguyenelse:
249172f3e7SCamVan Nguyen    import pickle
25179b39b5SBrad Bishop
26179b39b5SBrad BishopCACHE_PATH = '/var/cache/obmc/'
27179b39b5SBrad Bishop
28179b39b5SBrad Bishop
29179b39b5SBrad Bishopdef getCacheFilename(obj_path, iface_name):
30179b39b5SBrad Bishop    name = obj_path.replace('/', '.')
31179b39b5SBrad Bishop    filename = CACHE_PATH + name[1:] + "@" + iface_name + ".props"
32179b39b5SBrad Bishop    return filename
33179b39b5SBrad Bishop
34179b39b5SBrad Bishop
35179b39b5SBrad Bishopdef save(obj_path, iface_name, properties):
369172f3e7SCamVan Nguyen    print("Caching: "+ obj_path)
37179b39b5SBrad Bishop    filename = getCacheFilename(obj_path, iface_name)
38d637227aSBrad Bishop    parent = os.path.dirname(filename)
39d637227aSBrad Bishop    try:
40d637227aSBrad Bishop        if not os.path.exists(parent):
41d637227aSBrad Bishop            os.makedirs(parent)
420127d51cSBrad Bishop        with open(filename, 'wb') as output:
43179b39b5SBrad Bishop            try:
44*aea38c65SBrad Bishop                # use json module to convert dbus datatypes
45179b39b5SBrad Bishop                props = json.dumps(properties[iface_name])
46179b39b5SBrad Bishop                prop_obj = json.loads(props)
479172f3e7SCamVan Nguyen                pickle.dump(prop_obj, output)
48179b39b5SBrad Bishop            except Exception as e:
499172f3e7SCamVan Nguyen                print("ERROR: " + str(e))
50*aea38c65SBrad Bishop    except Exception:
519172f3e7SCamVan Nguyen        print("ERROR opening cache file: " + filename)
52179b39b5SBrad Bishop
53179b39b5SBrad Bishop
54179b39b5SBrad Bishopdef load(obj_path, iface_name, properties):
55*aea38c65SBrad Bishop    # overlay with pickled data
56179b39b5SBrad Bishop    filename = getCacheFilename(obj_path, iface_name)
57179b39b5SBrad Bishop    if (os.path.isfile(filename)):
58179b39b5SBrad Bishop        if iface_name in properties:
59179b39b5SBrad Bishop            properties[iface_name] = {}
609172f3e7SCamVan Nguyen        print("Loading from cache: " + filename)
61179b39b5SBrad Bishop        try:
62179b39b5SBrad Bishop            p = open(filename, 'rb')
639172f3e7SCamVan Nguyen            data = pickle.load(p)
649172f3e7SCamVan Nguyen            for prop in list(data.keys()):
65179b39b5SBrad Bishop                properties[iface_name][prop] = data[prop]
66179b39b5SBrad Bishop
67179b39b5SBrad Bishop        except Exception as e:
689172f3e7SCamVan Nguyen            print("ERROR: Loading cache file: " + str(e))
69179b39b5SBrad Bishop        finally:
70179b39b5SBrad Bishop            p.close()
71