diff --git a/deltabot_cli/cli.py b/deltabot_cli/cli.py index 81c3af4..6e88b18 100644 --- a/deltabot_cli/cli.py +++ b/deltabot_cli/cli.py @@ -13,10 +13,10 @@ from appdirs import user_config_dir from rich.logging import RichHandler -from ._utils import AttrDict, ConfigProgressBar, parse_docstring +from ._utils import ConfigProgressBar, parse_docstring from .client import Bot from .const import EventType -from .events import EventFilter, HookCollection, HookDecorator, RawEvent +from .events import EventFilter, HookCollection, HookDecorator from .rpc import JsonRpcError, Rpc CliEventHook = Callable[[Bot, Namespace], None] @@ -201,14 +201,9 @@ def start(self) -> None: self._parser.parse_args(["-h"]) -def _init_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None: +def _init_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None: # noqa: C901 """initialize the account""" - def on_progress(bot: Bot, _accid: int, event: AttrDict) -> None: - if event.comment: - bot.logger.info(event.comment) - pbar.set_progress(event.progress) - def configure() -> None: try: bot.configure(accid, email=args.addr, password=args.password) @@ -224,11 +219,21 @@ def configure() -> None: accid = cli.get_or_create_account(bot.rpc, args.addr) bot.logger.info("Starting configuration process...") - pbar = ConfigProgressBar() - bot.add_hook(on_progress, RawEvent(EventType.CONFIGURE_PROGRESS)) task = Thread(target=configure) task.start() - bot._run_until(lambda _: pbar.progress in (-1, pbar.total)) # noqa: access to protected field + pbar = ConfigProgressBar() + while True: + raw_event = bot.rpc.get_next_event() + accid = raw_event.context_id + event = raw_event.event + if event.kind == EventType.CONFIGURE_PROGRESS: + if event.comment: + bot.logger.info(event.comment) + pbar.set_progress(event.progress) + elif event.kind in (EventType.INFO, EventType.WARNING, EventType.ERROR): + bot._on_event(accid, event) # noqa: access to protected field + if pbar.progress in (-1, pbar.total): + break task.join() pbar.close() if pbar.progress == -1: diff --git a/deltabot_cli/client.py b/deltabot_cli/client.py index 0769fa3..0d7eab2 100644 --- a/deltabot_cli/client.py +++ b/deltabot_cli/client.py @@ -59,7 +59,6 @@ def configure(self, accid: int, email: str, password: str, **kwargs) -> None: if kwargs: self.rpc.batch_set_config(accid, kwargs) self.rpc.configure(accid) - self.logger.debug(f"Account {accid} configured") def run_forever(self, accid: int = 0) -> None: """Process events forever. @@ -85,19 +84,13 @@ def run_until(self, func: Callable[[AttrDict], bool], accid: int = 0) -> AttrDic if self.rpc.is_configured(acc_id): self._process_messages(acc_id) # Process old messages. - def wrapper(event): - if event.kind == EventType.INCOMING_MSG: - self._process_messages(accid) - return func(event) - - return self._run_until(wrapper) - - def _run_until(self, func: Callable[[AttrDict], bool]) -> AttrDict: while True: raw_event = self.rpc.get_next_event() accid = raw_event.context_id event = raw_event.event self._on_event(accid, event) + if event.kind == EventType.INCOMING_MSG: + self._process_messages(accid) if func(event): return event