-
Notifications
You must be signed in to change notification settings - Fork 0
/
topcat.py
84 lines (59 loc) · 2.28 KB
/
topcat.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
import os
import urllib
import atpy
from sampy import SAMPHubProxy, SAMPClient
import numpy as np
class SAMPHelper(object):
def __init__(self):
self.myhub = SAMPHubProxy()
self.myhub.connect()
self.client = SAMPClient(self.myhub)
metadata1 = {"samp.name": "Client",
"samp.description.text": "Client",
"client.version": "0.01"}
self.client.start()
self.client.register()
self.client.declareMetadata(metadata1)
self.client.bindReceiveCall("table.load.*", self._load_table)
self.client.bindReceiveCall("table.select.*", self._select_rows)
def _load_table(self, private_key, sender_id, msg_id, mtype, params, extra):
print "Received table:", params['url']
self.client.ereply(msg_id, SAMP_STATUS_OK, result = {"txt": "printed"})
def _select_rows(self, private_key, sender_id, msg_id, mtype, params, extra):
print "Selected rows:", params['row-list']
self.client.ereply(msg_id, SAMP_STATUS_OK, result = {"txt": "printed"})
def send_table(self, filename):
'''
Send a table via SAMP
Parameters
----------
filename : str
The table to send
'''
result = self.myhub.notifyAll(self.client.getPrivateKey(),
{"samp.mtype": "table.load.votable",
"samp.params": {"url": 'file://' + os.path.abspath(filename),
"name": filename}
})
return result[0]
def select_rows(self, table_id, rows):
result = self.myhub.notifyAll(self.client.getPrivateKey(),
{"samp.mtype": "table.select.rowList",
"samp.params": {"table-id": table_id,
"row-list": rows}
})
def finalize(self):
self.client.unregister()
self.client.stop()
self.myhub.disconnect()
if __name__ == "__main__":
t = atpy.Table()
t.add_column('id', range(10))
t.add_column('flux', np.random.random(10))
t.write('test.xml', overwrite=True)
try:
h = SAMPHelper()
tid = h.send_table('test.xml')
h.select_rows(tid, ["1", "3", "4"])
finally:
h.finalize()