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