/
cli.py
102 lines (78 loc) · 1.78 KB
/
cli.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
# Copyright 2011 Klas Lindberg <klas.lindberg@gmail.com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
import sys
import getopt
import time
from Queue import Queue, Empty
from protocol import Play, JsonResult, Add
from wire import JsonWire, Connected
def txrx(cmd):
queue = Queue(10)
wire = JsonWire('', 3482, queue, accept=False)
try:
wire.start()
while True:
try:
msg = queue.get(block=False)
except Empty:
time.sleep(0.1)
continue
if isinstance(msg, Connected):
break
print('Garbage messsage: %s' % unicode(msg))
wire.send(cmd.serialize())
while True:
try:
msg = queue.get(block=False)
except Empty:
time.sleep(0.1)
continue
if isinstance(msg, JsonResult):
print msg
break
print('Garbage messsage: %s' % unicode(msg))
except KeyboardInterrupt:
pass
wire.stop()
def play(argv):
def syntax():
print('Syntax: cli play --url cm://<cm label>/<guid> [--seek <msec>]')
sys.exit(1)
try:
(opts, args) = getopt.gnu_getopt(argv, '', ['url=', 'seek='])
except:
syntax()
url = None
seek = None
for (opt, arg) in opts:
if opt == '--url':
url = arg.decode('utf-8')
if opt == '--seek':
seek = arg
if not url:
syntax()
if seek:
try: seek = int(seek)
except: syntax()
else:
seek = 0
cmd = Play(0, url, seek=seek)
txrx(cmd)
def add(argv):
def syntax():
print('Syntax: cli add --url cm://<cm label>/<guid>')
sys.exit(1)
try:
(opts, args) = getopt.gnu_getopt(argv, '', ['url='])
except:
syntax()
url = None
for (opt, arg) in opts:
if opt == '--url':
url = arg.decode('utf-8')
if not url:
syntax()
cmd = Add(0, url)
txrx(cmd)