def batch_update(self, mgr: CommandsManager): mgr.batch_update_status( mgr.command_reg.calc_status_diff( # type: ignore { "echo": True, "hidden": True, } ) )
def mgr(self): class SlowCommandRegistry(CommandRegistry): def set_status(self, name: str, status: bool) -> None: time.sleep(0.1) return super().set_status(name, status) mgr = CommandsManager(command_reg=SlowCommandRegistry()) @mgr.context def name(): return "Jack" @mgr.context def haha(): return "Haha" @mgr.command def echo(payload, name): return f"{name} says {payload}" @mgr.command() @mgr.command_reg.mark_default_closed def hidden(payload, haha): return f"{haha}! You found {payload}" return mgr
def mgr(self): mgr = CommandsManager() @mgr.fallback def uhoh(content): return f'Cannot respond to "{content}"' return mgr
def mgr(self): mgr = CommandsManager() @mgr.command def hEllo(): return "hi" return mgr
def mgr(self): mgr = CommandsManager(command_case_sensitive=False) @mgr.command def hEllo(): return "hi" return mgr
def test_mark_default_close_after_register(): mgr = CommandsManager() with pytest.raises(ValueError) as e_info: @mgr.command_reg.mark_default_closed @mgr.command def echo(payload): return payload e_message = e_info.value.args[0].lower() assert "mark" in e_message
def mgr(self, data_share): mgr = CommandsManager(config=dict(enable_default_fallback=False)) @mgr.fallback(priority=10) def fallback(content, user): return user @mgr.fallback(priority=0) def final_fallback(content, **kwargs): return "Here!" return mgr
def mgr(self): mgr = CommandsManager(command_context_ignore=["message"]) @mgr.command def hello(message): return f'hello, {message["source"]}!' @mgr.command def world(): "Say world" return "world" return mgr
def mgr(self): mgr = CommandsManager() @mgr.context def name(): return "Steve" @mgr.command(groups=["hello"]) def hi(name): return f"hi, {name}!" @mgr.command(groups=["hello"]) def aloha(name): return f"aloha, {name}!" return mgr
def mgr(self, data_share): mgr = CommandsManager() data_share.open_count = [] data_share.close_count = [] @mgr.context def data(): time.sleep(0.1) data_share.open_count.append(42) yield "abc" time.sleep(0.1) data_share.close_count.append(42) @mgr.command() def post(data): return data return mgr
def mgr(self, data_share): mgr = CommandsManager(config=dict(enable_default_fallback=False)) data_share.calls = [] @mgr.fallback(priority=10) def fallback10(content): data_share.calls.append(10) return None @mgr.fallback(priority=2) def fallback2(content): data_share.calls.append(2) return "Hey!" @mgr.fallback(priority=5) def fallback5(content): data_share.calls.append(5) return None return mgr
def mgr(): mgr = CommandsManager() @mgr.context def name(): return "Jack" @mgr.context def haha(): return "Haha" @mgr.command def echo(payload, name): return f"{name} says {payload}" @mgr.command() @mgr.command_reg.mark_default_closed def hidden(payload, haha): return f"{haha}! You found {payload}" return mgr
def close_echo(self, mgr: CommandsManager): thread_execute(lambda: mgr.close("echo"), 2)
def open_hidden(self, mgr: CommandsManager): thread_execute(lambda: mgr.open("hidden"), 2)
def exec_result(self, mgr: CommandsManager): return mgr.exec("hello")
def test_simple(self, mgr: CommandsManager): assert mgr.exec("world") == "world"
def test_invoke(self, mgr: CommandsManager): assert "hEllo" in mgr.exec("hello")
def test_invoke(self, mgr: CommandsManager, cmd: str): assert mgr.exec(cmd) == "hi"
def test_forgot_kw(self, mgr: CommandsManager): with pytest.raises(TypeError): mgr.exec("nothing")
def mgr(): return CommandsManager()
def exec_post(self, mgr: CommandsManager): thread_execute(lambda: mgr.exec("post"), 2)
def test_fallback(self, mgr: CommandsManager): "This tests both two types of help and cached fallback" assert (mgr.exec("word") == DEFAULT_CONFIG["text_possible_command"] + "\n- Say world") assert mgr.exec("adflj") == DEFAULT_CONFIG["text_general_response"]
def test_custom_args(self, mgr: CommandsManager): result = mgr.exec("hello", message={"source": "source"}) assert result == "hello, source!"
def test_parameter_passed(self, mgr: CommandsManager): assert mgr.exec("nothing", user="******") == "Alex"
def test_kw_fine(self, mgr: CommandsManager): assert mgr.exec("nothing", user=None) == "Here!"
def test_fallback(self, mgr: CommandsManager): assert mgr.exec("hello") == 'Cannot respond to "hello"'
def mgr(self): mgr = CommandsManager(config=dict(enable_default_fallback=False)) return mgr
def test_exec(self, mgr: CommandsManager): assert mgr.exec("hello") is None
def test_nothing_passed(self, mgr: CommandsManager): with pytest.raises(TypeError): mgr.exec("nothing")
def close_post(self, mgr: CommandsManager): thread_execute(lambda: mgr.close("post"), 2)