Exemple #1
0
def app(message: Message):
    global DRIVER
    text = message.body["text"]
    argv = shlex.split(text.split("$app", 1)[1], posix=True)

    output_list = []
    with contextlib.redirect_stdout(StringIO()) as o:
        with contextlib.redirect_stderr(o):
            try:
                if DRIVER is None:
                    parser = Parser(prog="$app")
                    DRIVER = runtime.Driver(parser=parser)
                    DRIVER.run(argv, module=sys.modules[__name__], debug=True)
                else:
                    DRIVER._run(argv, debug=True)
            except Exit as e:
                logger.debug("exit: %r", e)

    output = o.getvalue()
    if output.strip():
        output_list.append(output)

    new_line = "\n"
    if output_list:
        message.send(f"""\
```
{new_line.join(output_list).strip(new_line)}
```""")
Exemple #2
0
def main():
    from monogusa.web.codegen import _codeobject as codeobject
    from monogusa.web.codegen._codeobject import Module
    from monogusa.cli import runtime
    from handofcats.injector import Injector

    m = Module()

    # hmm
    m.stmt("from codegen import hello, byebye")

    with m.def_("create_parser"):
        argparse = m.import_("argparse")
        driver = runtime.Driver(
            parser=m.let("parser", argparse.ArgumentParser()))
        driver.subparsers = m.let("subparsers", driver.subparsers)
        if driver.subparsers.required:
            m.stmt("{}.required = True", driver.subparsers)
        m.sep()

        scanned = runtime.scan_module()
        for fn in scanned.commands:
            sub_parser = m.let(
                "sub_parser",
                driver.subparsers.add_parser(fn.__name__,
                                             help=runtime._get_summary(fn)),
            )

            # NOTE: positional arguments are treated as component
            Injector(fn).inject(sub_parser,
                                ignore_arguments=True,
                                callback=m.stmt)
            m.stmt("{}.set_defaults(subcommand={})", sub_parser, fn.__name__)
            sub_parser.set_defaults(subcommand=fn)
            m.sep()

        m.return_(driver.parser)

    with m.def_("main"):
        parser = m.let("parser", codeobject.Symbol("create_parser")())
        args = m.let("args", parser.parse_args())
        params = m.let("params", codeobject.Symbol("vars")(args))
        action = m.let("action", params.pop("subcommand"))
        m.sep()
        m.stmt("# TODO positionals")
        m.sep()
        m.stmt("{}(**params)", action)

    with m.if_("__name__ == '__main__'"):
        m.stmt("main()")
    print(m)
Exemple #3
0
    def run(self, message: Message) -> None:
        text = message.body["text"]
        argv = runtime.parse(text, name=self.command_name)

        with runtime.handle() as output_list:
            if self.driver is None:
                self.driver = cli_runtime.Driver(parser=self.parser)
                self.driver.run(argv, module=self.module, debug=self.debug)
            else:
                self.driver._run(argv, debug=self.debug)

        if output_list:
            new_line = "\n"
            message.reply(
                f"""\
```
{new_line.join(output_list).strip(new_line)}
```""",
                in_thread=True,
            )
Exemple #4
0
def app(message: Message):
    global DRIVER
    text = message.body["text"]
    argv = runtime.parse(text, name="$app")

    with runtime.handle() as output_list:
        if DRIVER is None:
            parser = runtime.ExitExceptionParser(prog="$app")
            DRIVER = cli_runtime.Driver(parser=parser)
            DRIVER.run(argv, module=sys.modules[__name__], debug=True)
        else:
            DRIVER._run(argv, debug=True)

    new_line = "\n"
    if output_list:
        message.reply(
            f"""\
```
{new_line.join(output_list).strip(new_line)}
```""",
            in_thread=True,
        )
Exemple #5
0
from monogusa.web.codegen import _codeobject as codeobject
from monogusa.web.codegen._codeobject import Module
from monogusa.cli import runtime


m = Module()


class pool:
    pass


argparse = m.import_("argparse")
pool.parser = argparse.ArgumentParser()
driver = runtime.Driver(parser=m.let("parser", argparse.ArgumentParser()))