Move message handling function into the server class.

This commit is contained in:
poljar (Damir Jelić) 2018-02-25 17:59:08 +01:00
parent 6fdea4d213
commit efa663133b
3 changed files with 67 additions and 98 deletions

15
main.py
View file

@ -31,8 +31,7 @@ from typing import (List, Set, Dict, Tuple, Text, Optional, AnyStr, Deque, Any)
from matrix.colors import Formatted from matrix.colors import Formatted
from matrix.utf import utf8_decode from matrix.utf import utf8_decode
from matrix.http import HttpResponse from matrix.http import HttpResponse
from matrix.api import (MessageType, MatrixSendMessage) from matrix.api import MatrixSendMessage
from matrix.messages import handle_http_response
# Weechat searches for the registered callbacks in the scope of the main script # Weechat searches for the registered callbacks in the scope of the main script
# file, import the callbacks here so weechat can find them. # file, import the callbacks here so weechat can find them.
@ -62,13 +61,9 @@ from matrix.completion import (
matrix_message_completion_cb, matrix_server_completion_cb) matrix_message_completion_cb, matrix_server_completion_cb)
from matrix.utils import (key_from_value, server_buffer_prnt, prnt_debug, from matrix.utils import (key_from_value, server_buffer_prnt, prnt_debug,
tags_from_line_data, server_buffer_set_title) server_buffer_set_title)
from matrix.plugin_options import ( from matrix.plugin_options import (DebugType, RedactType)
DebugType,
RedactType,
ServerBufferType,
)
from matrix.config import (matrix_config_init, matrix_config_read, from matrix.config import (matrix_config_init, matrix_config_read,
matrix_config_free, matrix_config_change_cb, matrix_config_free, matrix_config_change_cb,
@ -76,7 +71,7 @@ from matrix.config import (matrix_config_init, matrix_config_read,
import matrix.globals import matrix.globals
from matrix.globals import W, OPTIONS, SERVERS from matrix.globals import W, SERVERS
# yapf: disable # yapf: disable
WEECHAT_SCRIPT_NAME = "matrix" # type: str WEECHAT_SCRIPT_NAME = "matrix" # type: str
@ -259,7 +254,7 @@ def receive_cb(server_name, file_descriptor):
# Message done, reset the parser state. # Message done, reset the parser state.
server.reset_parser() server.reset_parser()
handle_http_response(server, message) server.handle_response(message)
break break
return W.WEECHAT_RC_OK return W.WEECHAT_RC_OK

View file

@ -1,88 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright © 2018 Damir Jelić <poljar@termina.org.uk>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import unicode_literals
from builtins import str
import time
import pprint
import datetime
from matrix.globals import W
import matrix.api as API
from matrix.utils import server_buffer_prnt, prnt_debug
from matrix.plugin_options import DebugType
def print_message_error(server, message):
server_buffer_prnt(server, ("{prefix}Unhandled {status_code} error, please "
"inform the developers about this.").format(
prefix=W.prefix("error"),
status_code=message.response.status))
server_buffer_prnt(server, pprint.pformat(message.__class__.__name__))
server_buffer_prnt(server, pprint.pformat(message.request.payload))
server_buffer_prnt(server, pprint.pformat(message.response.body))
def handle_http_response(server, message):
# type: (MatrixServer, MatrixMessage) -> None
assert message.response
if ('content-type' in message.response.headers and
message.response.headers['content-type'] == 'application/json'):
ret, error = message.decode_body(server)
if not ret:
message = ("{prefix}matrix: Error decoding json response from "
"server: {error}").format(
prefix=W.prefix("error"), error=error)
W.prnt(server.server_buffer, message)
return
event = message.event
event.execute()
else:
status_code = message.response.status
if status_code == 504:
if isinstance(message, API.MatrixSyncMessage):
server.sync()
else:
print_message_error(server, message)
else:
print_message_error(server, message)
creation_date = datetime.datetime.fromtimestamp(message.creation_time)
done_time = time.time()
info_message = ("Message of type {t} created at {c}."
"\nMessage lifetime information:"
"\n Send delay: {s} ms"
"\n Receive delay: {r} ms"
"\n Handling time: {h} ms"
"\n Total time: {total} ms").format(
t=message.type,
c=creation_date,
s=(message.send_time - message.creation_time) * 1000,
r=(message.receive_time - message.send_time) * 1000,
h=(done_time - message.receive_time) * 1000,
total=(done_time - message.creation_time) * 1000,
)
prnt_debug(DebugType.TIMING, server, info_message)
return

View file

@ -20,6 +20,8 @@ from builtins import str, bytes
import ssl import ssl
import socket import socket
import time import time
import datetime
import pprint
from collections import deque from collections import deque
from http_parser.pyparser import HttpParser from http_parser.pyparser import HttpParser
@ -29,6 +31,7 @@ from matrix.utils import (key_from_value, prnt_debug, server_buffer_prnt,
create_server_buffer) create_server_buffer)
from matrix.utf import utf8_decode from matrix.utf import utf8_decode
from matrix.globals import W, SERVERS, OPTIONS from matrix.globals import W, SERVERS, OPTIONS
import matrix.api as API
from matrix.api import MatrixClient, MatrixSyncMessage, MatrixLoginMessage from matrix.api import MatrixClient, MatrixSyncMessage, MatrixLoginMessage
@ -374,6 +377,65 @@ class MatrixServer:
self.device_name) self.device_name)
self.send_or_queue(message) self.send_or_queue(message)
def _print_message_error(self, message):
server_buffer_prnt(self,
("{prefix}Unhandled {status_code} error, please "
"inform the developers about this.").format(
prefix=W.prefix("error"),
status_code=message.response.status))
server_buffer_prnt(self, pprint.pformat(message.__class__.__name__))
server_buffer_prnt(self, pprint.pformat(message.request.payload))
server_buffer_prnt(self, pprint.pformat(message.response.body))
def handle_response(self, message):
# type: (MatrixMessage) -> None
assert message.response
if ('content-type' in message.response.headers and
message.response.headers['content-type'] == 'application/json'):
ret, error = message.decode_body(self)
if not ret:
message = ("{prefix}matrix: Error decoding json response from "
"server: {error}").format(
prefix=W.prefix("error"), error=error)
W.prnt(self.server_buffer, message)
return
event = message.event
event.execute()
else:
status_code = message.response.status
if status_code == 504:
if isinstance(message, API.MatrixSyncMessage):
self.sync()
else:
self._print_message_error(message)
else:
self._print_message_error(message)
creation_date = datetime.datetime.fromtimestamp(message.creation_time)
done_time = time.time()
info_message = (
"Message of type {t} created at {c}."
"\nMessage lifetime information:"
"\n Send delay: {s} ms"
"\n Receive delay: {r} ms"
"\n Handling time: {h} ms"
"\n Total time: {total} ms").format(
t=message.type,
c=creation_date,
s=(message.send_time - message.creation_time) * 1000,
r=(message.receive_time - message.send_time) * 1000,
h=(done_time - message.receive_time) * 1000,
total=(done_time - message.creation_time) * 1000,
)
prnt_debug(DebugType.TIMING, self, info_message)
return
@utf8_decode @utf8_decode
def matrix_config_server_read_cb(data, config_file, section, option_name, def matrix_config_server_read_cb(data, config_file, section, option_name,