Stop passing loggers around and organize imports

This commit is contained in:
Tulir Asokan
2018-02-06 18:10:19 +02:00
parent 9ea0ab3df1
commit 13c2d327fb
11 changed files with 45 additions and 30 deletions
+7 -5
View File
@@ -16,12 +16,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Partly based on github.com/Cadair/python-appservice-framework (MIT license)
import asyncio
import logging
import aiohttp
from aiohttp import web
from functools import partial
from contextlib import contextmanager
from aiohttp import web
import aiohttp
import asyncio
import logging
from .intent_api import HTTPAPI
from .state_store import StateStore
@@ -43,7 +44,8 @@ class AppService:
self._intent = None
self.loop = loop or asyncio.get_event_loop()
self.log = log or logging.getLogger("mautrix_appservice")
self.log = (logging.getLogger(log) if isinstance(log, str)
else log or logging.getLogger("mautrix_appservice"))
self.query_user = query_user or (lambda user: None)
self.query_alias = query_alias or (lambda alias: None)
+1
View File
@@ -18,6 +18,7 @@ import re
import json
import magic
import urllib.request
from matrix_client.api import MatrixHttpApi
from matrix_client.errors import MatrixRequestError
+4 -4
View File
@@ -31,7 +31,7 @@ from .db import init as init_db
from .user import init as init_user
from .portal import init as init_portal
from .puppet import init as init_puppet
from .formatter import init as init_formatter
# from .formatter import init as init_formatter
log = logging.getLogger("mau")
time_formatter = logging.Formatter("[%(asctime)s] [%(levelname)s@%(name)s] %(message)s")
@@ -74,12 +74,12 @@ Base.metadata.create_all()
appserv = AppService(config["homeserver.address"], config["homeserver.domain"],
config["appservice.as_token"], config["appservice.hs_token"],
config["appservice.bot_username"], log=log.getChild("as"))
context = (appserv, db_session, log, config)
config["appservice.bot_username"], log="mau.as")
context = (appserv, db_session, config)
with appserv.run(config["appservice.hostname"], config["appservice.port"]) as start:
init_db(db_session)
init_formatter(context)
# init_formatter(context)
init_portal(context)
init_puppet(context)
init_user(context)
+7 -2
View File
@@ -16,12 +16,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from contextlib import contextmanager
import markdown
import logging
from matrix_client.errors import MatrixRequestError
from telethon.errors import *
from telethon.tl.types import *
from telethon.tl.functions.contacts import SearchRequest
from telethon.tl.functions.messages import ImportChatInviteRequest, CheckChatInviteRequest
from telethon.tl.functions.channels import JoinChannelRequest
from . import puppet as pu, portal as po
command_handlers = {}
@@ -51,9 +55,10 @@ def format_duration(seconds):
class CommandHandler:
log = logging.getLogger("mau.commands")
def __init__(self, context):
self.az, self.db, log, self.config = context
self.log = log.getChild("commands")
self.az, self.db, self.config = context
self.command_prefix = self.config["bridge.command_prefix"]
self._room_id = None
self._is_management = False
+2 -1
View File
@@ -14,7 +14,8 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sqlalchemy import Column, ForeignKey, UniqueConstraint, Integer, String
from sqlalchemy import Column, UniqueConstraint, Integer, String
from .base import Base
+5 -7
View File
@@ -14,15 +14,18 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
from html import escape, unescape
from html.parser import HTMLParser
from collections import deque
import re
import logging
from telethon.tl.types import *
from . import user as u, puppet as p
from .db import Message as DBMessage
log = None
log = logging.getLogger("mau.formatter")
# region Matrix to Telegram
@@ -314,8 +317,3 @@ def _telegram_to_matrix(text, entities):
# endregion
def init(context):
global log
_, _, parent_log, _ = context
log = parent_log.getChild("formatter")
+5 -2
View File
@@ -14,6 +14,8 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from matrix_client.errors import MatrixRequestError
from .user import User
@@ -23,9 +25,10 @@ from .commands import CommandHandler
class MatrixHandler:
log = logging.getLogger("mau.mx")
def __init__(self, context):
self.az, self.db, log, self.config = context
self.log = log.getChild("mx")
self.az, self.db, self.config = context
self.commands = CommandHandler(context)
self.az.matrix_event_handler(self.handle_event)
+3 -3
View File
@@ -19,6 +19,7 @@ from collections import deque
from datetime import datetime
import mimetypes
import hashlib
import logging
from PIL import Image
import magic
@@ -37,7 +38,7 @@ config = None
class Portal:
log = None
log = logging.getLogger("mau.portal")
db = None
az = None
by_mxid = {}
@@ -897,5 +898,4 @@ class Portal:
def init(context):
global config
Portal.az, Portal.db, log, config = context
Portal.log = log.getChild("portal")
Portal.az, Portal.db, config = context
+5 -3
View File
@@ -15,15 +15,18 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import logging
from telethon.tl.types import UserProfilePhoto, PeerUser
from telethon.errors.rpc_error_list import LocationInvalidError
from .db import Puppet as DBPuppet
config = None
class Puppet:
log = None
log = logging.getLogger("mau.puppet")
db = None
az = None
mxid_regex = None
@@ -167,8 +170,7 @@ class Puppet:
def init(context):
global config
Puppet.az, Puppet.db, log, config = context
Puppet.log = log.getChild("puppet")
Puppet.az, Puppet.db, config = context
localpart = config.get("bridge.username_template", "telegram_{userid}").format(userid="(.+)")
hs = config["homeserver"]["domain"]
Puppet.mxid_regex = re.compile(f"@{localpart}:{hs}")
+1
View File
@@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from io import BytesIO
from telethon import TelegramClient
from telethon.tl.functions.messages import SendMessageRequest, SendMediaRequest
from telethon.tl.types import *
+5 -3
View File
@@ -14,8 +14,11 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from telethon.tl.types import *
from telethon.tl.types import User as TLUser
from .db import User as DBUser, Message as DBMessage
from .tgclient import MautrixTelegramClient
from . import portal as po, puppet as pu
@@ -24,7 +27,7 @@ config = None
class User:
log = None
log = logging.getLogger("mau.user")
db = None
az = None
by_mxid = {}
@@ -306,8 +309,7 @@ class User:
def init(context):
global config
User.az, User.db, log, config = context
User.log = log.getChild("user")
User.az, User.db, config = context
users = [User.from_db(user) for user in DBUser.query.all()]
for user in users: