def do_ret(args): if not s.fn_cur.flow_n: s.fn_cur.flow_ret_t = True if len(args): res_ = emit.emit(s.fn_cur, e.EMIT_RET, args[0]) else: res_ = emit.emit(s.fn_cur, e.EMIT_RET, None) return res_
def emit(*, title: str = "egoist", version: str = "0.0.0") -> None: from emit import emit, get_walker from dictknife import loading w = get_walker([Article]) root = emit(w, app.routes, title=title, version=version) loading.dumpfile(root)
def do_else(args): if (not s.fn_cur.flow_n) or (s.fn_cur.flow_cur[s.fn_cur.flow_n - 1][0] != e.FLOW_IF): out.error("outside flow control") return False res_ = emit.emit(s.fn_cur, e.EMIT_ELSE, None) return res_
def do_end(args): if s.fn_cur.flow_n: out.error("unclosed flow control") return False res_ = emit.emit(s.fn_cur, e.EMIT_END, "") s.fn_cur = None return res_
def do_wend(args): if (not s.fn_cur.flow_n) or (s.fn_cur.flow_cur[s.fn_cur.flow_n - 1][0] != e.FLOW_WHILE): out.error("outside flow control") return False res_ = emit.emit(s.fn_cur, e.EMIT_WEND, None) s.fn_cur.flow_cur.pop() s.fn_cur.flow_n -= 1 return res_
def do_def(args): if s.fn_cur: out.error("nested function") return False _fn = fn.Fn(args[0], args[1:], emit.s.long_len) if not _fn: return False s.fn_cur = _fn res_ = emit.emit(s.fn_cur, e.EMIT_DEF, args[0]) return res_
def do_set(args): res_ = emit.emit(s.fn_cur, e.EMIT_SET, args[0], val2=args[1]) return res_
def do_res(args): res_ = emit.emit(s.fn_cur, e.EMIT_RES, args[0]) return res_
@api.post("/pets", metadata={"tags": ["pets"]}) def createPets() -> tx.Annotated[None, DefaultStatus(201)]: """create a pets""" c = api.get_current_context() c.return_.description = "Null response" @api.get("/pets/{petId}", metadata={"tags": ["pets"]}) def showPetById() -> Pet: """Info for a specific pet""" c = api.get_current_context() c.petId.description = "The id of the pet to retrieve" c.return_.description = "Expected response to a valid request" # TODO: # - servers # - responses headers # - todo type as NewType # - todo extra headers d = emit( api, title="Swagger Petstore", version="1.0.0", default_error_response=ErrorResponse(Error), ) loading.dumpfile(d, format="yaml")
from __future__ import annotations import typing as t from emit import emit, metadata, field from prestring.go.gofmt import gofmt class Person: name: str age: int memo: Memo = field(metadata=metadata(inline=True)) class Memo: data: t.Dict[str, t.Any] print(gofmt(emit([Person]), always=False))
@api.post("/pets", metadata={"tags": ["pets"]}) def createPets() -> tx.Annotated[None, DefaultStatus(201)]: """Create a pet""" c = api.get_current_context() c.return_.description = "Null response" @api.get("/pets/{petId}", metadata={"tags": ["pets"]}) def showPetById() -> Pet: """Info for a specific pet""" c = api.get_current_context() c.petId.description = "The id of the pet to retrieve" c.return_.description = "Expected response to a valid request" # TODO: # - v2 support if __name__ == "__main__": d = emit( api, title="Swagger Petstore", version="1.0.0", license="MIT", servers=["http://petstore.swagger.io/v1"], default_error_response=ErrorResponse(Error), ) loading.dumpfile(d, format="yaml")
import typing as t from emit import emit from app import App from dictknife import loading app = App() class Article: title: str content: str @app.post("/api/articles", metadata={"tags": ["xxx"]}) def create_article(article: Article) -> t.List[Article]: pass title = "egoist" version = "0.0.0" root = emit(list(app.routes), title=title, version=version) loading.dumpfile(root)
# https://eli.thegreenplace.net/2018/go-and-algebraic-data-types/ from __future__ import annotations import typing as t from prestring.go.gofmt import gofmt from emit import emit, field, metadata class Empty: pass class Leaf: value: int class Node: left: Tree right: Tree Tree = t.Union[Empty, Leaf, Node] Tree.__name__ = "Tree" print(gofmt(emit([Tree]), always=False))
def do_multo(args): res_ = emit.emit(s.fn_cur, e.EMIT_MULTO, args[0], val2=args[1]) return res_
def do_addto(args): res_ = emit.emit(s.fn_cur, e.EMIT_ADDTO, args[0], val2=args[1]) return res_
def do_if(args): s.fn_cur.flow_cur += [[e.FLOW_IF, s.fn_cur.flow_if_n]] s.fn_cur.flow_if_n += 1 s.fn_cur.flow_n += 1 res_ = emit.emit(s.fn_cur, e.EMIT_IF, args[0]) return res_
def do_call(args): res_ = emit.emit(s.fn_cur, e.EMIT_CALL, args[0], val2=args[1:]) return res_
from __future__ import annotations import typing as t from emit import emit class Team: name: str members: t.List[User] class User: name: str print(emit([Team]))
def do_subfrom(args): res_ = emit.emit(s.fn_cur, e.EMIT_SUBFROM, args[0], val2=args[1]) return res_
def do_while(args): s.fn_cur.flow_cur += [[e.FLOW_WHILE, s.fn_cur.flow_while_n]] s.fn_cur.flow_while_n += 1 s.fn_cur.flow_n += 1 res_ = emit.emit(s.fn_cur, e.EMIT_WHILE, args[0]) return res_
def do_divfrom(args): res_ = emit.emit(s.fn_cur, e.EMIT_DIVFROM, args[0], val2=args[1]) return res_
def do_add(args): res_ = emit.emit(s.fn_cur, e.EMIT_ADD, args[0]) return res_
from __future__ import annotations import typing as t from emit import emit from prestring.go.gofmt import gofmt class Atom: type_: str class Composite: type_: str args: t.List[Node] Node = t.Union[Atom, Composite] Node.__name__ = "Node" print(gofmt(emit([Node]), always=False))
def do_sub(args): res_ = emit.emit(s.fn_cur, e.EMIT_SUB, args[0]) return res_
Pets = t.List[Pet] class Error: code: int # int32 message: str api = API() # paths @api.get("/pets", metadata={"tags": ["pets"]}) def listPets() -> t.List[Pet]: """list all pets""" # parameters: # - name: limit # in: query # description: How many items to return at one time (max 100) # required: false # schema: # type: integer # format: int32 pass # todo: default error d = emit(api, title="hello", version="0.0.0") loading.dumpfile(d, format="yaml")
def do_mul(args): res_ = emit.emit(s.fn_cur, e.EMIT_MUL, args[0]) return res_
from emit import emit class Person: name: str age: int print(emit([Person]))
def do_div(args): res_ = emit.emit(s.fn_cur, e.EMIT_DIV, args[0]) return res_
"""list all pets""" c = api.get_current_context() c.limit.description = "How many items to return at one time (max 100)" c.limit.required = False c.limit.schema = {"type": "integer", "format": "int32"} @api.post("/pets", metadata={"tags": ["pets"]}) def createPets() -> tx.Annotated[None, DefaultStatus(201)]: """create a pets""" c = api.get_current_context() c.return_.description = "Null response" @api.get("/pets/{petId}", metadata={"tags": ["pets"]}) def showPetById() -> Pet: """Info for a specific pet""" c = api.get_current_context() c.petId.description = "The id of the pet to retrieve" # TODO: # - servers # - responses headers # - default error handling # - todo type as NewType # - todo extra headers d = emit(api, title="Swagger Petstore", version="1.0.0") loading.dumpfile(d, format="yaml")
from emit import emit from prestring.go.gofmt import gofmt class A: x: int y: int class B(A): y: str z: str print(gofmt(emit([B]), always=False))