poljar / weechat-matrix

Weechat Matrix protocol script written in python
Other
954 stars 119 forks source link

Fix compatibility with matrix-nio 0.21 #353

Closed schopin-pro closed 1 year ago

schopin-pro commented 1 year ago

The 0.20.0 made a breaking change in how they handle logging, moving off logbook to the standard logging module, breaking weechat-matrix's config module.

This patch adresses the API change (without migrating ourselves to logboox), and bumps the matrix-nio requirements to reflect the dependency on the new API.

jspricke commented 1 year ago

Thanks for working on this! I think it makes sense to switch weechat-matrix to logging as well. Here is a quick patch:

diff --git a/main.py b/main.py
index 765043a..91838b6 100644
--- a/main.py
+++ b/main.py
@@ -29,6 +29,7 @@ activate_this = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'venv'
 if os.path.exists(activate_this):
     exec(open(activate_this).read(), {'__file__': activate_this})

+import logging
 import socket
 import ssl
 import textwrap
@@ -38,11 +39,9 @@ from itertools import chain
 # pylint: disable=unused-import
 from typing import Any, AnyStr, Deque, Dict, List, Optional, Set, Text, Tuple

-import logbook
 import json
 import OpenSSL.crypto as crypto
 from future.utils import bytes_to_native_str as n
-from logbook import Logger, StreamHandler

 try:
     from json.decoder import JSONDecodeError
@@ -114,7 +113,7 @@ WEECHAT_SCRIPT_LICENSE = "ISC"                                 # type: str
 # yapf: enable

-logger = Logger("matrix-cli")
+logger = logging.getLogger(__name__)

 def print_certificate_info(buff, sock, cert):
@@ -532,20 +531,8 @@ def server_buffer_cb(server_name, buffer, input_data):
     return W.WEECHAT_RC_OK

-class WeechatHandler(StreamHandler):
-    def __init__(self, level=logbook.NOTSET, format_string=None, filter=None,
-                 bubble=False):
-        StreamHandler.__init__(
-            self,
-            object(),
-            level,
-            format_string,
-            None,
-            filter,
-            bubble
-        )
-
-    def write(self, item):
+class WeechatHandler(logging.StreamHandler):
+    def emit(self, record):
         buf = ""

         if G.CONFIG.network.debug_buffer:
@@ -555,7 +542,7 @@ class WeechatHandler(StreamHandler):

             buf = G.CONFIG.debug_buffer

-        W.prnt(buf, item)
+        W.prnt(buf, record)

 def buffer_switch_cb(_, _signal, buffer_ptr):
@@ -688,8 +675,7 @@ if __name__ == "__main__":
             W.prnt("", message)

         handler = WeechatHandler()
-        handler.format_string = "{record.channel}: {record.message}"
-        handler.push_application()
+        logger.addHandler(handler)

         # TODO if this fails we should abort and unload the script.
         G.CONFIG = MatrixConfig()
diff --git a/matrix/globals.py b/matrix/globals.py
index c3e099e..22a8837 100644
--- a/matrix/globals.py
+++ b/matrix/globals.py
@@ -16,9 +16,9 @@

 from __future__ import unicode_literals

+import logging
 import sys
 from typing import Any, Dict, Optional
-from logbook import Logger
 from collections import OrderedDict

 from .utf import WeechatWrapper
@@ -44,5 +44,5 @@ ENCRYPTION = True  # type: bool
 SCRIPT_NAME = "matrix"  # type: str
 BUFFER_NAME_PREFIX = "{}.".format(SCRIPT_NAME)  # type: str
 TYPING_NOTICE_TIMEOUT = 4000  # 4 seconds typing notice lifetime
-LOGGER = Logger("weechat-matrix")
+LOGGER = logging.getLogger(__name__)
 UPLOADS = OrderedDict()  # type: Dict[str, Upload]
diff --git a/pyproject.toml b/pyproject.toml
index 7b3dca8..6690240 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -17,9 +17,8 @@ webcolors = "^1.11.1"
 atomicwrites = "^1.3.0"
 future = "^0.18.2"
 attrs = "^19.3.0"
-logbook = "^1.5.3"
 pygments = "^2.6.1"
-matrix-nio = { version = "^0.18.0", extras = [ "e2e" ] }
+matrix-nio = { version = "^0.21.0", extras = [ "e2e" ] }
 python-magic = { version = "^0.4.15", optional = true }
 aiohttp = { version = "^3.6.2", optional = true }
 requests = { version = "^2.23.0", optional = true }

Feel free to add if you agree.

3v1n0 commented 1 year ago

@poljar hey, since there are some PR's waiting, I know that this project is in maintainance mode but sice it's basically the only way to use matrix through weechat in a proper way, would be still nice to keep it working.

Would you be ok adding more contributors to be able to review/merge stuff or do you think is better to move to another fork?

Cheers!

poljar commented 1 year ago

@poljar hey, since there are some PR's waiting, I know that this project is in maintainance mode but sice it's basically the only way to use matrix through weechat in a proper way, would be still nice to keep it working.

Would you be ok adding more contributors to be able to review/merge stuff or do you think is better to move to another fork?

Cheers!

Yes please, if there are people who'd like to help maintaining, I would be happy to add more contributors.