This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
urwZOCP.py
executable file
·776 lines (665 loc) · 25.9 KB
/
urwZOCP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#!/usr/bin/python3
# Z25 Orchestror Control Protocol
# Copyright (c) 2013, Stichting z25.org, All rights reserved.
# Copyright (c) 2013, Arnaud Loonstra, All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import urwid
import json
import sys
import io
import zmq
import time
import zocp
import errno
# http://stackoverflow.com/questions/38987/how-can-i-merge-union-two-python-dictionaries-in-a-single-expression?rq=1
def mergedicts(a, b, path=None):
"""
merges b into a, overwites a with b if equal
"""
if path is None: path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
mergedicts(a[key], b[key], path + [str(key)])
else:
a[key] = b[key]
else:
a[key] = b[key]
return a
class ZmqEventLoop(urwid.SelectEventLoop):
def __init__(self, ctx=zmq.Context(), *args, **kwargs):
super(ZmqEventLoop, self).__init__(*args, **kwargs)
self._zctx = ctx
self._zpoller = zmq.Poller()
def watch_file(self, fd, callback):
"""
Call callback() when fd has some data to read. No parameters
are passed to callback.
Returns a handle that may be passed to remove_watch_file()
fd -- file descriptor to watch for input
callback -- function to call when input is available
"""
self._watch_files[fd] = callback
self._zpoller.register(fd, zmq.POLLIN)
return fd
def remove_watch_file(self, handle):
"""
Remove an input file.
Returns True if the input file exists, False otherwise
"""
if handle in self._watch_files:
self._zpoller.unregister(handle)
del self._watch_files[handle]
return True
return False
def run(self):
"""
Start the event loop. Exit the loop when any callback raises
an exception. If ExitMainLoop is raised, exit cleanly.
"""
self._did_something = True
while True:
try:
self._loop()
except urwid.ExitMainLoop:
break
def _loop(self):
"""
A single iteration of the event loop
"""
if self._alarms or self._did_something:
if self._alarms:
tm = self._alarms[0][0]
timeout = max(0, tm - time.time())
if self._did_something and (not self._alarms or
(self._alarms and timeout > 0)):
timeout = 0
tm = 'idle'
try:
items = dict(self._zpoller.poll(timeout))
except zmq.ZMQError as e:
if e.errno == errno.EINTR:
items = {}
else:
raise(e)
else:
tm = None
try:
items = dict(self._zpoller.poll())
except zmq.ZMQError as e:
if e.errno == errno.EINTR:
items = {}
pass
else:
raise(e)
if not items:
if tm == 'idle':
self._entering_idle()
self._did_something = False
elif tm is not None:
# must have been a timeout
tm, alarm_callback = self._alarms.pop(0)
alarm_callback()
self._did_something = True
for fd, ev in items.items():
if ev in (zmq.POLLIN, zmq.POLLOUT):
self._watch_files[fd]()
self._did_something = True
class PopUpDialog(urwid.WidgetWrap):
"""A dialog that appears with nothing but a close button """
signals = ['close']
def __init__(self):
close_button = urwid.Button("that's pretty cool")
urwid.connect_signal(close_button, 'click',
lambda button:self._emit("close"))
pile = urwid.Pile([urwid.Text(
"^^ I'm attached to the widget that opened me. "
"Try resizing the window!\n"), close_button])
fill = urwid.Filler(pile)
self.__super.__init__(urwid.AttrWrap(fill, 'selected'))
class HeadingWithPopUp(urwid.PopUpLauncher):
def __init__(self, name):
super(HeadingWithPopUp, self).__init__(urwid.Button(name))
urwid.connect_signal(self.original_widget, 'click',
lambda button: self.open_pop_up())
def create_pop_up(self):
pop_up = PopUpDialog()
urwid.connect_signal(pop_up, 'close',
lambda button: self.close_pop_up())
return pop_up
def get_pop_up_parameters(self):
return {'left':10, 'top':1, 'overlay_width':32, 'overlay_height':7}
class UrwStdWriter(io.TextIOWrapper):
def __init__(self, *args, **kwargs):
self.wgt = urwid.Text("")
super(UrwStdWriter, self).__init__(*args, **kwargs)
def write(self, data):
self.wgt.set_text("%s%s" %(self.wgt.get_text()[0], data))
def get_widget(self):
return self.wgt
class ZOCPProgressBar(urwid.ProgressBar):
signals =["change"]
def selectable(selfs): return True
def __init__(self, sel_normal, sel_complete, *args, **kwargs):
self.sel_normal = sel_normal
self.sel_complete = sel_complete
super(ZOCPProgressBar, self).__init__(*args, **kwargs)
def set_label(self, label):
self._label, self._attrib = decompose_tagmarkup(caption)
self._invalidate()
def keypress(self, size, key):
(maxcol,) = size
cur = int(self.current)
if key == 'right':
if cur < self.done:
self.set_completion(cur + 1)
self._emit('change', cur + 1)
return
elif key == 'left':
if cur > 0:
self.set_completion(cur - 1)
self._emit('change', cur - 1)
return
else:
return key
class ZOCPKeyboardWidget(urwid.Edit):
"""Edit widget which emits its value when pressing enter or loosing focus"""
def __init__(self,caption="",val=None):
"""
caption -- caption markup
default -- default edit value
"""
self.set_val = val
self.focused = False
self.__super.__init__(caption,val)
def keypress(self, size, key):
"""
Handle editing keystrokes. Emit change message when
pressing enter or when leaving widget
"""
(maxcol,) = size
unhandled = super(ZOCPKeyboardWidget, self).keypress((maxcol,),key)
if key == 'enter':
self.set_val = self.value()
self._emit("change", self.value())
return unhandled
def render(self, size, focus):
"""
Render the widget.
`render` is overridden so we can generate `on_focus` events
when the focus argument changes
:param size: see `urwid.Widget.render(size, focus)`
:type size: widget size
:param focus: set to ``True`` if this widget or one of its children
is in focus
:type focus: bool
"""
if focus != self.focused:
self.focused = focus
self.on_focus(focus)
return self.__super.render(size, focus)
def on_focus(self, focus):
"""
Handle changes in focus.
:param focus: set to ``True`` if this widget or one of its children
is in focus
:type focus: bool
"""
if focus:
# Received focus
self.set_val = self.value()
else:
# Lost focus
val = self.value()
if val != self.set_val:
self.set_val = val
self._emit("change", val)
class ZOCPStringWidget(ZOCPKeyboardWidget):
"""Edit widget for string values"""
def valid_char(self, ch):
"""
Return true for all characters.
"""
return len(ch)==1
def __init__(self,caption="",default=None):
"""
caption -- caption markup
default -- default edit value
"""
if default is not None: val = str(default)
else: val = ""
self.__super.__init__(caption,val)
def value(self):
"""
Return the string value of self.edit_text.
"""
if self.edit_text:
return self.edit_text
else:
return ""
def set_edit_text(self, text):
"""
Set the text for this widget.
:param text: text for editing, type (bytes or unicode)
must match the text in the caption
"""
text = self._normalize_to_caption(text)
self.highlight = None
self._edit_text = text
if self.edit_pos > len(text):
self.edit_pos = len(text)
self._invalidate()
class ZOCPIntWidget(ZOCPKeyboardWidget):
"""Edit widget for integer values"""
def valid_char(self, ch):
"""
Return true for decimal digits.
"""
return len(ch)==1 and ch in "0123456789"
def __init__(self,caption="",default=None):
"""
caption -- caption markup
default -- default edit value
>>> IntEdit("", 42)
<IntEdit selectable flow widget '42' edit_pos=2>
"""
if default is not None: val = str(default)
else: val = ""
self.set_val = val
self.__super.__init__(caption,val)
def keypress(self, size, key):
"""
Handle editing keystrokes. Remove leading zeros.
>>> e, size = IntEdit("", 5002), (10,)
>>> e.keypress(size, 'home')
>>> e.keypress(size, 'delete')
>>> print(e.edit_text)
002
>>> e.keypress(size, 'end')
>>> print(e.edit_text)
2
"""
(maxcol,) = size
unhandled = super(ZOCPIntWidget, self).keypress((maxcol,),key)
if not unhandled:
# trim leading zeros
while self.edit_pos > 0 and self.edit_text[:1] == "0":
self.set_edit_pos( self.edit_pos - 1)
self.set_edit_text(self.edit_text[1:])
return unhandled
def value(self):
"""
Return the numeric value of self.edit_text.
>>> e, size = IntEdit(), (10,)
>>> e.keypress(size, '5')
>>> e.keypress(size, '1')
>>> e.value() == 51
True
"""
if self.edit_text and self.edit_text != "":
return int(self.edit_text)
else:
return 0
def set_edit_text(self, text):
"""
Set the text for this widget.
:param text: text for editing, type (bytes or unicode)
must match the text in the caption
:type text: bytes or unicode
>>> e = FloatEdit()
>>> e.set_edit_text("0.12")
>>> print(e.edit_text)
'0.12'
"""
text = self._normalize_to_caption(text)
self.highlight = None
self._edit_text = text
if self.edit_pos > len(text):
self.edit_pos = len(text)
self._invalidate()
class ZOCPFloatWidget(ZOCPKeyboardWidget):
def valid_char(self, ch):
"""
Return true for decimal digits.
"""
return len(ch)==1 and ch in "0123456789."
def __init__(self, caption="", value=0.0, min=None, max=None, step=0.1):
"""
caption -- caption markup
default -- default edit value
>>> IntEdit("", 42)
<IntEdit selectable flow widget '42' edit_pos=2>
"""
self.min = min
self.max = max
self.step = step
super(ZOCPFloatWidget).__init__(caption, "%f" %value)
#rwid.connect_signal(self, 'change', self.on_change)
def keypress(self, size, key):
"""
Handle editing keystrokes. Remove leading zeros.
>>> e, size = IntEdit("", 5002), (10,)
>>> e.keypress(size, 'home')
>>> e.keypress(size, 'delete')
>>> print(e.edit_text)
002
>>> e.keypress(size, 'end')
>>> print(e.edit_text)
2
"""
(maxcol,) = size
if key == 'right':
val = float(self.get_edit_text()) if self.get_edit_text() != "" else 0.0
val += self.step
self.set_edit_text("%f" %val)
self.set_val = val
self._emit("change", val)
elif key == 'left':
val = float(self.get_edit_text()) if self.get_edit_text() != "" else 0.0
val -= self.step
self.set_edit_text("%f" %val)
self.set_val = val
self._emit("change", val)
else:
unhandled = super(ZOCPFloatWidget, self).keypress((maxcol,),key)
if not unhandled:
# trim leading zeros
while self.edit_pos > 0 and self.edit_text[:1] == "0":
self.set_edit_pos( self.edit_pos - 1)
self.set_edit_text(self.edit_text[1:])
return unhandled
def value(self):
"""
Return the numeric value of self.edit_text.
>>> e, size = IntEdit(), (10,)
>>> e.keypress(size, '5')
>>> e.keypress(size, '1')
>>> e.value() == 51
True
"""
if self.edit_text and self.edit_text != "":
return float(self.edit_text)
else:
return 0.0
def set_value(self, val):
self.set_edit_text("%f" %val)
def set_edit_text(self, text):
"""
Set the text for this widget.
:param text: text for editing, type (bytes or unicode)
must match the text in the caption
:type text: bytes or unicode
>>> e = FloatEdit()
>>> e.set_edit_text("0.12")
>>> print(e.edit_text)
'0.12'
"""
text = self._normalize_to_caption(text)
self.highlight = None
self._edit_text = text
if self.edit_pos > len(text):
self.edit_pos = len(text)
self._invalidate()
class ZOCPVec2fWidget(ZOCPKeyboardWidget):
def valid_char(self, ch):
"""
Return true for decimal digits.
"""
return len(ch)==1 and ch in "0123456789."
def __init__(self, caption="", value=[0.0,0.0], min=None, max=None, step=[0.1,0.1]):
"""
caption -- caption markup
default -- default edit value
"""
self.min = min
self.max = max
self.step = step
super(ZOCPVec2fWidget, self).__init__(caption, "[%.3f, %.3f]" %(value[0], value[1]))
def keypress(self, size, key):
"""
Handle editing keystrokes. Remove leading zeros.
"""
(maxcol,) = size
if key == 'd':
val = self.value()
val[0] = val[0] + self.step[0]
self.set_value(val)
self._emit("change", val)
elif key == 'a':
val = self.value()
val[0] = val[0] - self.step[0]
self.set_value(val)
self._emit("change", val)
elif key == 'w':
val = self.value()
val[1] = val[1] + self.step[1]
self.set_value(val)
self._emit("change", val)
elif key == 's':
val = self.value()
val[1] = val[1] - self.step[1]
self.set_value(val)
self._emit("change", val)
else:
unhandled = super(ZOCPVec2fWidget, self).keypress((maxcol,),key)
if not unhandled:
# trim leading zeros
while self.edit_pos > 0 and self.edit_text[:1] == "0":
self.set_edit_pos( self.edit_pos - 1)
self.set_edit_text(self.edit_text[1:])
return unhandled
def value(self):
"""
Return the numeric value of self.edit_text.
"""
if self.edit_text and self.edit_text != "":
return eval(self.edit_text)
else:
return [0.0, 0,0]
def set_value(self, val):
self.set_edit_text("[%.3f, %.3f]" %(val[0], val[1]))
def set_edit_text(self, text):
"""
Set the text for this widget.
:param text: text for editing, type (bytes or unicode)
must match the text in the caption
:type text: bytes or unicode
"""
text = self._normalize_to_caption(text)
self.highlight = None
self._edit_text = text
if self.edit_pos > len(text):
self.edit_pos = len(text)
self._invalidate()
class ZOCPNodeWidget(urwid.WidgetWrap):
#control_ex = '{"control": {"myInt": {"control": "rw", "value": 1, "typeHint": "int"}, "myFloat": {"control": "rw", "value": 1.0, "typeHint": "float"}}}'
def __init__(self, parent=None, data={}, node_id=None, node_name="NoName", *args, **kwargs):
#if not isinstance(node, ZOCP)
# raise Exception("Node %s is not of ZOCP type" %node)
self.parent = parent
self.node_id = node_id
self.node_name = node_name
self.node_data = data
self.focused = False
self._update_widgets()
display_widget = urwid.Pile(urwid.SimpleFocusListWalker(self._widgets))
super(ZOCPNodeWidget, self).__init__(display_widget)
def update(self, data=None):
if isinstance(data, dict):
self.node_data = mergedicts(self.node_data, data)
self._update_widgets()
focus = self._w.focus_position
self._w = urwid.Pile(urwid.SimpleFocusListWalker(self._widgets))
self._w.set_focus(self._w.contents[focus][0])
def _update_widgets(self):
#self.node_data.update(data)
name = self.node_name
#self._widgets = [urwid.AttrMap(urwid.Text([name]), 'heading') ]
self._widgets = [urwid.AttrMap(HeadingWithPopUp(name), 'heading') ]
for name, val in self.node_data.items():
# we only parse keys not starting with '_'
if not name[0] == '_':
wgt = None
# generate widgets based on the typeHint
#print("%s, %s" %(val, type(val)))
dtype = val.get('typeHint')
# get access property, readonly if none
access = val.get('access', 'r')
if dtype == 'string' and access.count('w'):
wgt = urwid.AttrMap(ZOCPStringWidget(name + " : ", val.get('value')), 'options', 'selected')
urwid.connect_signal(wgt.original_widget, 'change', self.on_changed, (name))
elif dtype == 'int' and access.count('w'):
wgt = urwid.AttrMap(ZOCPIntWidget(name + " : ", val.get('value')), 'options', 'selected')
urwid.connect_signal(wgt.original_widget, 'change', self.on_changed, (name))
elif dtype == 'float' and access.count('w'):
wgt = urwid.AttrMap(ZOCPFloatWidget(caption=name + " :", value=val.get('value')), 'options', 'selected')
urwid.connect_signal(wgt.original_widget, 'change', self.on_changed, (name))
elif dtype == 'vec2f' and access.count('w'):
wgt = urwid.AttrMap(ZOCPVec2fWidget(caption=name + " :", value=val.get('value')), 'options', 'selected')
urwid.connect_signal(wgt.original_widget, 'change', self.on_changed, (name))
elif dtype == 'bool' and access.count('w'):
wgt = urwid.AttrMap(urwid.CheckBox(name, state=val.get('value')), 'options', 'selected')
urwid.connect_signal(wgt.original_widget, 'change', self.on_changed, (name))
elif dtype == 'percent':
wgt = urwid.AttrMap(ZOCPProgressBar(sel_normal='focus pg normal',
sel_complete='focus pg complete',
normal='pg normal',
complete='pg complete',
current=val.get('value'),
done=100,
satt='pg smooth'),
'options', focus_map_pg)
#'options', {'pg normal': 'focus pg normal', 'pg complete': 'focus pg complete'})
urwid.connect_signal(wgt.original_widget, 'change', self.on_changed, (name))
else:
wgt = wgt = urwid.AttrMap(urwid.Text(name + " : %s" %val.get('value')), 'options', 'selected')
self._widgets.append(wgt)
self._widgets.append(urwid.AttrMap(urwid.Divider('\N{LOWER ONE QUARTER BLOCK}'), 'line'))
#self._widgets.append(urwid.Divider())
def on_changed(self, wgt, value, name):
self.node_data[name].update({'value': value})
dat = {name: {'value': value}}
z.whisper(self.node_id, json.dumps({'SET': dat}).encode('utf-8'))
def on_focus(self, focus):
if focus:
self.parent.foot.original_widget.set_text("%s" %self.node_id)
def render(self, size, focus):
"""
Render the widget.
`render` is overridden so we can generate `on_focus` events
when the focus argument changes
:param size: see `urwid.Widget.render(size, focus)`
:type size: widget size
:param focus: set to ``True`` if this widget or one of its children
is in focus
:type focus: bool
"""
if focus != self.focused:
self.focused = focus
self.on_focus(focus)
return self.__super.render(size, focus)
palette = [
(None, 'light gray', 'black'),
('selected', 'white', 'dark blue'),
('header', 'black', 'dark gray', 'bold'),
('heading', 'black', 'light gray', 'bold'),
('focus heading', 'white', 'dark red' ),
('line', 'black', 'light gray'),
('focus line', 'black', 'dark red'),
('options', 'white', 'dark gray'),
('focus options', 'white', 'dark gray'),
('pg normal', 'white', 'dark gray'),
('focus pg normal', 'white', 'dark blue'),
('pg complete', 'white', 'dark magenta'),
('focus pg complete', 'white', 'dark green'),
('pg smooth', 'dark magenta','black'),
('focus pg smooth', 'dark magenta','dark blue')]
focus_map = {
'heading': 'focus heading',
'options': 'focus options',
'line': 'focus line'
}
focus_map_pg = {
'pg normal': 'focus pg normal',
'pg complete': 'focus pg complete',
'pg smooth': 'focus pg smooth',
'options': 'focus options'
}
class urwZOCP(zocp.ZOCP):
def __init__(self, *args, **kwargs):
super(urwZOCP, self).__init__(*args, **kwargs)
self.oldout = sys.stdout
self.out = UrwStdWriter(io.BytesIO(), sys.stdout.encoding)
self.znodes = {}
# Urwid containers
self.cells = urwid.GridFlow(self.znodes.values(), 20, 1, 0, 'left')
self.foot = urwid.AttrMap(urwid.Text("[no node selected]", align='center'), 'header')
self.frame = urwid.Pile((self.foot, self.cells, self.out.get_widget()), 1)
self.fill = urwid.Filler(self.frame, 'top')
self.loop = urwid.MainLoop(self.fill, palette, unhandled_input=self.handle_input, event_loop=ZmqEventLoop(), pop_ups=True)
# capture std output
sys.stdout = self.out
def handle_input(self, key):
if key == 'esc':
raise urwid.ExitMainLoop()
if key in ('q', 'Q'):
self.out.get_widget().set_text("")
#########################################
# ZOCP Event methods.
#########################################
def on_peer_enter(self, peer, name, *args, **kwargs):
print("ZOCP ENTER : %s" %(name))
nd = self.znodes.get(peer)
if not nd:
#raise Exception("bla", self.peers_capabilities)
nd = ZOCPNodeWidget(self, self.peers_capabilities[peer], peer, name)
self.znodes[peer] = (urwid.AttrMap(nd, 'options', focus_map), self.cells.options('given', 20))
#index = len(cells.contents)
#cells.contents.insert(index, (nd, ('given', 20)))
self.cells.contents.append(self.znodes[peer])
self.signal_subscribe(self.uuid(), None, peer, None)
def on_peer_exit(self, peer, name, *args, **kwargs):
print("ZOCP EXIT : %s" %(name))
nd = self.znodes.pop(peer)
if nd:
# clear self.contents
del self.cells.contents[:]
# repopulate self.contents with remaining self.znodes
for val in self.znodes.values():
self.cells.contents.append(val)
if len(self.znodes)==0:
self.cells._invalidate()
self.foot.original_widget.set_text("[no node selected]")
def on_peer_modified(self, peer, name, data, *args, **kwargs):
print("ZOCP MODIFIED: %s modified %s" %(name, data))
nd = self.znodes.get(peer)
if nd:
nd[0].original_widget.update(self.peers_capabilities.get(peer, {}))
def on_peer_signaled(self, peer, name, data, *args, **kwargs):
print("ZOCP SIGNALED: %s modified %s" %(peer.hex, data))
nd = self.znodes.get(peer)
if nd:
nd[0].original_widget.update()
def run(self):
self.start()
handle = self.loop.watch_file(self.inbox, self.get_message)
self._running = True
self.loop.run()
self.stop()
sys.stdout = self.oldout
if __name__ == "__main__":
ctx = zmq.Context()
z = urwZOCP("urwMonitor", ctx=ctx)
z.run()