Ejemplo n.º 1
0
 def RunCommand(self, timestamp, network, channel, nick, text):
     context = {
         'datetime': datetime.fromtimestamp(timestamp, tz=timezone.utc),
         'network': network,
         'channel': channel,
         'nick': nick,
         'text': text
     }
     try:
         result, display_name = interpret(self.bindings, context, text)
         return {'text': result, 'command': display_name}
     except ApplyError as e:
         return {
             'text': "Usage: {}".format(self.command_usage(e.command)),
             'command': e.command.display_name
         }
     except ScriptError as e:
         return {'text': str(e), 'command': 'error'}
     except RecursionError:
         return {
             'text': "RecursionError: Maximum recursion depth exceeded",
             'command': 'error'
         }
     except Exception as e:
         print(str(e))
         return {'text': "Internal error: " + str(e), 'command': 'error'}
Ejemplo n.º 2
0
def test_eval_contextual_propagates_context():
    script = "ctxaware"
    context = {"a": "foo"}
    bindings = [command_binding("ctxaware", lambda context: context["a"], contextual=True)]
    result, command = interpret(bindings, context, script)
    assert result == "foo"
    assert command == "ctxaware"
Ejemplo n.º 3
0
def test_eval_contextual_means_extra_arg():
    script = "ctxaware foo"
    bindings = [
        command_binding("ctxaware", lambda _context, arg: arg, contextual=True)
    ]
    result, command = interpret(new_context(bindings), script)
    assert result == "foo"
    assert command == "ctxaware"
Ejemplo n.º 4
0
def test_eval_nested():
    script = "upper [reverse foo]"
    bindings = [
        command_binding("upper", lambda s: s.upper()),
        command_binding("reverse", lambda s: s[::-1]),
    ]
    result = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "OOF"
Ejemplo n.º 5
0
def test_eval_nested():
    script = "upper [reverse foo]"
    bindings = [
        command_binding("upper", lambda s: s.upper()),
        command_binding("reverse", lambda s: s[::-1]),
    ]
    result, command = interpret(new_context(bindings), script)
    assert result == "OOF"
    assert command == "upper"
Ejemplo n.º 6
0
def test_eval_contextual_regex_command():
    script = "grooooovy"
    bindings = [
        command_binding(re.compile("^groo+vy$"),
                        lambda context: context.command_name,
                        contextual=True)
    ]
    result, command = interpret(new_context(bindings), script)
    assert result == "grooooovy"
    assert command == "/groo+vy/"
Ejemplo n.º 7
0
def test_eval_contextual_adds_command_name():
    script = "ctxaware"
    bindings = [
        command_binding("ctxaware",
                        lambda context: context.command_name,
                        contextual=True)
    ]
    result, command = interpret(new_context(bindings), script)
    assert result == "ctxaware"
    assert command == "ctxaware"
Ejemplo n.º 8
0
 def trace(self, context, mode, script):
     if mode not in ["-brief", "-full"]:
         return "Mode must be one of: -brief, -full"
     subcontext = new_context(context.bindings, context.metadata)
     try:
         interpret(subcontext, script)
     except Exception:
         pass
     color_pairs = [
         (color.LIGHT_RED, color.DARK_RED),
         (color.LIGHT_GREEN, color.DARK_GREEN),
         (color.LIGHT_BLUE, color.DARK_BLUE),
         (color.LIGHT_YELLOW, color.DARK_YELLOW),
         (color.LIGHT_MAGENTA, color.DARK_MAGENTA),
         (color.LIGHT_CYAN, color.DARK_CYAN),
     ]
     if mode == "-brief":
         return brief_trace(subcontext.trace, color_pairs)
     elif mode == "-full":
         return full_trace(subcontext.trace, color_pairs)
Ejemplo n.º 9
0
def test_eval_contextual_propagates_metadata():
    script = "ctxaware"
    metadata = {"a": "foo"}
    bindings = [
        command_binding("ctxaware",
                        lambda context: context.metadata["a"],
                        contextual=True)
    ]
    result, command = interpret(new_context(bindings, metadata), script)
    assert result == "foo"
    assert command == "ctxaware"
Ejemplo n.º 10
0
 def RunCommand(self, timestamp, network, channel, nick, text):
     context = {
         'datetime': datetime.fromtimestamp(timestamp, tz=timezone.utc),
         'network': network,
         'channel': channel,
         'nick': nick,
         'text': text
     }
     try:
         return interpret(self.bindings, context, text)
     except ApplyError as e:
         return "Usage: {}".format(self.command_usage(e.command))
     except ScriptError as e:
         return str(e)
Ejemplo n.º 11
0
 def RunCommand(self, timestamp, network, channel, nick, text):
     metadata = {
         'datetime': datetime.fromtimestamp(timestamp, tz=timezone.utc),
         'network': network,
         'channel': channel,
         'nick': nick,
         'text': text
     }
     try:
         fuse_result = self.fuse.run(metadata['datetime'], network, channel)
         if fuse_result == FuseResult.JUST_BLOWN:
             return {
                 'text':
                 f'{color.LIGHT_YELLOW}*daily fuse blown*{color.LIGHT_YELLOW}',
                 'command': 'error'
             }
         elif fuse_result == FuseResult.BLOWN:
             return {'text': '', 'command': 'error'}
         context = new_context(self.bindings, metadata)
         result, display_name = interpret(context, text)
         result = result[:10000]
         return {'text': result, 'command': display_name}
     except ApplyError as e:
         return {
             'text': "Usage: {}".format(self.command_usage(e.command)),
             'command': e.command.display_name
         }
     except ScriptError as e:
         return {'text': str(e), 'command': 'error'}
     except RecursionError:
         return {
             'text': "RecursionError: Maximum recursion depth exceeded",
             'command': 'error'
         }
     except Exception as e:
         print(str(e))
         return {'text': "Internal error: " + str(e), 'command': 'error'}
Ejemplo n.º 12
0
def test_eval_text_varargs_with_extra_whitespace():
    script = "echo   one   two   three   "
    bindings = [command_binding("echo", lambda text: text, varargs=True)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "one two three"
    assert command == "echo"
Ejemplo n.º 13
0
def test_eval_optional_arg_unfilled():
    script = "maybe foo"
    bindings = [command_binding("maybe", lambda x="bar": x)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "foo"
    assert command == "maybe"
Ejemplo n.º 14
0
def test_eval_too_many_args():
    script = "cat3 one two three four"
    bindings = [command_binding("cat3", lambda x, y, z: x + y + z)]
    with pytest.raises(ApplyError):
        interpret(bindings, EMPTY_CONTEXT, script)
Ejemplo n.º 15
0
def test_eval_multple_args():
    script = "cat3 one two three"
    bindings = [command_binding("cat3", lambda x, y, z: x + y + z)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "onetwothree"
    assert command == "cat3"
Ejemplo n.º 16
0
def test_eval_missing_command():
    script = "foo"
    bindings = []
    with pytest.raises(EvalError):
        interpret(bindings, EMPTY_CONTEXT, script)
Ejemplo n.º 17
0
def test_eval_contextual_means_extra_arg():
    script = "ctxaware foo"
    bindings = [command_binding("ctxaware", lambda _context, arg: arg, contextual=True)]
    result = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "foo"
Ejemplo n.º 18
0
 def eval_command(self, context, script):
     text, _display_name = interpret(self.bindings, context, script)
     return text
Ejemplo n.º 19
0
def test_eval_too_few_args():
    script = "cat3 one two"
    bindings = [command_binding("cat3", lambda x, y, z: x + y + z)]
    with pytest.raises(ApplyError):
        interpret(new_context(bindings), script)
Ejemplo n.º 20
0
def test_eval_python_varargs_with_no_args():
    script = "list"
    bindings = [command_binding("list", lambda *words: ",".join(words))]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == ""
    assert command == "list"
Ejemplo n.º 21
0
def test_eval_simple():
    script = "upper foo"
    bindings = [command_binding("upper", lambda s: s.upper())]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "FOO"
    assert command == "upper"
Ejemplo n.º 22
0
def test_eval_optional_arg_filled():
    script = "maybe foo"
    bindings = [command_binding("maybe", lambda x="bar": x)]
    result, command = interpret(new_context(bindings), script)
    assert result == "foo"
    assert command == "maybe"
Ejemplo n.º 23
0
def test_eval_text_varargs():
    script = "echo one two three"
    bindings = [command_binding("echo", lambda text: text, varargs=True)]
    result, command = interpret(new_context(bindings), script)
    assert result == "one two three"
    assert command == "echo"
Ejemplo n.º 24
0
 def eval_pick(self, context, *args):
     script = random.choice(args) if args else ""
     text, _display_name = interpret(context, script)
     return text
Ejemplo n.º 25
0
def test_eval_python_varargs():
    script = "list one two three"
    bindings = [command_binding("list", lambda *words: ",".join(words))]
    result = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "one,two,three"
Ejemplo n.º 26
0
def test_eval_text_varargs_with_no_args():
    script = "echo"
    bindings = [command_binding("echo", lambda text: text, varargs=True)]
    with pytest.raises(ApplyError):
        interpret(bindings, EMPTY_CONTEXT, script)
Ejemplo n.º 27
0
def test_eval_text_varargs_with_no_args_and_default():
    script = "echo"
    bindings = [command_binding("echo", lambda text="": text, varargs=True)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == ""
    assert command == "echo"
Ejemplo n.º 28
0
def test_eval_contextual_adds_command_name():
    script = "ctxaware"
    bindings = [command_binding("ctxaware", lambda context: context["command"], contextual=True)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "ctxaware"
    assert command == "ctxaware"
Ejemplo n.º 29
0
def test_eval_contextual_regex_command():
    script = "grooooovy"
    bindings = [command_binding(re.compile("^groo+vy$"), lambda context: context["command"], regex=True, contextual=True)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "grooooovy"
    assert command == "/groo+vy/"
Ejemplo n.º 30
0
def test_eval_regex_command():
    script = "grooooovy"
    bindings = [command_binding(re.compile("^groo+vy$"), lambda: "foo", regex=True)]
    result = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "foo"