Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
[MINOR]: FastAPI is served checker
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Sep 5, 2024
1 parent d834a91 commit c6a7940
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
38 changes: 24 additions & 14 deletions jaclang_jaseci/jaseci/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ class FastAPI:
"""FastAPI Handler."""

__app__ = None
__is_imported__: bool | None = None
__is_served__: bool | None = None

@staticmethod
def is_imported() -> bool:
"""Check if fastapi is used."""
def serve() -> None:
"""Tag Fastapi as served."""
from jaclang.plugin.feature import JacFeature as Jac
from jaclang.runtimelib.machine import JacMachine

from ..core.architype import (
EdgeArchitype,
Expand All @@ -31,17 +30,28 @@ def is_imported() -> bool:
WalkerArchitype,
)

if not isinstance(FastAPI.__is_imported__, bool):
FastAPI.__is_served__ = True

Jac.RootType = Root # type: ignore[assignment]
Jac.Obj = ObjectArchitype # type: ignore[assignment]
Jac.Node = NodeArchitype # type: ignore[assignment]
Jac.Edge = EdgeArchitype # type: ignore[assignment]
Jac.Walker = WalkerArchitype # type: ignore[assignment]

@staticmethod
def is_served() -> bool:
"""Check if FastAPI is already served."""
if FastAPI.__is_served__ is None:
from jaclang.runtimelib.machine import JacMachine

main = JacMachine.get().loaded_modules.get("__main__")
FastAPI.__is_imported__ = getattr(main, "FastAPI", None) is FastAPI
if FastAPI.__is_imported__:
Jac.RootType = Root # type: ignore[assignment]
Jac.Obj = ObjectArchitype # type: ignore[assignment]
Jac.Node = NodeArchitype # type: ignore[assignment]
Jac.Edge = EdgeArchitype # type: ignore[assignment]
Jac.Walker = WalkerArchitype # type: ignore[assignment]

return FastAPI.__is_imported__
if getattr(main, "FastAPI", None) is FastAPI:
FastAPI.serve()
return True
else:
FastAPI.__is_served__ = False

return FastAPI.__is_served__

@classmethod
def get(cls) -> _FaststAPI:
Expand Down
1 change: 1 addition & 0 deletions jaclang_jaseci/plugin/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def serve(filename: str, host: str = "0.0.0.0", port: int = 8000) -> None:
base = base if base else "./"
mod = mod[:-4]

FastAPI.serve()
jctx = ExecutionContext.create()

if filename.endswith(".jac"):
Expand Down
20 changes: 10 additions & 10 deletions jaclang_jaseci/plugin/jaseci.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class JacPlugin:
@hookimpl
def get_context() -> ExecutionContext:
"""Get current execution context."""
if FastAPI.is_imported():
if FastAPI.is_served():
return JaseciContext.get()
return JacFeatureDefaults.get_context()

Expand All @@ -259,7 +259,7 @@ def make_architype(
on_exit: list[DSFunc],
) -> Type[Architype]:
"""Create a new architype."""
if FastAPI.is_imported():
if FastAPI.is_served():
for i in on_entry + on_exit:
i.resolve(cls)
if not hasattr(cls, "_jac_entry_funcs_") or not hasattr(
Expand Down Expand Up @@ -306,7 +306,7 @@ def make_obj(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a new architype."""
if FastAPI.is_imported():
if FastAPI.is_served():

def decorator(cls: Type[Architype]) -> Type[Architype]:
"""Decorate class."""
Expand All @@ -327,7 +327,7 @@ def make_node(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a obj architype."""
if FastAPI.is_imported():
if FastAPI.is_served():

def decorator(cls: Type[Architype]) -> Type[Architype]:
"""Decorate class."""
Expand All @@ -345,7 +345,7 @@ def make_edge(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a edge architype."""
if FastAPI.is_imported():
if FastAPI.is_served():

def decorator(cls: Type[Architype]) -> Type[Architype]:
"""Decorate class."""
Expand All @@ -363,7 +363,7 @@ def make_walker(
on_entry: list[DSFunc], on_exit: list[DSFunc]
) -> Callable[[type], type]:
"""Create a walker architype."""
if FastAPI.is_imported():
if FastAPI.is_served():

def decorator(cls: Type[Architype]) -> Type[Architype]:
"""Decorate class."""
Expand All @@ -383,7 +383,7 @@ def decorator(cls: Type[Architype]) -> Type[Architype]:
@hookimpl
def report(expr: Any) -> None: # noqa:ANN401
"""Jac's report stmt feature."""
if FastAPI.is_imported():
if FastAPI.is_served():
JaseciContext.get().reports.append(expr)
return
JacFeatureDefaults.report(expr=expr)
Expand All @@ -392,15 +392,15 @@ def report(expr: Any) -> None: # noqa:ANN401
@hookimpl
def get_root() -> Root:
"""Jac's assign comprehension feature."""
if FastAPI.is_imported():
if FastAPI.is_served():
return JaseciContext.get_root()
return JacFeatureDefaults.get_root() # type:ignore[return-value]

@staticmethod
@hookimpl
def get_root_type() -> Type[Root]:
"""Jac's root getter."""
if FastAPI.is_imported():
if FastAPI.is_served():
return Root
return JacFeatureDefaults.get_root_type() # type:ignore[return-value]

Expand All @@ -412,7 +412,7 @@ def build_edge(
conn_assign: tuple[tuple, tuple] | None,
) -> Callable[[NodeAnchor, NodeAnchor], EdgeArchitype]:
"""Jac's root getter."""
if FastAPI.is_imported():
if FastAPI.is_served():
conn_type = conn_type if conn_type else GenericEdge

def builder(source: NodeAnchor, target: NodeAnchor) -> EdgeArchitype:
Expand Down

0 comments on commit c6a7940

Please sign in to comment.