Skip to content

Commit

Permalink
[ENHANCEMENT]: Custom report
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Oct 15, 2024
1 parent fc31670 commit 8edf2cb
Show file tree
Hide file tree
Showing 8 changed files with 718 additions and 124 deletions.
76 changes: 36 additions & 40 deletions jac-cloud/jac_cloud/plugin/jaseci.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import OrderedDict
from contextlib import suppress
from dataclasses import Field, MISSING, fields
from dataclasses import Field, MISSING, fields, is_dataclass
from functools import wraps
from os import getenv
from re import compile
Expand Down Expand Up @@ -115,36 +115,38 @@ def populate_apis(cls: Type[WalkerArchitype]) -> None:
as_query += PATH_VARIABLE_REGEX.findall(path)

hintings = get_type_hints(cls)
if excluded != "*":
if isinstance(excluded, str):
excluded = [excluded]

for f in fields(cls):
if f.name in excluded:
if f.default is MISSING and not callable(f.default_factory):
raise AttributeError(
f"{cls.__name__} {f.name} should have default or default_factory."
)
continue

f_name = f.name
f_type = hintings[f_name]
if f_type in FILE_TYPES:
files[f_name] = gen_model_field(f_type, f, True)
else:
consts = gen_model_field(f_type, f)

if as_query == "*" or f_name in as_query:
query[f_name] = consts
if is_dataclass(cls):
if excluded != "*":
if isinstance(excluded, str):
excluded = [excluded]

for f in fields(cls):
if f.name in excluded:
if f.default is MISSING and not callable(f.default_factory):
raise AttributeError(
f"{cls.__name__} {f.name} should have default or default_factory."
)
continue

f_name = f.name
f_type = hintings[f_name]
if f_type in FILE_TYPES:
files[f_name] = gen_model_field(f_type, f, True)
else:
body[f_name] = consts
elif any(
f.default is MISSING and not callable(f.default_factory)
for f in fields(cls)
):
raise AttributeError(
f"{cls.__name__} fields should all have default or default_factory."
)
consts = gen_model_field(f_type, f)

if as_query == "*" or f_name in as_query:
query[f_name] = consts
else:
body[f_name] = consts
elif any(
f.default is MISSING and not callable(f.default_factory)
for f in fields(cls)
):
raise AttributeError(
f"{cls.__name__} fields should all have default or default_factory."
)

payload: dict[str, Any] = {
"query": (
Expand Down Expand Up @@ -197,6 +199,9 @@ def api_entry(
Jac.spawn_call(wlk.architype, jctx.entry_node.architype)
jctx.close()

if jctx.custom is not MISSING:
return jctx.custom

resp = jctx.response(wlk.returns)
log_exit(resp, log)

Expand Down Expand Up @@ -236,7 +241,7 @@ def api_root(

settings: dict[str, Any] = {
"tags": ["walker"],
"response_model": ContextResponse[ret_types],
"response_model": ContextResponse[ret_types] | Any,
}
if auth:
settings["dependencies"] = cast(list, authenticator)
Expand Down Expand Up @@ -620,20 +625,11 @@ def decorator(cls: Type[Architype]) -> Type[Architype]:
on_entry=on_entry,
on_exit=on_exit,
)
populate_apis(cls)
populate_apis(cls) # type: ignore[arg-type]
return cls

return decorator

@staticmethod
@hookimpl
def report(expr: Any) -> None: # noqa:ANN401
"""Jac's report stmt feature."""
if not FastAPI.is_enabled():
return JacFeatureImpl.report(expr=expr)

JaseciContext.get().reports.append(expr)

@staticmethod
@hookimpl
def get_root() -> Root:
Expand Down
Loading

0 comments on commit 8edf2cb

Please sign in to comment.