From a1a914dc874177f922057e4595b1b10e016a64fd Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Tue, 6 Nov 2018 17:42:27 -0300 Subject: [PATCH] add manage.py --- litecord/pubsub/lazy_guild.py | 2 ++ manage.py | 12 ++++++++++++ manage/__init__.py | 0 manage/main.py | 30 ++++++++++++++++++++++++++++++ run.py | 21 +++++++++++++++------ 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100755 manage.py create mode 100644 manage/__init__.py create mode 100644 manage/main.py diff --git a/litecord/pubsub/lazy_guild.py b/litecord/pubsub/lazy_guild.py index 2202234..ba19276 100644 --- a/litecord/pubsub/lazy_guild.py +++ b/litecord/pubsub/lazy_guild.py @@ -21,6 +21,7 @@ Presence = Dict[str, Any] @dataclass class GroupInfo: + """Store information about a specific group.""" gid: GroupID name: str position: int @@ -29,6 +30,7 @@ class GroupInfo: @dataclass class MemberList: + """Total information on the guild's member list.""" groups: List[GroupInfo] = None group_info: Dict[GroupID, GroupInfo] = None data: Dict[GroupID, Presence] = None diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..a65b9d6 --- /dev/null +++ b/manage.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import logging +import sys + +from manage.main import main + +import config + +logging.basicConfig(level=logging.DEBUG) + +if __name__ == '__main__': + sys.exit(main(config)) diff --git a/manage/__init__.py b/manage/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/manage/main.py b/manage/main.py new file mode 100644 index 0000000..b05d6db --- /dev/null +++ b/manage/main.py @@ -0,0 +1,30 @@ +import asyncio +from dataclasses import dataclass + + +from run import init_app_managers, init_app_db + + +@dataclass +class FakeApp: + """Fake app instance.""" + config: dict + db = None + loop: asyncio.BaseEventLoop = None + ratelimiter = None + state_manager = None + storage = None + dispatcher = None + presence = None + + +def main(config): + """Start the script""" + loop = asyncio.get_event_loop() + cfg = getattr(config, config.MODE) + app = FakeApp(cfg.__dict__) + + loop.run_until_complete(init_app_db(app)) + init_app_managers(app) + + print(app) diff --git a/run.py b/run.py index 48a630c..c1af61f 100644 --- a/run.py +++ b/run.py @@ -143,16 +143,14 @@ async def app_set_ratelimit_headers(resp): return resp -@app.before_serving -async def app_before_serving(): - log.info('opening db') +async def init_app_db(app): + """Connect to databases""" app.db = await asyncpg.create_pool(**app.config['POSTGRES']) - g.app = app +def init_app_managers(app): + """Initialize singleton classes.""" app.loop = asyncio.get_event_loop() - g.loop = asyncio.get_event_loop() - app.ratelimiter = RatelimitManager() app.state_manager = StateManager() app.storage = Storage(app.db) @@ -162,6 +160,17 @@ async def app_before_serving(): app.state_manager, app.dispatcher) app.storage.presence = app.presence + +@app.before_serving +async def app_before_serving(): + log.info('opening db') + await init_app_db(app) + + g.app = app + g.loop = asyncio.get_event_loop() + + init_app_managers(app) + # start the websocket, etc host, port = app.config['WS_HOST'], app.config['WS_PORT'] log.info(f'starting websocket at {host} {port}')