Example #1
0
def use_gencode():
    from codeobject import Module

    m = Module()

    argparse = m.import_("argparse")
    with m.def_("create_parser"):
        parser = m.let("parser", argparse.ArgumentParser(prog="app.py"))
        subparsers = m.let(
            "subparsers",
            parser.add_subparsers(title="subcommands",
                                  required=True,
                                  dest="subcommand"),
        )
        m.sep()
        for fn in [hello, byebye]:
            m.stmt(f"# for {fn.__name__}")
            sub_parser = m.let(
                "sub_parser",
                subparsers.add_parser(fn.__name__,
                                      help=UnRepr(f"{fn.__name__}.__doc__")),
            )
            Injector(fn).inject(sub_parser, callback=m.stmt)
            m.stmt(sub_parser.set_defaults(subcommand=fn))
            m.sep()
        m.return_(parser)

    print(m)
Example #2
0
def use_gencode():
    from codeobject import Module, codeobject

    @codeobject
    def ArgumentParser(m: Module, name: str) -> Module:
        pass

    m = Module()
    parser = m.let("parser", ArgumentParser(prog="app.py"))
    Injector(hello).inject(parser, callback=m.stmt)
    print(m)
Example #3
0
def use_gencode():
    from codeobject import Module, codeobject

    @codeobject
    def ArgumentParser(m: Module, name: str) -> Module:
        pass

    m = Module()
    parser = m.let("parser", ArgumentParser(prog="app.py"))
    bind_parser(parser, rec=m.stmt)
    print(m)
Example #4
0
def main():

    m = Module(import_unique=True)
    m.toplevel = m.submodule()
    m.sep()

    m.toplevel.import_("typing", as_="t")
    m.toplevel.from_("pydantic", "BaseModel")
    m.toplevel.from_("fastapi", "APIRouter", "Depends")

    m.stmt("router = APIRouter()")
    m.sep()

    for fn in [hello, byebye]:
        spec = fnspec.fnspec(fn)

        m.toplevel.from_("monogusa.web", "runtime")
        if fn.__module__ != "__main__":
            m.toplevel.import_(fn.__module__)

        InputSchema = None
        if len(spec.parameters) > 0:
            InputSchema = codeobject.codeobject(partial(schema_code_from_spec,
                                                        spec=spec),
                                                name=pascalcase(fn.__name__))
            m.stmt(InputSchema)

        view_callable = codeobject.codeobject(
            partial(view_code_from_spec, spec=spec, InputSchema=InputSchema),
            name=spec.name,
        )
        m.stmt(view_callable)

    with m.def_("main", return_type=None):
        m.from_("monogusa.web", "cli")
        m.from_("fastapi", "FastAPI")
        m.stmt("app = FastAPI()")
        m.stmt("app.include_router(router)")
        m.stmt("cli.run(app)")

    print(m)
Example #5
0
    with m.def_(name, return_type=Hello):
        p = m.let("p", Hello(name="foo", age=20))
        m.stmt(p.say("hello"))  # type: ignore
        m.return_(p)
    return m


@codeobject.codeobject
def hello_view(m: Module, name: str) -> Module:
    m.toplevel.from_("monogusa.web", "runtime")  # todo: toplevel?

    m.stmt('@router.post( "/{}", response_model=runtime.CommandOutput)',
           hello.__name__)
    with m.def_(name, f"input: {hello.__name__}", return_type=Hello):
        if hello.__doc__ is not None:
            m.docstring(hello.__doc__)
        with m.with_("runtime.handle() as s"):
            m.stmt("{}.{}(**input.dict())", "cli", hello.__name__,
                   hello.__name__)
            m.stmt("return s.dict()")
    return m


m = Module()
m.toplevel = m.submodule()
m.sep()
m.stmt(Hello)
m.stmt(hello_with_hello)
m.stmt(hello_view)
print(m)