blob: d3176d1e0f04f05f25e5c4068e81e67c3834bab2 [file] [log] [blame]
Norman James92053022015-09-02 22:26:47 -05001#!/usr/bin/env python
2
3import sys
4import cPickle
5import os
6import Openbmc
Norman James92053022015-09-02 22:26:47 -05007import dbus
8import dbus.service
9import dbus.mainloop.glib
Norman James5d78b4d2015-09-05 13:34:34 -050010from gi.repository import Gio, GLib, GObject
Norman James92053022015-09-02 22:26:47 -050011
12
Norman James5d78b4d2015-09-05 13:34:34 -050013class 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 James92053022015-09-02 22:26:47 -050031 directory = obj_path.replace('/','.')
Norman James5d78b4d2015-09-05 13:34:34 -050032 directory = self.save_path+directory.lstrip('.')
33 filename = directory+"/"+prop_interface
34 if (os.path.isfile(filename) == False):
Norman James362a80f2015-09-14 14:04:39 -050035 pass
Norman James5d78b4d2015-09-05 13:34:34 -050036 ## not an error
Norman James362a80f2015-09-14 14:04:39 -050037 #print "No cache available for: "+filename
Norman James5d78b4d2015-09-05 13:34:34 -050038 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 James92053022015-09-02 22:26:47 -050047
Norman James5d78b4d2015-09-05 13:34:34 -050048 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 James5d78b4d2015-09-05 13:34:34 -050061 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 James92053022015-09-02 22:26:47 -050086
87