diff options
| author | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2016-05-12 16:45:57 -0400 |
|---|---|---|
| committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2016-05-17 10:04:29 -0400 |
| commit | 179b39b5e30c8ac23d885e4d0826131756dcd1ac (patch) | |
| tree | c06b403e11c1c2f2248d553e712e1102522e70e5 /obmc/dbuslib | |
| parent | 1b3c05c1f44185f8176f46829a26ad6e50d0831f (diff) | |
| download | pyphosphor-179b39b5e30c8ac23d885e4d0826131756dcd1ac.tar.gz pyphosphor-179b39b5e30c8ac23d885e4d0826131756dcd1ac.zip | |
Move sensors.py and propertycacher.py frm skeleton
These skeleton module are somewhat general purpose so move them
here.
This is just a straight copy plus a pep8 run.
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Diffstat (limited to 'obmc/dbuslib')
| -rw-r--r-- | obmc/dbuslib/propertycacher.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/obmc/dbuslib/propertycacher.py b/obmc/dbuslib/propertycacher.py new file mode 100644 index 0000000..701a413 --- /dev/null +++ b/obmc/dbuslib/propertycacher.py @@ -0,0 +1,64 @@ +# Contributors Listed Below - COPYRIGHT 2016 +# [+] International Business Machines Corp. +# +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. See the License for the specific language governing +# permissions and limitations under the License. + +import os +import cPickle +import json + +CACHE_PATH = '/var/cache/obmc/' + + +def getCacheFilename(obj_path, iface_name): + name = obj_path.replace('/', '.') + filename = CACHE_PATH+name[1:]+"@"+iface_name+".props" + return filename + + +def save(obj_path, iface_name, properties): + print "Caching: "+obj_path + try: + filename = getCacheFilename(obj_path, iface_name) + output = open(filename, 'wb') + try: + ## use json module to convert dbus datatypes + props = json.dumps(properties[iface_name]) + prop_obj = json.loads(props) + cPickle.dump(prop_obj, output) + except Exception as e: + print "ERROR: "+str(e) + finally: + output.close() + except: + print "ERROR opening cache file: "+filename + + +def load(obj_path, iface_name, properties): + ## overlay with pickled data + filename = getCacheFilename(obj_path, iface_name) + if (os.path.isfile(filename)): + if iface_name in properties: + properties[iface_name] = {} + print "Loading from cache: "+filename + try: + p = open(filename, 'rb') + data = cPickle.load(p) + for prop in data.keys(): + properties[iface_name][prop] = data[prop] + + except Exception as e: + print "ERROR: Loading cache file: " + str(e) + finally: + p.close() |

