Compare commits
3
Commits
3a61363189
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c750a80206 | ||
|
|
5c4b1a7dbd | ||
|
|
f9c6845da4 |
@@ -0,0 +1,2 @@
|
||||
aiohttp
|
||||
Telethon
|
||||
+37
-12
@@ -1,13 +1,38 @@
|
||||
aiohttp==3.6.3
|
||||
async-timeout==3.0.1
|
||||
attrs==20.3.0
|
||||
chardet==3.0.4
|
||||
idna==3.1
|
||||
idna-ssl==1.1.0
|
||||
multidict==4.7.6
|
||||
# This file was autogenerated by uv via the following command:
|
||||
# uv pip compile --python-version 3.11 -o requirements.txt requirements.in
|
||||
aiohappyeyeballs==2.7.1
|
||||
# via aiohttp
|
||||
aiohttp==3.14.5
|
||||
# via -r requirements.in
|
||||
aiosignal==1.4.0
|
||||
# via aiohttp
|
||||
attrs==26.1.0
|
||||
# via aiohttp
|
||||
frozenlist==1.8.0
|
||||
# via
|
||||
# aiohttp
|
||||
# aiosignal
|
||||
idna==3.20
|
||||
# via yarl
|
||||
multidict==7.2.1
|
||||
# via
|
||||
# aiohttp
|
||||
# yarl
|
||||
propcache==0.5.4
|
||||
# via
|
||||
# aiohttp
|
||||
# yarl
|
||||
pyaes==1.6.1
|
||||
pyasn1==0.4.8
|
||||
rsa==4.7.2
|
||||
telethon==1.45.0
|
||||
typing-extensions==3.7.4.3
|
||||
yarl==1.5.1
|
||||
# via telethon
|
||||
pyasn1==0.6.5
|
||||
# via rsa
|
||||
rsa==4.9.1
|
||||
# via telethon
|
||||
telethon==1.46.0
|
||||
# via -r requirements.in
|
||||
typing-extensions==4.16.0
|
||||
# via
|
||||
# aiohttp
|
||||
# aiosignal
|
||||
yarl==1.25.1
|
||||
# via aiohttp
|
||||
|
||||
+17
-6
@@ -6,7 +6,7 @@ import re
|
||||
import settings
|
||||
from telethon import TelegramClient, events
|
||||
|
||||
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN)
|
||||
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH)
|
||||
web = None
|
||||
api = lambda route: settings.DYNMAP_ADDRESS + route
|
||||
md5 = lambda name: int(hashlib.md5(name.encode()).hexdigest(), 16)
|
||||
@@ -58,9 +58,7 @@ async def new_message(event):
|
||||
logging.exception('Problem sending message to dynmap:')
|
||||
|
||||
|
||||
async def main():
|
||||
global web
|
||||
web = aiohttp.ClientSession()
|
||||
async def bridge_loop():
|
||||
last_time = 0
|
||||
last_msg = ''
|
||||
logging.info('Bridge loaded')
|
||||
@@ -93,5 +91,18 @@ async def main():
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
with bot:
|
||||
bot.loop.run_until_complete(main())
|
||||
|
||||
async def main():
|
||||
global web
|
||||
await bot.start(bot_token=settings.API_TOKEN)
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
web = session
|
||||
await bridge_loop()
|
||||
finally:
|
||||
web = None
|
||||
await bot.disconnect()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import asyncio
|
||||
import importlib.util
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import aiohttp
|
||||
|
||||
|
||||
ROOT = Path(__file__).parent
|
||||
|
||||
|
||||
class _FakeBot:
|
||||
def __init__(self):
|
||||
self.started_loop = None
|
||||
self.disconnected_loop = None
|
||||
|
||||
def on(self, event):
|
||||
return lambda callback: callback
|
||||
|
||||
async def start(self, **kwargs):
|
||||
self.started_loop = asyncio.get_running_loop()
|
||||
|
||||
async def disconnect(self):
|
||||
self.disconnected_loop = asyncio.get_running_loop()
|
||||
|
||||
|
||||
def load_service():
|
||||
settings = types.ModuleType('settings')
|
||||
settings.API_ID = 1
|
||||
settings.API_HASH = 'test-hash'
|
||||
settings.API_TOKEN = 'test-token'
|
||||
settings.DYNMAP_ADDRESS = 'https://dynmap.example.test'
|
||||
settings.CUSTOM_NAMES = {}
|
||||
settings.CUSTOM_EMOJIS = {}
|
||||
settings.CHAT_ID = 1
|
||||
settings.THIS_SERVER_IP = '127.0.0.1'
|
||||
telethon = types.ModuleType('telethon')
|
||||
telethon.TelegramClient = lambda *args: _FakeBot()
|
||||
telethon.events = types.SimpleNamespace(NewMessage=lambda **kwargs: object())
|
||||
|
||||
saved = {name: sys.modules.get(name) for name in ('settings', 'telethon')}
|
||||
sys.modules['settings'] = settings
|
||||
sys.modules['telethon'] = telethon
|
||||
try:
|
||||
spec = importlib.util.spec_from_file_location('teledynmap_service_under_test', ROOT / 'teledynmap.py')
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
finally:
|
||||
for name, previous in saved.items():
|
||||
if previous is None:
|
||||
sys.modules.pop(name, None)
|
||||
else:
|
||||
sys.modules[name] = previous
|
||||
|
||||
|
||||
service = load_service()
|
||||
|
||||
|
||||
class TeleDynmapUpgradeTests(unittest.TestCase):
|
||||
def test_suite_runs_on_supported_python(self):
|
||||
self.assertEqual(sys.version_info[0], 3)
|
||||
self.assertGreaterEqual(sys.version_info[:2], (3, 11))
|
||||
|
||||
def test_service_sources_compile_under_target_python(self):
|
||||
for path in ROOT.rglob('*.py'):
|
||||
if 'env' in path.parts or '.git' in path.parts or path.name == 'tests.py':
|
||||
continue
|
||||
with self.subTest(source=str(path.relative_to(ROOT))):
|
||||
compile(path.read_bytes(), str(path), 'exec')
|
||||
|
||||
def test_aiohttp_and_telethon_apis_import(self):
|
||||
from telethon import TelegramClient, events
|
||||
self.assertTrue(aiohttp.ClientSession)
|
||||
self.assertTrue(TelegramClient)
|
||||
self.assertTrue(events.NewMessage)
|
||||
|
||||
def test_api_url_and_name_hash_are_stable(self):
|
||||
import hashlib
|
||||
self.assertEqual(service.api('/route'), 'https://dynmap.example.test/route')
|
||||
expected = int(hashlib.md5('🌎 Tanner'.encode()).hexdigest(), 16)
|
||||
self.assertEqual(service.md5('🌎 Tanner'), expected)
|
||||
self.assertGreaterEqual(expected % len(service.emojis), 0)
|
||||
self.assertLess(expected % len(service.emojis), len(service.emojis))
|
||||
|
||||
def test_telethon_and_http_session_share_loop_and_are_closed(self):
|
||||
class StopBridge(Exception):
|
||||
pass
|
||||
|
||||
async def stop_bridge():
|
||||
raise StopBridge
|
||||
|
||||
with patch.object(service, 'bridge_loop', stop_bridge):
|
||||
with self.assertRaises(StopBridge):
|
||||
asyncio.run(service.main())
|
||||
|
||||
self.assertIsNotNone(service.bot.started_loop)
|
||||
self.assertIs(service.bot.disconnected_loop, service.bot.started_loop)
|
||||
self.assertIsNone(service.web)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user