forked from Answeror/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
118 lines (93 loc) · 2.56 KB
/
server.py
File metadata and controls
118 lines (93 loc) · 2.56 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import windows
from qt.QtCore import (
QCoreApplication,
QObject,
QDataStream,
QMetaObject,
Qt,
QThread,
Signal,
Slot
)
from qt.QtNetwork import (
QTcpServer,
QHostAddress
)
import logging
from service import (
set_version,
write,
PORT
)
from common import set_app_info
class Server(QObject):
def __init__(self, parent=None):
super(Server, self).__init__(parent)
self._make_tcp_server()
self.hotkey_thread = HotkeyThread()
self.hotkey_thread.fire.connect(self._handle_hotkey)
def _make_tcp_server(self):
self.s = QTcpServer(self)
self.s.newConnection.connect(self._handle_connect)
self.con = None
def _handle_connect(self):
self.con = self.s.nextPendingConnection()
self.con.readyRead.connect(self._handle_read)
self.con.disconnected.connect(self._handle_disconnect)
logging.info('connected')
def _handle_disconnect(self):
self.con.deleteLater()
QCoreApplication.quit()
def _write(self, callback):
assert self.con
write(self.con, callback)
def _handle_hotkey(self):
logging.info('toggle')
self._write(lambda out: out.writeString(b'toggle'))
def _handle_read(self):
ins = QDataStream(self.con)
set_version(ins)
line = str(ins.readString(), encoding='ascii')
logging.info(line)
windows.goto(int(line))
@Slot()
def start(self):
self.hotkey_thread.start()
if not self.s.listen(QHostAddress.LocalHost, PORT):
logging.error(
"Unable to start the server: %s." %
self.tcpServer.errorString()
)
return
class HotkeyThread(QThread):
fire = Signal()
def __init__(self):
QThread.__init__(self)
from hotkey import Hotkey
self.hotkey = Hotkey(self.handle_hotkey)
def handle_hotkey(self):
self.fire.emit()
def stop(self):
self.hotkey.stop()
def run(self):
self.hotkey.start()
if __name__ == '__main__':
import sys
import os
logging.basicConfig(
filename=os.path.expanduser('~/.lit.server.log'),
filemode='w',
format='%(asctime)s - %(levelname)s - %(message)s',
level=logging.DEBUG
)
app = QCoreApplication(sys.argv)
set_app_info(app, 'litserver')
server = Server()
QMetaObject.invokeMethod(
server,
'start',
Qt.QueuedConnection
)
sys.exit(app.exec_())