Пример #1
0
def trace(context, mode, script):
    subcontext = new_context(context.commands, metadata=context.metadata)
    try:
        interpret(subcontext, script)
    except Exception:
        pass
    return render_trace(subcontext.trace, mode)
Пример #2
0
 def exec_command(context: Context, *args: str) -> str:
     if len(command.params) != len(args):
         raise ScriptError(
             f"{command.name} takes {len(command.params)} arguments, got {len(args)}"
         )
     new_env = dict(zip(command.params, args))
     subcontext = context._replace(environment=new_env)
     result, _display_name = interpret(subcontext, command.script)
     return result
Пример #3
0
def let(context, *args):
    if len(args) % 2 != 1:
        raise ScriptError("Let accepts an odd number of arguments (name-value pairs and a body)")
    new_env = dict(context.environment)
    for variable, value in _pairs(args[:-1]):
        new_env[variable] = value
    subcontext = context._replace(environment=new_env)
    result, _display_name = interpret(subcontext, args[-1])
    return result
Пример #4
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:
         context = new_context(self.commands, metadata=metadata)
         fuse_result = self.fuse.run(metadata['datetime'], network, channel)
         if fuse_result == FuseResult.JUST_BLOWN:
             result = {
                 'text':
                 f'{color.LIGHT_YELLOW}*daily fuse blown*{color.LIGHT_YELLOW}',
                 'command': 'error'
             }
         elif fuse_result == FuseResult.BLOWN:
             result = {'text': '', 'command': 'error'}
         else:
             result_text, display_name = interpret(context, text)
             result_text = result_text[:10000]
             result = {'text': result_text, 'command': display_name}
     except ApplyError as e:
         result = {
             'text': "Usage: {}".format(command_usage(e.command)),
             'command': e.command.display_name
         }
     except ScriptError as e:
         result = {'text': f"Error: {e}", 'command': 'error'}
     except RecursionError:
         result = {
             'text': "RecursionError: Maximum recursion depth exceeded",
             'command': 'error'
         }
     except Exception as e:
         print(traceback.format_exc())
         result = {'text': "Internal error: " + repr(e), 'command': 'error'}
     self.last_contexts[(network, channel)] = context
     return result
Пример #5
0
 def exec_command(context: Context) -> str:
     script = "echo " + template
     subcontext = context._replace(environment={})
     result, _display_name = interpret(subcontext, script)
     return result
Пример #6
0
def repeat(context, count, script, delimiter="   "):
    texts = [interpret(context, script)[0] for _ in range(int(count))]
    return delimiter.join(texts)
Пример #7
0
def eval_pick(context, *args):
    script = random.choice(args) if args else ""
    text, _display_name = interpret(context, script)
    return text
Пример #8
0
def eval_command(context, script):
    text, _display_name = interpret(context, script)
    return text
Пример #9
0
def test_exec_alias(commands, populated_alias_cmds):
    context = new_context(commands, command_name="testalias")
    script = "testalias"
    result, _ = interpret(context, script)
    assert result == "testtest"