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

Commit c4ed925

Browse files
committed
[MINOR]: FastAPI is served checker
1 parent d834a91 commit c4ed925

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

jaclang_jaseci/jaseci/__init__.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ class FastAPI:
1515
"""FastAPI Handler."""
1616

1717
__app__ = None
18-
__is_imported__: bool | None = None
18+
__is_served__: bool | None = None
1919

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

2625
from ..core.architype import (
2726
EdgeArchitype,
@@ -31,17 +30,28 @@ def is_imported() -> bool:
3130
WalkerArchitype,
3231
)
3332

34-
if not isinstance(FastAPI.__is_imported__, bool):
33+
FastAPI.__is_served__ = True
34+
35+
Jac.RootType = Root # type: ignore[assignment]
36+
Jac.Obj = ObjectArchitype # type: ignore[assignment]
37+
Jac.Node = NodeArchitype # type: ignore[assignment]
38+
Jac.Edge = EdgeArchitype # type: ignore[assignment]
39+
Jac.Walker = WalkerArchitype # type: ignore[assignment]
40+
41+
@staticmethod
42+
def is_served() -> bool:
43+
"""Check if FastAPI is already served."""
44+
if FastAPI.__is_served__ is None:
45+
from jaclang.runtimelib.machine import JacMachine
46+
3547
main = JacMachine.get().loaded_modules.get("__main__")
36-
FastAPI.__is_imported__ = getattr(main, "FastAPI", None) is FastAPI
37-
if FastAPI.__is_imported__:
38-
Jac.RootType = Root # type: ignore[assignment]
39-
Jac.Obj = ObjectArchitype # type: ignore[assignment]
40-
Jac.Node = NodeArchitype # type: ignore[assignment]
41-
Jac.Edge = EdgeArchitype # type: ignore[assignment]
42-
Jac.Walker = WalkerArchitype # type: ignore[assignment]
43-
44-
return FastAPI.__is_imported__
48+
if getattr(main, "FastAPI", None) is FastAPI:
49+
FastAPI.serve()
50+
return True
51+
else:
52+
FastAPI.__is_served__ = False
53+
54+
return FastAPI.__is_served__
4555

4656
@classmethod
4757
def get(cls) -> _FaststAPI:

jaclang_jaseci/plugin/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def serve(filename: str, host: str = "0.0.0.0", port: int = 8000) -> None:
2727
base = base if base else "./"
2828
mod = mod[:-4]
2929

30+
FastAPI.serve()
3031
jctx = ExecutionContext.create()
3132

3233
if filename.endswith(".jac"):

jaclang_jaseci/plugin/jaseci.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class JacPlugin:
246246
@hookimpl
247247
def get_context() -> ExecutionContext:
248248
"""Get current execution context."""
249-
if FastAPI.is_imported():
249+
if FastAPI.is_served():
250250
return JaseciContext.get()
251251
return JacFeatureDefaults.get_context()
252252

@@ -259,7 +259,7 @@ def make_architype(
259259
on_exit: list[DSFunc],
260260
) -> Type[Architype]:
261261
"""Create a new architype."""
262-
if FastAPI.is_imported():
262+
if FastAPI.is_served():
263263
for i in on_entry + on_exit:
264264
i.resolve(cls)
265265
if not hasattr(cls, "_jac_entry_funcs_") or not hasattr(
@@ -306,7 +306,7 @@ def make_obj(
306306
on_entry: list[DSFunc], on_exit: list[DSFunc]
307307
) -> Callable[[type], type]:
308308
"""Create a new architype."""
309-
if FastAPI.is_imported():
309+
if FastAPI.is_served():
310310

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

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

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

368368
def decorator(cls: Type[Architype]) -> Type[Architype]:
369369
"""Decorate class."""
@@ -383,7 +383,7 @@ def decorator(cls: Type[Architype]) -> Type[Architype]:
383383
@hookimpl
384384
def report(expr: Any) -> None: # noqa:ANN401
385385
"""Jac's report stmt feature."""
386-
if FastAPI.is_imported():
386+
if FastAPI.is_served():
387387
JaseciContext.get().reports.append(expr)
388388
return
389389
JacFeatureDefaults.report(expr=expr)
@@ -392,15 +392,15 @@ def report(expr: Any) -> None: # noqa:ANN401
392392
@hookimpl
393393
def get_root() -> Root:
394394
"""Jac's assign comprehension feature."""
395-
if FastAPI.is_imported():
395+
if FastAPI.is_served():
396396
return JaseciContext.get_root()
397397
return JacFeatureDefaults.get_root() # type:ignore[return-value]
398398

399399
@staticmethod
400400
@hookimpl
401401
def get_root_type() -> Type[Root]:
402402
"""Jac's root getter."""
403-
if FastAPI.is_imported():
403+
if FastAPI.is_served():
404404
return Root
405405
return JacFeatureDefaults.get_root_type() # type:ignore[return-value]
406406

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

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

jaclang_jaseci/tests/test_simple_graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SimpleGraphTest(IsolatedAsyncioTestCase):
1616

1717
async def asyncSetUp(self) -> None:
1818
"""Reset DB and wait for server."""
19-
self.host = "http://0.0.0.0:8001"
19+
self.host = "http://0.0.0.0:8000"
2020
Collection.__client__ = None
2121
Collection.__database__ = None
2222
self.client = Collection.get_client()

0 commit comments

Comments
 (0)