Skip to content

Commit

Permalink
fix bug in run_until(), restrict event handling on init subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Mar 7, 2024
1 parent 2b14279 commit a22ff4b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
27 changes: 16 additions & 11 deletions deltabot_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
11 changes: 2 additions & 9 deletions deltabot_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down

0 comments on commit a22ff4b

Please sign in to comment.