/
DDPCommandLineTool.py
203 lines (166 loc) · 6.29 KB
/
DDPCommandLineTool.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
import sys
import json
import socket
from cmd import Cmd
from DDPClient import DDPClient
class App(Cmd):
"""Main input loop"""
def __init__(self, ddp_endpoint):
Cmd.__init__(self)
self.ddp_client = DDPClient(
'ws://' + ddp_endpoint + '/websocket'
)
self.subscriptions = {}
self.pending_results = {}
# Showing a fancy prompt string if we're interactive
if sys.stdin.isatty():
self.prompt = ddp_endpoint + '> '
else:
self.prompt = ''
self.ddp_client.on('connected', self.connected)
self.ddp_client.on('socket_closed', self.closed)
self.ddp_client.on('failed', self.failed)
self.ddp_client.on('added', self.added)
self.ddp_client.on('changed', self.changed)
self.ddp_client.on('removed', self.removed)
self.ddp_client.connect()
def subscribe(self, name, params):
if name in self.subscriptions:
self._log('* ALREADY SUBSCRIBED - {}'.format(name))
return
sub_id = self.ddp_client.subscribe(name, params, self.subscribed)
self.subscriptions[name] = sub_id
def unsubscribe(self, name):
if name not in self.subscriptions:
self._log('* NOT SUBSCRIBED - {}'.format(name))
return
self.ddp_client.unsubscribe(self.subscriptions[name])
del self.subscriptions[name]
self._log('* UNSUBSCRIBED - {}'.format(name))
def call(self, method_name, params):
self.ddp_client.call(method_name, params, self.result)
#
# Callbacks
#
def added(self, collection, id, fields):
self._log('* ADDED {} {}'.format(collection, id), True)
for key, value in fields.items():
self._log(' - FIELD {} {}'.format(key, value))
def changed(self, collection, id, fields, cleared):
self._log('* CHANGED {} {}'.format(collection, id), True)
for key, value in fields.items():
self._log(' - FIELD {} {}'.format(key, value))
for key, value in cleared.items():
self._log(' - CLEARED {} {}'.format(key, value))
def removed(self, collection, id):
self._log('* REMOVED {} {}'.format(collection, id), True)
def subscribed(self, error, sub_id):
if error:
self._remove_sub_by_id(sub_id)
self._log('* SUBSCRIPTION FAILED - {}'.format(error.get('reason')), True)
return
self._log('* READY', True)
def result(self, error, result):
if error:
self._log('* METHOD FAILED - {}'.format(error.get('reason')), True)
return
self._log('* METHOD RESULT - {}'.format(str(result)), True)
def connected(self):
self._log('* CONNECTED', True)
def closed(self, code, reason):
"""Called when the connection is closed"""
self._log('* CONNECTION CLOSED {} {}'.format(code, reason))
def failed(self, data):
self._log('* FAILED - data: {}'.format(str(data)))
#
# Commands
#
def do_sub(self, params):
"""The `sub` command"""
try:
sub_name, sub_params = self._parse_command(params)
except ValueError:
self._log('Error parsing parameter list - try `help sub`')
return
self.subscribe(sub_name, sub_params)
def do_unsub(self, params):
"""The `unsub` command"""
try:
sub_name, sub_params = self._parse_command(params)
except ValueError:
self._log('Error parsing parameter list - try `help unsub`')
return
self.unsubscribe(sub_name)
def do_call(self, params):
"""The `call` command"""
try:
method, params = self._parse_command(params)
except ValueError:
self._log('Error parsing parameter list - try `help call`')
return
self.call(method, params)
def do_login(self, params):
"""The `login` command"""
self._log('Not Implemented Yet -- SRP is in progress')
self._log('However you can still calling the method `login` (WARNING: password sent in plaintext)')
#self.ddp_client._login({'username': 'alice'}, 'password123')
def do_EOF(self, line):
"""The `EOF` "command"
It's here to support `cat file | python ddpclient.py`
"""
return True
def do_help(self, line):
"""The `help` command"""
msgs = {
'call': (
'call <method name> <json array of parameters>\n'
' Calls a remote method\n'
' Example: call vote ["foo.meteor.com"]'),
'sub': (
'sub <subscription name> [<json array of parameters>]\n'
' Subscribes to a remote dataset\n'
' Examples: `sub allApps` or `sub myApp '
'["foo.meteor.com"]`'),
'unsub': (
'unsub <subscription name>\n'
' Unsubscribes from a remote dataset\n'
' Examples: `unsub allApps` or `unsub myApp '
'["foo.meteor.com"]`'),
}
line = line.strip()
if line and line in msgs:
return self._log('\n' + msgs[line])
for msg in msgs.values():
self._log('\n' + msg)
#
# Utils
#
def _log(self, msg, server_msg=False):
if server_msg:
sys.stderr.write('\n')
sys.stderr.write('{}\n'.format(msg))
if server_msg:
sys.stderr.write('{}'.format(self.prompt))
def _parse_command(self, params):
"""Parses a command with a first string param and a second
json-encoded param"""
name, args = (params + ' ').split(' ', 1)
return name, args and json.loads(args) or []
def _remove_sub_by_id(self, sub_id):
for name, cur_sub_id in self.subscriptions.items():
if cur_sub_id == sub_id:
del self.subscriptions[name]
if __name__ == '__main__':
ws_conn = '127.0.0.1:3000'
if len(sys.argv) > 1:
ws_conn = sys.argv[1]
sys.stderr.write('Connecting to {}\n'.format(ws_conn))
try:
app = App(ws_conn)
except socket.error:
sys.stderr.write('###Error connecting to {}###\n'.format(ws_conn))
sys.exit(-1)
try:
app.cmdloop()
except KeyboardInterrupt:
pass