forked from quodlibet/quodlibet
/
remote.py
169 lines (126 loc) · 4.2 KB
/
remote.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
# Copyright 2014 Christoph Reiter
# 2020 Nick Boultbee
#
# 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 2 of the License, or
# (at your option) any later version.
import os
from typing import Type
from senf import path2fsn, fsn2bytes, bytes2fsn, fsnative
from quodlibet.util import fifo, print_w
from quodlibet import get_user_dir
try:
from quodlibet.util import winpipe
except ImportError:
winpipe = None # type: ignore
class RemoteError(Exception):
pass
class RemoteBase:
"""A thing for communicating with existing instances of ourself."""
def __init__(self, app, cmd_registry):
"""
Args:
app (Application)
cmd_registry (CommandRegistry)
"""
raise NotImplementedError
@classmethod
def remote_exists(self):
"""See if another instance exists
Returns:
bool
"""
raise NotImplementedError
@classmethod
def send_message(cls, message):
"""Send data to the existing instance if possible and returns
a response.
Args:
message (fsnative)
Returns:
fsnative or None
Raises:
RemoteError: in case the message couldn't be send or
there was no response.
"""
raise NotImplementedError
def start(self):
"""Start the listener for other instances.
Raises:
RemoteError: in case another instance is already listening.
"""
raise NotImplementedError
def stop(self):
"""Stop the listener for other instances"""
raise NotImplementedError
class QuodLibetWinRemote(RemoteBase):
_NAME = "quodlibet"
def __init__(self, app, cmd_registry):
self._app = app
self._cmd_registry = cmd_registry
self._server = winpipe.NamedPipeServer(self._NAME, self._callback)
@classmethod
def remote_exists(cls):
return winpipe.pipe_exists(cls._NAME)
@classmethod
def send_message(cls, message):
data = fsn2bytes(path2fsn(message), "utf-8")
try:
winpipe.write_pipe(cls._NAME, data)
except EnvironmentError as e:
raise RemoteError(e)
def start(self):
try:
self._server.start()
except winpipe.NamedPipeServerError as e:
raise RemoteError(e)
def stop(self):
self._server.stop()
def _callback(self, data):
message = bytes2fsn(data, "utf-8")
self._cmd_registry.handle_line(self._app, message)
class QuodLibetUnixRemote(RemoteBase):
_FIFO_NAME = "control"
_PATH = os.path.join(get_user_dir(), _FIFO_NAME)
def __init__(self, app, cmd_registry):
self._app = app
self._cmd_registry = cmd_registry
self._fifo = fifo.FIFO(self._PATH, self._callback)
@classmethod
def remote_exists(cls):
return fifo.fifo_exists(cls._PATH)
@classmethod
def send_message(cls, message):
assert isinstance(message, fsnative)
try:
return fifo.write_fifo(cls._PATH, fsn2bytes(message, None))
except EnvironmentError as e:
raise RemoteError(e)
def start(self):
try:
self._fifo.open()
except fifo.FIFOError as e:
raise RemoteError(e)
def stop(self):
self._fifo.destroy()
def _callback(self, data):
try:
messages = list(fifo.split_message(data))
except ValueError:
print_w("invalid message: %r" % data)
return
for command, path in messages:
command = bytes2fsn(command, None)
response = self._cmd_registry.handle_line(self._app, command)
if path is not None:
path = bytes2fsn(path, None)
with open(path, "wb") as h:
if response is not None:
assert isinstance(response, fsnative)
h.write(fsn2bytes(response, None))
Remote: Type[RemoteBase]
if os.name == "nt":
Remote = QuodLibetWinRemote
else:
Remote = QuodLibetUnixRemote