From 179c84b45a8ea27c5e5aba327bcf3ab10b392c5e Mon Sep 17 00:00:00 2001 From: Romain J Date: Mon, 19 Oct 2020 22:17:19 +0200 Subject: [PATCH] feat(instances): done -L active since --- tuxbot/__run__.py | 2 +- tuxbot/core/bot.py | 27 +++++++++++++++++++++++++-- tuxbot/core/config.py | 11 +++++++++-- tuxbot/setup.py | 22 +++++++++++++--------- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/tuxbot/__run__.py b/tuxbot/__run__.py index 0741abd..0a74120 100644 --- a/tuxbot/__run__.py +++ b/tuxbot/__run__.py @@ -47,7 +47,7 @@ def list_instances() -> NoReturn: console.print() columns = Columns(expand=True, padding=2, align="center") - for instance, details in app_config.instances.items(): + for instance, details in app_config.Instances.items(): active = details["active"] last_run = ( humanize.naturaltime( diff --git a/tuxbot/core/bot.py b/tuxbot/core/bot.py index 596e42f..f97d6b4 100644 --- a/tuxbot/core/bot.py +++ b/tuxbot/core/bot.py @@ -15,8 +15,14 @@ from rich.traceback import install from tuxbot import version_info -from .config import Config, ConfigFile, search_for -from .data_manager import logs_data_path, data_path +from .config import ( + Config, + ConfigFile, + search_for, + AppConfig, + set_for_key, +) +from .data_manager import logs_data_path, data_path, config_dir from . import __version__, ExitCodes from . import exceptions @@ -105,6 +111,15 @@ class Tux(commands.AutoShardedBot): async def on_ready(self): self.uptime = datetime.datetime.now() + app_config = ConfigFile(config_dir / "config.yaml", AppConfig).config + set_for_key( + app_config.Instances, + self.instance_name, + AppConfig.Instance, + active=True, + last_run=datetime.datetime.timestamp(self.uptime), + ) + self._progress.get("main").stop_task( self._progress.get("tasks")["connecting"] ) @@ -234,6 +249,14 @@ class Tux(commands.AutoShardedBot): Todo: add postgresql logout here """ + app_config = ConfigFile(config_dir / "config.yaml", AppConfig).config + set_for_key( + app_config.Instances, + self.instance_name, + AppConfig.Instance, + active=False, + ) + for task in self._progress.get("tasks").keys(): self._progress.get("main").log("Shutting down", task) diff --git a/tuxbot/core/config.py b/tuxbot/core/config.py index 81f260e..6d8a13e 100644 --- a/tuxbot/core/config.py +++ b/tuxbot/core/config.py @@ -9,7 +9,14 @@ from structured_config import ( ) -__all__ = ["Config", "ConfigFile", "search_for", "set_for_key", "set_for_key"] +__all__ = [ + "Config", + "ConfigFile", + "AppConfig", + "search_for", + "set_for_key", + "set_for", +] log = logging.getLogger("tuxbot.core.config") @@ -59,7 +66,7 @@ class AppConfig(Structure): active: bool = BoolField(False) last_run: int = IntField(0) - instances: Dict[str, Instance] = {} + Instances: Dict[str, Instance] = {} # ============================================================================= diff --git a/tuxbot/setup.py b/tuxbot/setup.py index 94cd312..09570b0 100644 --- a/tuxbot/setup.py +++ b/tuxbot/setup.py @@ -13,7 +13,7 @@ from rich.rule import Rule from rich.style import Style from rich.traceback import install -from tuxbot.core.config import set_for +from tuxbot.core.config import set_for, set_for_key from tuxbot.logging import formatter from tuxbot.core.data_manager import config_dir, app_dir, cogs_data_path from tuxbot.core import config @@ -29,12 +29,14 @@ except PermissionError: ) sys.exit(1) -app_config = config.ConfigFile(config_dir / "config.yaml", config.AppConfig) +app_config = config.ConfigFile( + config_dir / "config.yaml", config.AppConfig +).config -if not app_config.config.instances: +if not app_config.Instances: instances_list = [] else: - instances_list = list(app_config.config.instances.keys()) + instances_list = list(app_config.Instances.keys()) def get_name() -> str: @@ -339,11 +341,13 @@ def basic_setup() -> NoReturn: console.print("Abandon...") sys.exit(0) - instance = config.AppConfig.Instance() - instance.path = str(data_dir.resolve()) - instance.active = False - - app_config.config.instances[name] = instance + set_for_key( + app_config.Instances, + name, + config.AppConfig.Instance, + path=str(data_dir.resolve()), + active=False, + ) console.print("\n" * 4)