Add functions to build init/sync commands

This commit is contained in:
Sébastien Helleu 2021-11-14 10:04:24 +01:00
parent aae59e29e6
commit bb96884151

View File

@ -29,20 +29,22 @@ from PySide6 import QtCore, QtNetwork
from qweechat import config from qweechat import config
_PROTO_INIT_CMD = ['init password=%(password)s'] _PROTO_INIT_CMD = [
# initialize with the password
'init password=%(password)s',
]
_PROTO_SYNC_CMDS = [ _PROTO_SYNC_CMDS = [
# get buffers
'(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,' '(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,'
'type,nicklist,title,local_variables', 'type,nicklist,title,local_variables',
# get lines
'(listlines) hdata buffer:gui_buffers(*)/own_lines/last_line(-%(lines)d)/' '(listlines) hdata buffer:gui_buffers(*)/own_lines/last_line(-%(lines)d)/'
'data date,displayed,prefix,message', 'data date,displayed,prefix,message',
# get nicklist for all buffers
'(nicklist) nicklist', '(nicklist) nicklist',
# enable synchronization
'sync', 'sync',
''
] ]
STATUS_DISCONNECTED = 'disconnected' STATUS_DISCONNECTED = 'disconnected'
@ -86,13 +88,22 @@ class Network(QtCore.QObject):
self._socket.readyRead.connect(self._socket_read) self._socket.readyRead.connect(self._socket_read)
self._socket.disconnected.connect(self._socket_disconnected) self._socket.disconnected.connect(self._socket_disconnected)
def _build_init_command(self):
"""Build the init command to send to WeeChat."""
cmd = '\n'.join(_PROTO_INIT_CMD) + '\n'
return cmd % {'password': self._password}
def _build_sync_command(self):
"""Build the sync commands to send to WeeChat."""
cmd = '\n'.join(_PROTO_SYNC_CMDS) + '\n'
return cmd % {'lines': self._lines}
def _socket_connected(self): def _socket_connected(self):
"""Slot: socket connected.""" """Slot: socket connected."""
self.statusChanged.emit(STATUS_CONNECTED, None) self.statusChanged.emit(STATUS_CONNECTED, None)
if self._password: if self._password:
self.send_to_weechat('\n'.join(_PROTO_INIT_CMD + _PROTO_SYNC_CMDS) cmd = self._build_init_command() + self._build_sync_command()
% {'password': str(self._password), self.send_to_weechat(cmd)
'lines': self._lines})
def _socket_read(self): def _socket_read(self):
"""Slot: data available on socket.""" """Slot: data available on socket."""
@ -177,8 +188,7 @@ class Network(QtCore.QObject):
def sync_weechat(self): def sync_weechat(self):
"""Synchronize with WeeChat.""" """Synchronize with WeeChat."""
self.send_to_weechat('\n'.join(_PROTO_SYNC_CMDS) self.send_to_weechat(self._build_sync_command())
% {'lines': self._lines})
def status_label(self, status): def status_label(self, status):
"""Return the label for a given status.""" """Return the label for a given status."""