-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarpTab.py
383 lines (264 loc) · 11.6 KB
/
yarpTab.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
####################################################################################################
# Copyright (C) 2016 by Ingo Keller #
# <[email protected]> #
# #
# This file is part of yarpTab (OS X Menu Tab for YARP). #
# #
# yarpTab is free software: you can redistribute it and/or modify it under the terms of the #
# GNU Affero General Public License as published by the Free Software Foundation, either #
# version 3 of the License, or (at your option) any later version. #
# #
# yarpTab 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 General Public License for more details. #
# #
# You should have received a copy of the GNU Affero General Public License #
# along with yarpTab. If not, see <http://www.gnu.org/licenses/>. #
####################################################################################################
from subprocess import Popen, call, PIPE
from urllib2 import urlopen
import time
import rumps
@rumps.timer(1)
def timerCallback_checkAlive(_):
YarpTab.getInstance().updateUi()
def getBasePath():
return '/usr/local/bin/'
class YarpController(object):
Y_BASE = getBasePath()
Y_YARP = Y_BASE + 'yarp'
Y_YARPSRV = Y_BASE + 'yarpserver3'
Y_YARPVIEW = Y_BASE + 'yarpview'
URL_BASE = 'http://{0}:{1}/'
URL_DATALIST = URL_BASE + 'data=list'
CMD_YARP_START = [Y_YARPSRV, '--write']
CMD_YARP_SHOW = '/usr/bin/open ' + URL_BASE
def __init__(self, ip = '127.0.0.1', port = 10000):
self.ip = ip
self.port = port
self.allPorts = {}
self.nestedNames = {}
self.prev_html = ''
self.updatePortList()
def clearPortList(self):
self.allPorts = []
self.nestedNames = {}
self.prev_html = ''
self.updatePortList()
def updatePortList(self):
try:
html = urlopen(YarpController.URL_DATALIST.format(self.ip, self.port)).read()
except:
return
# no changes - no need to update
if self.prev_html == html:
return False
self.prev_html = html
ports = self.parsePortList(html)
self.allPorts = dict([(port[1], port[0]) for port in ports])
self.nestedNames = {}
# create nested names
for port in self.allPorts.keys():
namespace = self.nestedNames
for name in port.split('/')[1:]:
if not name in namespace:
namespace[name] = {}
namespace = namespace[name]
return True
def parsePortList(self, html):
""" This method returns a list of port descriptions [<PORT_DESCRIPTION>]"""
# remove line breaks and other non-visual characters
html = html.replace('\n', '').replace('\r', '')
lines = html.split('<pre>')[1].split('</pre>')[0].split('<a href="')
return [ self.parseLink(link) for link in lines if len(link.strip()) > 0 ]
def parseLink(self, link_line):
""" This method returns a list for a port link [<URL>, <NAME>, <DESCRIPTION>]"""
return [ link_line.split('"')[0],
link_line.split('>')[1].split('<')[0],
link_line.split('(')[1].split(')')[0] ]
def openImageOutput(self, port):
""" This method opens a yarp view and connects it to the given port."""
Popen([YarpController.Y_YARPVIEW, "--name", "/yarpTab%s" % port])
time.sleep(0.5)
call([YarpController.Y_YARP, "connect", port, "/yarpTab%s" % port])
def openTextOutput(self, port):
""" This method opens a Terminal window and runs yarp read which is connected to the given
port.
"""
yarp_cmd = 'yarp read /yarpTab{0} {0}'.format(port)
osa_script = 'tell application "Terminal" to do script "{0}"'.format(yarp_cmd)
Popen(["/usr/bin/osascript", '-e', osa_script])
def openTextInput(self, port):
""" This method opens a Terminal window and runs yarp write which is connected to the given
port.
"""
yarp_cmd = 'yarp write /yarpTab{0} {0}'.format(port)
osa_script = 'tell application "Terminal" to do script "{0}"'.format(yarp_cmd)
Popen(["/usr/bin/osascript", '-e', osa_script])
def openYarpInBrowser(self):
""" This methods opens a browser with the yarp web interface URL."""
cmd = YarpController.CMD_YARP_SHOW.format(self.ip, self.port)
print cmd
call(cmd.split())
def cleanYarp(self):
""" This method runs the yarp clean command."""
call([YarpController.Y_YARP, 'clean', '--timeout', '1'])
time.sleep(1)
self.clearPortList()
def killYarp(self):
""" This method kills all yarpservers. """
call(['killall', 'yarpserver'])
self.clearPortList()
class YarpTab(rumps.App):
MI_YS_START = ' Run'
MI_YS_STOP = ' Stop'
MI_YS_SHOW = ' Show'
MI_YS_PORTS = ' Ports'
MI_YS_CLEAN = ' Clean'
MI_YS_KILL = ' Kill'
MI_QUIT = ' Quit'
_instance = None
@staticmethod
def getInstance():
if not YarpTab._instance:
YarpTab._instance = YarpTab()
return YarpTab._instance
def __init__(self):
super(YarpTab, self).__init__('yarpTab', 'Yarp')
self.yarpserver = None
self.quit_button = None
self.prev_state = None
self.yarpController = YarpController()
self._delayedSetup = True
global_timer.start()
def delayedSetup(self):
self._delayedSetup = False
@rumps.clicked(MI_YS_START)
def start(self, sender):
if not self.isAlive():
self.yarpserver = Popen(YarpController.CMD_YARP_START)
ip = self.yarpController.ip
port = self.yarpController.port
msg = 'Yarp Server started'
msg += '\nIP: {0}\nPort: {1}'.format(ip, port)
rumps.notification('YarpTab', '', msg)
else:
self.yarpserver.kill()
self.yarpserver = None
rumps.notification('YarpTab', '', 'Yarp Server stopped')
self.updateUi()
@rumps.clicked(MI_YS_SHOW)
def show(self, _):
self.yarpController.openYarpInBrowser()
@rumps.clicked(MI_YS_PORTS)
def nothing(self, _):
pass
@rumps.clicked(MI_YS_CLEAN)
def clean(self, _):
self.yarpController.cleanYarp()
time.sleep(1)
self.updateUi()
@rumps.clicked(MI_YS_KILL)
def killYarpServer(self, _):
self.yarpController.killYarp()
rumps.notification('YarpServer', '', 'Servers killed')
self.updateUi()
@rumps.clicked(MI_QUIT)
def clean_up_before_quit(self, _):
if self.isAlive():
self.yarpserver.kill()
rumps.quit_application()
def isAlive(self):
return (self.yarpserver is not None) and (self.yarpserver.poll() == None)
def updateUi(self):
if self._delayedSetup:
self.delayedSetup()
isAlive = self.isAlive()
# only update UI if there is a reason
if (self.prev_state != isAlive) or self.prev_state is None :
self.prev_state = isAlive
startStopMenuItem = self.menu.get(YarpTab.MI_YS_START)
if isAlive:
startStopMenuItem.title = YarpTab.MI_YS_STOP
else:
startStopMenuItem.title = YarpTab.MI_YS_START
if self.yarpController.updatePortList():
# clear menu items
rootItem = self.menu.get(YarpTab.MI_YS_PORTS)
if rootItem:
rootItem.clear()
# add menu items
self.addPortItem(rootItem, self.yarpController.nestedNames, '/')
def addPortItem(self, parent, item_dict, namespace):
names = item_dict.keys()
names.sort()
for name in names:
rmi = rumps.MenuItem('/%s' % name)
rmi.port = namespace + name
parent.add(rmi)
# add children
if item_dict[name]:
self.addPortItem(rmi, item_dict[name], namespace + name + '/')
# or add a callback function
else:
if 'yarpTab' not in namespace:
rmi.set_callback(self.callPort)
# create sub menus
rmiIO = rumps.MenuItem('Image Output', self.callIO)
rmiIO.port = namespace + name
rmi.add(rmiIO)
rmiTO = rumps.MenuItem('Text Output', self.callTO)
rmiTO.port = namespace + name
rmi.add(rmiTO)
rmiTI = rumps.MenuItem('Text Input', self.callTI)
rmiTI.port = namespace + name
rmi.add(rmiTI)
def callIO(self, sender):
self.yarpController.openImageOutput(sender.port)
def callTO(self, sender):
self.yarpController.openTextOutput(sender.port)
def callTI(self, sender):
self.yarpController.openTextInput(sender.port)
def callPort(self, sender):
portType = self.guessPortType(sender.port)
msgType = self.guessMessageType(sender.port)
port = sender.port
# output ports
if portType == 'o':
# images
if msgType == 'image':
self.yarpController.openImageOutput(port)
# text
else:
self.yarpController.openTextOutput(port)
# input port
elif portType == 'i':
# text
if msgType == 'text':
self.yarpController.openTextInput(port)
# images
else:
pass
# unknown - assume text output
else:
self.yarpController.openTextOutput(port)
def guessPortType(self, portname):
# see if we got a input/output prefix
suffix = portname.split(':')[1] if ':' in portname else portname.split('/')[-1]
# check input suffixes
if suffix in ['i', 'rpc']:
return 'i'
# check output suffixes
elif suffix in ['o', 'grabber']:
return 'o'
# unkown
return 'u'
def guessMessageType(self, portname):
name = portname.split('/')[-1]
for t in ['image', 'img', 'grabber']:
if t in name.lower():
return 'image'
return 'text'
if __name__ == '__main__':
global_timer = rumps.Timer(timerCallback_checkAlive, 1)
YarpTab.getInstance().run()