summaryrefslogtreecommitdiffstats
path: root/presentty/client.py
blob: a819dfd48f582859e8e4e1980291988872b1f097 (plain)
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
# Copyright (C) 2015 James E. Blair <corvus@gnu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import socket

class Client(object):
    def __init__(self, host='127.0.0.1', port=1292):
        self.host = host
        self.port = port
        self.sock = None
        self.connect()

    def connect(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.host, self.port))
        self.file = self.sock.makefile('rw', 0)

    def list(self):
        self.file.write('list\n')
        program = []
        while True:
            ln = self.file.readline().strip()
            if ln == 'end':
                break
            x, index, title = ln.split(' ', 2)
            program.append(title)
        return program

    def size(self):
        self.file.write('size\n')
        ln = self.file.readline().strip()
        x, cols, rows = ln.split(' ', 2)
        return (int(cols), int(rows))

    def parseCurrent(self):
        ln = self.file.readline().strip()
        x, index, progressive_state, title = ln.split(' ', 3)
        return (int(index), int(progressive_state))

    def current(self):
        self.file.write('current\n')
        return self.parseCurrent()

    def jump(self, index):
        self.file.write('jump %i\n' % index)
        return self.parseCurrent()

    def next(self):
        self.file.write('next\n')
        return self.parseCurrent()

    def prev(self):
        self.file.write('prev\n')
        return self.parseCurrent()