diff options
author | James E. Blair <corvus@gnu.org> | 2014-12-20 07:35:13 -0800 |
---|---|---|
committer | James E. Blair <corvus@gnu.org> | 2015-01-09 10:39:14 -0800 |
commit | ef9fb76de9ef299fbdc8f87f1dd05bdd1eda649e (patch) | |
tree | dc85b1dfb180f427b3686e59d6b7a8254397c1e2 /presentty/client.py |
Initial commit
Diffstat (limited to 'presentty/client.py')
-rw-r--r-- | presentty/client.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/presentty/client.py b/presentty/client.py new file mode 100644 index 0000000..a819dfd --- /dev/null +++ b/presentty/client.py | |||
@@ -0,0 +1,66 @@ | |||
1 | # Copyright (C) 2015 James E. Blair <corvus@gnu.org> | ||
2 | # | ||
3 | # This program is free software: you can redistribute it and/or modify | ||
4 | # it under the terms of the GNU General Public License as published by | ||
5 | # the Free Software Foundation, either version 3 of the License, or | ||
6 | # (at your option) any later version. | ||
7 | # | ||
8 | # This program is distributed in the hope that it will be useful, | ||
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | # GNU General Public License for more details. | ||
12 | # | ||
13 | # You should have received a copy of the GNU General Public License | ||
14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | |||
16 | import socket | ||
17 | |||
18 | class Client(object): | ||
19 | def __init__(self, host='127.0.0.1', port=1292): | ||
20 | self.host = host | ||
21 | self.port = port | ||
22 | self.sock = None | ||
23 | self.connect() | ||
24 | |||
25 | def connect(self): | ||
26 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
27 | self.sock.connect((self.host, self.port)) | ||
28 | self.file = self.sock.makefile('rw', 0) | ||
29 | |||
30 | def list(self): | ||
31 | self.file.write('list\n') | ||
32 | program = [] | ||
33 | while True: | ||
34 | ln = self.file.readline().strip() | ||
35 | if ln == 'end': | ||
36 | break | ||
37 | x, index, title = ln.split(' ', 2) | ||
38 | program.append(title) | ||
39 | return program | ||
40 | |||
41 | def size(self): | ||
42 | self.file.write('size\n') | ||
43 | ln = self.file.readline().strip() | ||
44 | x, cols, rows = ln.split(' ', 2) | ||
45 | return (int(cols), int(rows)) | ||
46 | |||
47 | def parseCurrent(self): | ||
48 | ln = self.file.readline().strip() | ||
49 | x, index, progressive_state, title = ln.split(' ', 3) | ||
50 | return (int(index), int(progressive_state)) | ||
51 | |||
52 | def current(self): | ||
53 | self.file.write('current\n') | ||
54 | return self.parseCurrent() | ||
55 | |||
56 | def jump(self, index): | ||
57 | self.file.write('jump %i\n' % index) | ||
58 | return self.parseCurrent() | ||
59 | |||
60 | def next(self): | ||
61 | self.file.write('next\n') | ||
62 | return self.parseCurrent() | ||
63 | |||
64 | def prev(self): | ||
65 | self.file.write('prev\n') | ||
66 | return self.parseCurrent() | ||