mirror of
https://github.com/lilydjwg/fcitx.vim.git
synced 2026-05-14 17:41:12 +08:00
fcitx socket connection
This commit is contained in:
parent
e70e870b97
commit
3a69ec3ff6
1 changed files with 11 additions and 16 deletions
|
|
@ -20,13 +20,13 @@ class FcitxComm(object):
|
||||||
self.sock = None
|
self.sock = None
|
||||||
|
|
||||||
def status(self):
|
def status(self):
|
||||||
return self._with_reconnect(self._status) == 2
|
return self._with_socket(self._status) == 2
|
||||||
|
|
||||||
def activate(self):
|
def activate(self):
|
||||||
self._with_reconnect(self._command, self.ACTIVATE)
|
self._with_socket(self._command, self.ACTIVATE)
|
||||||
|
|
||||||
def deactivate(self):
|
def deactivate(self):
|
||||||
self._with_reconnect(self._command, self.DEACTIVATE)
|
self._with_socket(self._command, self.DEACTIVATE)
|
||||||
|
|
||||||
def _error(self, e):
|
def _error(self, e):
|
||||||
estr = str(e).replace('"', r'\"')
|
estr = str(e).replace('"', r'\"')
|
||||||
|
|
@ -39,25 +39,20 @@ class FcitxComm(object):
|
||||||
try:
|
try:
|
||||||
sock.connect(self.socketfile)
|
sock.connect(self.socketfile)
|
||||||
return True
|
return True
|
||||||
except (socket.error, socket.timeout, struct.error) as e:
|
except (socket.error, socket.timeout) as e:
|
||||||
self._error(e)
|
self._error(e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _with_reconnect(self, func, *args, **kwargs):
|
def _with_socket(self, func, *args, **kwargs):
|
||||||
if self.sock is None:
|
# fcitx doesn't support connection reuse
|
||||||
if not self._connect():
|
if not self._connect():
|
||||||
return
|
return
|
||||||
|
|
||||||
for _ in range(2):
|
with self.sock:
|
||||||
try:
|
try:
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
except (socket.error, socket.timeout) as e:
|
except (socket.error, socket.timeout, struct.error) as e:
|
||||||
if self._connect():
|
self._error(e)
|
||||||
continue
|
|
||||||
else:
|
|
||||||
return
|
|
||||||
|
|
||||||
self._error(e)
|
|
||||||
|
|
||||||
def _status(self):
|
def _status(self):
|
||||||
self.sock.send(self.STATUS)
|
self.sock.send(self.STATUS)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue