Norman James | 9205302 | 2015-09-02 22:26:47 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import cPickle |
| 5 | import os |
| 6 | import Openbmc |
Norman James | 9205302 | 2015-09-02 22:26:47 -0500 | [diff] [blame] | 7 | import dbus |
| 8 | import dbus.service |
| 9 | import dbus.mainloop.glib |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 10 | from gi.repository import Gio, GLib, GObject |
Norman James | 9205302 | 2015-09-02 22:26:47 -0500 | [diff] [blame] | 11 | |
| 12 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 13 | class PropertyManager(): |
| 14 | def __init__(self,bus,save_path): |
| 15 | self.bus = bus |
| 16 | self.save_path = save_path |
| 17 | |
| 18 | def loadProperties(self,bus_name,obj_path,properties): |
| 19 | ## Load properties from system config |
| 20 | obj = self.bus.get_object(bus_name,obj_path) |
| 21 | dbus_properties = dbus.Interface(obj, 'org.freedesktop.DBus.Properties') |
| 22 | for prop_interface in properties.keys(): |
| 23 | for prop in properties[prop_interface]: |
| 24 | tmp_val = dbus_properties.Get(prop_interface,prop) |
| 25 | dbus_prop = Openbmc.DbusProperty(prop,tmp_val) |
| 26 | value = properties[prop_interface][prop] |
| 27 | dbus_prop.setValue(value) |
| 28 | dbus_properties.Set(prop_interface,prop,dbus_prop.getValue()) |
| 29 | |
| 30 | ## if save file exists, overlay properties from file |
Norman James | 9205302 | 2015-09-02 22:26:47 -0500 | [diff] [blame] | 31 | directory = obj_path.replace('/','.') |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 32 | directory = self.save_path+directory.lstrip('.') |
| 33 | filename = directory+"/"+prop_interface |
| 34 | if (os.path.isfile(filename) == False): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame^] | 35 | pass |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 36 | ## not an error |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame^] | 37 | #print "No cache available for: "+filename |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 38 | else: |
| 39 | try: |
| 40 | print "Loading from disk: "+obj_path |
| 41 | output = open(filename, 'rb') |
| 42 | dbus_props = cPickle.load(output) |
| 43 | output.close() |
| 44 | save_properties = dbus.Interface(obj, 'org.freedesktop.DBus.Properties') |
| 45 | for dbus_prop in dbus_props: |
| 46 | save_properties.Set(prop_interface,dbus_prop.getName(),dbus_prop.getValue()) |
Norman James | 9205302 | 2015-09-02 22:26:47 -0500 | [diff] [blame] | 47 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 48 | except Exception as e: |
| 49 | ## TODO: Error handling |
| 50 | print "Error loadFru: "+str(e) |
| 51 | |
| 52 | return None |
| 53 | |
| 54 | def saveProperties(self,bus_name,obj_path,interface_name,cache,properties): |
| 55 | obj = self.bus.get_object(bus_name,obj_path) |
| 56 | prop_intf = dbus.Interface(obj, 'org.freedesktop.DBus.Properties') |
| 57 | |
| 58 | for prop in properties.keys(): |
| 59 | print "Saving properties: "+prop |
| 60 | ## convert property to correct dbus type |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 61 | prop_intf.Set(interface_name,prop,properties[prop]) |
| 62 | |
| 63 | dbus_props = [] |
| 64 | if (cache): |
| 65 | print "Caching: "+obj_path |
| 66 | all_properties = prop_intf.GetAll(interface_name) |
| 67 | for prop in all_properties.keys(): |
| 68 | dbus_prop = Openbmc.DbusProperty(prop,all_properties[prop]) |
| 69 | dbus_props.append(dbus_prop) |
| 70 | |
| 71 | try: |
| 72 | directory = obj_path.replace('/','.') |
| 73 | directory = self.save_path+directory.lstrip('.') |
| 74 | filename = directory+"/"+interface_name |
| 75 | if not os.path.exists(directory): |
| 76 | os.makedirs(directory) |
| 77 | |
| 78 | output = open(filename, 'wb') |
| 79 | cPickle.dump(dbus_props,output) |
| 80 | output.close() |
| 81 | except Exception as e: |
| 82 | ## TODO: error handling |
| 83 | print str(e) |
| 84 | |
| 85 | return None |
Norman James | 9205302 | 2015-09-02 22:26:47 -0500 | [diff] [blame] | 86 | |
| 87 | |