blob: 8ac9e55f396fc15b803a800f66d5a60fe95572a4 [file] [log] [blame]
Brad Bishopc656a862016-07-25 09:11:41 -04001# Contributors Listed Below - COPYRIGHT 2016
2# [+] International Business Machines Corp.
3#
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied. See the License for the specific language governing
15# permissions and limitations under the License.
16
17import sys
18import dbus
19import dbus.mainloop.glib
20import gobject
21import obmc.mapper
22
23
24class Wait(object):
25 def __init__(self, bus, waitlist, *a, **kw):
26 self.bus = bus
27 self.waitlist = dict(zip(waitlist, [None]*len(waitlist)))
28 mapper = bus.get_object(
29 obmc.mapper.MAPPER_NAME,
30 obmc.mapper.MAPPER_PATH,
31 introspect=False)
32 self.mapper_iface = dbus.Interface(
33 mapper, dbus_interface=obmc.mapper.MAPPER_IFACE)
34 self.done = False
35 self.callback = kw.pop('callback', None)
36 self.callback_keyword = kw.pop('keyword', None)
37 self.callback_args = a
38 self.callback_kw = kw
39
40 self.bus.add_signal_receiver(
41 self.name_owner_changed_handler,
42 dbus_interface=dbus.BUS_DAEMON_IFACE,
43 signal_name='NameOwnerChanged')
44 self.bus.add_signal_receiver(
45 self.interfaces_added_handler,
46 dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager',
47 sender_keyword='sender',
48 signal_name='InterfacesAdded')
49 gobject.idle_add(self.name_owner_changed_handler)
50
51 def check_done(self):
52 if not all(self.waitlist.values()) or self.done:
53 return
54
55 self.done = True
56 self.bus.remove_signal_receiver(
57 self.name_owner_changed_handler,
58 dbus_interface=dbus.BUS_DAEMON_IFACE,
59 signal_name='NameOwnerChanged')
60 self.bus.remove_signal_receiver(
61 self.interfaces_added_handler,
62 dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager',
63 signal_name='InterfacesAdded')
64 if self.callback:
65 if self.callback_keyword:
66 self.callback_kw[self.callback_keyword] = self.waitlist
67 self.callback(*self.callback_args, **self.callback_kw)
68
69 def get_object_callback(self, path, info):
70 self.waitlist[path] = list(info)[0]
71 self.check_done()
72
73 def name_owner_changed_handler(self, *a, **kw):
74 class Callback(object):
75 def __init__(self, func, *args):
76 self.func = func
77 self.extra_args = args
78
79 def __call__(self, *a):
80 return self.func(*(self.extra_args + a))
81
82 if self.done:
83 return
84
85 for path in self.waitlist.keys():
86 method = getattr(self.mapper_iface, 'GetObject')
87 method.call_async(
88 path,
89 reply_handler=Callback(self.get_object_callback, path))
90
91 def interfaces_added_handler(self, path, *a, **kw):
92 if self.done:
93 return
94 if path in self.waitlist.keys():
95 self.waitlist[path] = kw['sender']
96 self.check_done()