blob: b89ed129154a51784879869d5ec9ad655e345d9b [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 James6f8d0422015-09-14 18:48:00 -050010#from gi.repository import Gio, GLib, GObject
11import gobject
Norman James92053022015-09-02 22:26:47 -050012
13
Norman James5d78b4d2015-09-05 13:34:34 -050014class 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)
26 dbus_prop = Openbmc.DbusProperty(prop,tmp_val)
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 James92053022015-09-02 22:26:47 -050032 directory = obj_path.replace('/','.')
Norman James5d78b4d2015-09-05 13:34:34 -050033 directory = self.save_path+directory.lstrip('.')
34 filename = directory+"/"+prop_interface
35 if (os.path.isfile(filename) == False):
Norman James362a80f2015-09-14 14:04:39 -050036 pass
Norman James5d78b4d2015-09-05 13:34:34 -050037 ## not an error
Norman James362a80f2015-09-14 14:04:39 -050038 #print "No cache available for: "+filename
Norman James5d78b4d2015-09-05 13:34:34 -050039 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 James92053022015-09-02 22:26:47 -050048
Norman James5d78b4d2015-09-05 13:34:34 -050049 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 James5d78b4d2015-09-05 13:34:34 -050062 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():
69 dbus_prop = Openbmc.DbusProperty(prop,all_properties[prop])
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 James92053022015-09-02 22:26:47 -050087
88