From 7c46bf4b9eb0e92973a16e0e1c1ec71a3e52cacc Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 16 Mar 2019 17:05:58 +0200 Subject: [PATCH] Remove remaining traces of ORM --- mautrix_telegram/__main__.py | 15 +++++---------- mautrix_telegram/abstract_user.py | 11 +++-------- mautrix_telegram/bot.py | 7 ++++--- mautrix_telegram/commands/handler.py | 2 +- mautrix_telegram/context.py | 21 ++++++++------------- mautrix_telegram/db/telegram_file.py | 3 +-- mautrix_telegram/matrix.py | 2 +- mautrix_telegram/portal.py | 2 +- mautrix_telegram/puppet.py | 13 ++++--------- setup.py | 4 ++-- 10 files changed, 30 insertions(+), 50 deletions(-) diff --git a/mautrix_telegram/__main__.py b/mautrix_telegram/__main__.py index 5da4d4dc..6c17faee 100644 --- a/mautrix_telegram/__main__.py +++ b/mautrix_telegram/__main__.py @@ -23,7 +23,6 @@ import sys import copy import signal -from sqlalchemy import orm import sqlalchemy as sql from mautrix_appservice import AppService @@ -73,13 +72,10 @@ log = logging.getLogger("mau.init") # type: logging.Logger log.debug(f"Initializing mautrix-telegram {__version__}") db_engine = sql.create_engine(config["appservice.database"] or "sqlite:///mautrix-telegram.db") -db_factory = orm.sessionmaker(bind=db_engine) -db_session = orm.scoping.scoped_session(db_factory) Base.metadata.bind = db_engine -session_container = AlchemySessionContainer(engine=db_engine, session=db_session, - table_base=Base, table_prefix="telethon_", - manage_tables=False) +session_container = AlchemySessionContainer(engine=db_engine, table_base=Base, session=False, + table_prefix="telethon_", manage_tables=False) session_container.core_mode = True try: @@ -102,8 +98,9 @@ appserv = AppService(config["homeserver.address"], config["homeserver.domain"], aiohttp_params={ "client_max_size": config["appservice.max_body_size"] * mebibyte }) - -context = Context(appserv, db_session, config, loop, session_container) +bot = init_bot(config) +context = Context(appserv, config, loop, session_container, bot) +context.mx = MatrixHandler(context) if config["appservice.public.enabled"]: public_website = PublicBridgeWebsite(loop) @@ -120,8 +117,6 @@ with appserv.run(config["appservice.hostname"], config["appservice.port"]) as st start_ts = time() init_db(db_engine) init_abstract_user(context) - context.bot = init_bot(context) - context.mx = MatrixHandler(context) init_formatter(context) init_portal(context) startup_actions = (init_puppet(context) + diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 258dc19b..6839f2e8 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -20,7 +20,6 @@ import asyncio import logging import platform -from sqlalchemy import orm from telethon.tl.patched import MessageService, Message from telethon.tl.types import ( Channel, ChannelForbidden, Chat, ChatForbidden, MessageActionChannelMigrateFrom, PeerUser, @@ -56,7 +55,6 @@ class AbstractUser(ABC): session_container = None # type: AlchemySessionContainer loop = None # type: asyncio.AbstractEventLoop log = None # type: logging.Logger - db = None # type: orm.Session az = None # type: AppService bot = None # type: Bot ignore_incoming_bot_events = True # type: bool @@ -175,11 +173,8 @@ class AbstractUser(ABC): async def ensure_started(self, even_if_no_session=False) -> 'AbstractUser': if not self.puppet_whitelisted or self.connected: return self - session_count = self.session_container.Session.query.filter( - self.session_container.Session.session_id == self.mxid).count() - self.log.debug("ensure_started(%s, even_if_no_session=%s, session_count=%s)", - self.mxid, even_if_no_session, session_count) - if even_if_no_session or session_count > 0: + self.log.debug("ensure_started(%s, even_if_no_session=%s)", self.mxid, even_if_no_session) + if even_if_no_session or self.session_container.has_session(self.mxid): await self.start(delete_unless_authenticated=not even_if_no_session) return self @@ -388,7 +383,7 @@ class AbstractUser(ABC): def init(context: "Context") -> None: global config, MAX_DELETIONS - AbstractUser.az, AbstractUser.db, config, AbstractUser.loop, AbstractUser.relaybot = context.core + AbstractUser.az, config, AbstractUser.loop, AbstractUser.relaybot = context.core AbstractUser.ignore_incoming_bot_events = config["bridge.relaybot.ignore_own_incoming_events"] AbstractUser.session_container = context.session_container MAX_DELETIONS = config.get("bridge.max_telegram_delete", 10) diff --git a/mautrix_telegram/bot.py b/mautrix_telegram/bot.py index 3a35792c..db3f40ea 100644 --- a/mautrix_telegram/bot.py +++ b/mautrix_telegram/bot.py @@ -56,7 +56,7 @@ class Bot(AbstractUser): self.username = None # type: str self.is_relaybot = True # type: bool self.is_bot = True # type: bool - self.chats = {chat.id: chat.type for chat in BotChat.all()} # type: Dict[int, str] + self.chats = {} # type: Dict[int, str] self.tg_whitelist = [] # type: List[int] self.whitelist_group_admins = (config["bridge.relaybot.whitelist_group_admins"] or False) # type: bool @@ -74,6 +74,7 @@ class Bot(AbstractUser): self.tg_whitelist.append(user_id) async def start(self, delete_unless_authenticated: bool = False) -> 'Bot': + self.chats = {chat.id: chat.type for chat in BotChat.all()} await super().start(delete_unless_authenticated) if not await self.is_logged_in(): await self.client.sign_in(bot_token=self.token) @@ -280,9 +281,9 @@ class Bot(AbstractUser): return "bot" -def init(context: 'Context') -> Optional[Bot]: +def init(cfg: 'Config') -> Optional[Bot]: global config - config = context.config + config = cfg token = config["telegram.bot_token"] if token and not token.lower().startswith("disable"): return Bot(token) diff --git a/mautrix_telegram/commands/handler.py b/mautrix_telegram/commands/handler.py index 43b45968..dc432605 100644 --- a/mautrix_telegram/commands/handler.py +++ b/mautrix_telegram/commands/handler.py @@ -328,7 +328,7 @@ class CommandProcessor: log = logging.getLogger("mau.commands") def __init__(self, context: c.Context) -> None: - self.az, self.db, self.config, self.loop, self.tgbot = context.core + self.az, self.config, self.loop, self.tgbot = context.core self.public_website = context.public_website self.command_prefix = self.config["bridge.command_prefix"] diff --git a/mautrix_telegram/context.py b/mautrix_telegram/context.py index 9f2375d8..f1b99830 100644 --- a/mautrix_telegram/context.py +++ b/mautrix_telegram/context.py @@ -19,8 +19,6 @@ from typing import Optional, Tuple, TYPE_CHECKING if TYPE_CHECKING: import asyncio - from sqlalchemy.orm import scoped_session - from alchemysession import AlchemySessionContainer from mautrix_appservice import AppService @@ -31,20 +29,17 @@ if TYPE_CHECKING: class Context: - def __init__(self, az: 'AppService', db: 'scoped_session', config: 'Config', - loop: 'asyncio.AbstractEventLoop', session_container: 'AlchemySessionContainer' - ) -> None: + def __init__(self, az: 'AppService', config: 'Config', loop: 'asyncio.AbstractEventLoop', + session_container: 'AlchemySessionContainer', bot: Optional['Bot']) -> None: self.az = az # type: AppService - self.db = db # type: scoped_session self.config = config # type: Config self.loop = loop # type: asyncio.AbstractEventLoop - self.bot = None # type: Optional[Bot] - self.mx = None # type: MatrixHandler + self.bot = bot # type: Optional[Bot] + self.mx = None # type: Optional[MatrixHandler] self.session_container = session_container # type: AlchemySessionContainer - self.public_website = None # type: PublicBridgeWebsite - self.provisioning_api = None # type: ProvisioningAPI + self.public_website = None # type: Optional[PublicBridgeWebsite] + self.provisioning_api = None # type: Optional[ProvisioningAPI] @property - def core(self) -> Tuple['AppService', 'scoped_session', 'Config', - 'asyncio.AbstractEventLoop', Optional['Bot']]: - return (self.az, self.db, self.config, self.loop, self.bot) + def core(self) -> Tuple['AppService', 'Config', 'asyncio.AbstractEventLoop', Optional['Bot']]: + return self.az, self.config, self.loop, self.bot diff --git a/mautrix_telegram/db/telegram_file.py b/mautrix_telegram/db/telegram_file.py index c8f53a39..65070533 100644 --- a/mautrix_telegram/db/telegram_file.py +++ b/mautrix_telegram/db/telegram_file.py @@ -15,7 +15,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . from sqlalchemy import Column, ForeignKey, Integer, BigInteger, String, Boolean -from sqlalchemy.orm import relationship from typing import Optional from .base import Base @@ -33,7 +32,7 @@ class TelegramFile(Base): width = Column(Integer, nullable=True) height = Column(Integer, nullable=True) thumbnail_id = Column("thumbnail", String, ForeignKey("telegram_file.id"), nullable=True) - thumbnail = relationship("TelegramFile", uselist=False) + thumbnail = None # type: Optional[TelegramFile] @classmethod def get(cls, id: str) -> Optional['TelegramFile']: diff --git a/mautrix_telegram/matrix.py b/mautrix_telegram/matrix.py index 573cbd75..3f80bc94 100644 --- a/mautrix_telegram/matrix.py +++ b/mautrix_telegram/matrix.py @@ -32,7 +32,7 @@ class MatrixHandler: log = logging.getLogger("mau.mx") # type: logging.Logger def __init__(self, context: 'Context') -> None: - self.az, self.db, self.config, _, self.tgbot = context.core + self.az, self.config, _, self.tgbot = context.core self.commands = com.CommandProcessor(context) # type: com.CommandProcessor self.previously_typing = [] # type: List[MatrixUserID] diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index d81655d7..a4d54824 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -2039,7 +2039,7 @@ class Portal: def init(context: Context) -> None: global config - Portal.az, _, config, Portal.loop, Portal.bot = context.core + Portal.az, config, Portal.loop, Portal.bot = context.core Portal.max_initial_member_sync = config["bridge.max_initial_member_sync"] Portal.sync_channel_members = config["bridge.sync_channel_members"] Portal.sync_matrix_state = config["bridge.sync_matrix_state"] diff --git a/mautrix_telegram/puppet.py b/mautrix_telegram/puppet.py index b22d9210..a3569997 100644 --- a/mautrix_telegram/puppet.py +++ b/mautrix_telegram/puppet.py @@ -14,8 +14,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import (Awaitable, Coroutine, Dict, List, Iterable, Optional, Pattern, Union, - TYPE_CHECKING) +from typing import Awaitable, Any, Dict, List, Iterable, Optional, Pattern, Union, TYPE_CHECKING from difflib import SequenceMatcher from enum import Enum from aiohttp import ServerDisconnectedError @@ -23,8 +22,6 @@ import asyncio import logging import re -from sqlalchemy import orm - from telethon.tl.types import UserProfilePhoto, User, FileLocation, UpdateUserName, PeerUser from mautrix_appservice import AppService, IntentAPI, IntentError, MatrixRequestError @@ -45,7 +42,6 @@ config = None # type: Config class Puppet: log = logging.getLogger("mau.puppet") # type: logging.Logger - db = None # type: orm.Session az = None # type: AppService mx = None # type: MatrixHandler loop = None # type: asyncio.AbstractEventLoop @@ -400,8 +396,7 @@ class Puppet: if create: puppet = cls(tgid) - cls.db.add(puppet.db_instance) - cls.db.commit() + puppet.db_instance.insert() return puppet return None @@ -481,9 +476,9 @@ class Puppet: # endregion -def init(context: 'Context') -> List[Coroutine]: # [None, None, PuppetError] +def init(context: 'Context') -> List[Awaitable[Any]]: # [None, None, PuppetError] global config - Puppet.az, Puppet.db, config, Puppet.loop, _ = context.core + Puppet.az, config, Puppet.loop, _ = context.core Puppet.mx = context.mx Puppet.username_template = config.get("bridge.username_template", "telegram_{userid}") Puppet.hs_domain = config["homeserver"]["domain"] diff --git a/setup.py b/setup.py index 68078c58..860da486 100644 --- a/setup.py +++ b/setup.py @@ -39,8 +39,8 @@ setuptools.setup( "ruamel.yaml>=0.15.35,<0.16", "future-fstrings>=0.4.2", "python-magic>=0.4.15,<0.5", - "telethon>=1.5.5,<1.6", - "telethon-session-sqlalchemy>=0.2.9,<0.3", + "telethon>=1.5.5,<1.7", + "telethon-session-sqlalchemy>=0.2.11,<0.3", ], extras_require=extras,