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

Commit 03dbf72

Browse files
committed
[CLEANUP]: Migrate some tools to jaclang-jaseci
1 parent 2d7b101 commit 03dbf72

File tree

1 file changed

+3
-90
lines changed

1 file changed

+3
-90
lines changed

jaclang/runtimelib/architype.py

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,22 @@
33
from __future__ import annotations
44

55
from copy import deepcopy
6-
from dataclasses import asdict, dataclass, field, fields, is_dataclass
6+
from dataclasses import asdict, dataclass, field, is_dataclass
77
from enum import Enum, IntEnum
88
from os import getenv
99
from pickle import dumps
1010
from types import UnionType
11-
from typing import (
12-
Any,
13-
Callable,
14-
ClassVar,
15-
Iterable,
16-
Optional,
17-
Type,
18-
TypeVar,
19-
cast,
20-
get_args,
21-
get_origin,
22-
get_type_hints,
23-
)
11+
from typing import Any, Callable, ClassVar, Iterable, Optional, Type, TypeVar, cast
2412
from uuid import UUID, uuid4
2513

26-
from jaclang.compiler.constant import EdgeDir, T
14+
from jaclang.compiler.constant import EdgeDir
2715
from jaclang.runtimelib.utils import collect_node_connections
2816

2917
MANUAL_SAVE = getenv("ENABLE_MANUAL_SAVE") == "true"
3018
TARCH = TypeVar("TARCH", bound="Architype")
3119
TANCH = TypeVar("TANCH", bound="Anchor")
3220

3321

34-
def to_dataclass(cls: type[T], data: dict[str, Any], **kwargs: object) -> T:
35-
"""Parse dict to dataclass."""
36-
hintings = get_type_hints(cls)
37-
for attr in fields(cls): # type: ignore[arg-type]
38-
if target := data.get(attr.name):
39-
hint = hintings[attr.name]
40-
if is_dataclass(hint):
41-
data[attr.name] = to_dataclass(hint, target)
42-
else:
43-
origin = get_origin(hint)
44-
if origin == dict and isinstance(target, dict):
45-
if is_dataclass(inner_cls := get_args(hint)[-1]):
46-
for key, value in target.items():
47-
target[key] = to_dataclass(inner_cls, value)
48-
elif (
49-
origin == list
50-
and isinstance(target, list)
51-
and is_dataclass(inner_cls := get_args(hint)[-1])
52-
):
53-
for key, value in enumerate(target):
54-
target[key] = to_dataclass(inner_cls, value)
55-
return cls(**data, **kwargs)
56-
57-
5822
class AnchorType(Enum):
5923
"""Enum For Anchor Types."""
6024

@@ -100,22 +64,6 @@ def check(
10064
else:
10165
return self.whitelist, self.anchors.get(anchor, AccessLevel.WRITE)
10266

103-
def serialize(self) -> dict[str, object]:
104-
"""Serialize Access."""
105-
return {
106-
"whitelist": self.whitelist,
107-
"anchors": {key: val.name for key, val in self.anchors.items()},
108-
}
109-
110-
@classmethod
111-
def deserialize(cls, data: dict[str, Any]) -> Access:
112-
"""Deserialize Access."""
113-
anchors = cast(dict[str, str], data.get("anchors"))
114-
return Access(
115-
whitelist=bool(data.get("whitelist")),
116-
anchors={key: AccessLevel[val] for key, val in anchors.items()},
117-
)
118-
11967

12068
@dataclass
12169
class Permission:
@@ -124,18 +72,6 @@ class Permission:
12472
all: AccessLevel = AccessLevel.NO_ACCESS
12573
roots: Access = field(default_factory=Access)
12674

127-
def serialize(self) -> dict[str, object]:
128-
"""Serialize Permission."""
129-
return {"all": self.all.name, "roots": self.roots.serialize()}
130-
131-
@classmethod
132-
def deserialize(cls, data: dict[str, Any]) -> Permission:
133-
"""Deserialize Permission."""
134-
return Permission(
135-
all=AccessLevel[data.get("all", AccessLevel.NO_ACCESS.name)],
136-
roots=Access.deserialize(data.get("roots", {})),
137-
)
138-
13975

14076
@dataclass
14177
class AnchorState:
@@ -778,8 +714,6 @@ class Architype:
778714

779715
_jac_entry_funcs_: list[DSFunc]
780716
_jac_exit_funcs_: list[DSFunc]
781-
__jac_classes__: dict[str, type[Architype]]
782-
__jac_hintings__: dict[str, type]
783717

784718
def __init__(self, __jac__: Optional[Anchor] = None) -> None:
785719
"""Create default architype."""
@@ -796,27 +730,6 @@ def __repr__(self) -> str:
796730
"""Override repr for architype."""
797731
return f"{self.__class__.__name__}"
798732

799-
@classmethod
800-
def __set_classes__(cls) -> dict[str, Any]:
801-
"""Initialize Jac Classes."""
802-
jac_classes = {}
803-
for sub in cls.__subclasses__():
804-
sub.__jac_hintings__ = get_type_hints(sub)
805-
jac_classes[sub.__name__] = sub
806-
cls.__jac_classes__ = jac_classes
807-
808-
return jac_classes
809-
810-
@classmethod
811-
def __get_class__(cls: type[TARCH], name: str) -> type[TARCH]:
812-
"""Build class map from subclasses."""
813-
jac_classes: dict[str, Any] | None = getattr(cls, "__jac_classes__", None)
814-
if not jac_classes or not (jac_class := jac_classes.get(name)):
815-
jac_classes = cls.__set_classes__()
816-
jac_class = jac_classes.get(name, cls)
817-
818-
return jac_class
819-
820733

821734
class NodeArchitype(Architype):
822735
"""Node Architype Protocol."""

0 commit comments

Comments
 (0)