From bad20f1d99751360dadfd390dd5ca1cc1118e8ca Mon Sep 17 00:00:00 2001 From: "Alexie (Boyong) Madolid" Date: Fri, 12 Jul 2024 20:29:05 +0800 Subject: [PATCH] [REFACTOR]: Rename ObjectAnchor to Anchor --- jaclang/cli/cli.py | 4 ++-- jaclang/core/architype.py | 36 ++++++++++++++++---------------- jaclang/core/constructs.py | 4 ++-- jaclang/core/memory.py | 30 +++++++++++++------------- jaclang/plugin/default.py | 6 +++--- stubs/jaclang/plugin/default.pyi | 4 ++-- 6 files changed, 41 insertions(+), 43 deletions(-) diff --git a/jaclang/cli/cli.py b/jaclang/cli/cli.py index 0d8319310..f5fac68d3 100644 --- a/jaclang/cli/cli.py +++ b/jaclang/cli/cli.py @@ -18,7 +18,7 @@ from jaclang.compiler.passes.main.pyast_load_pass import PyastBuildPass from jaclang.compiler.passes.main.schedules import py_code_gen_typed from jaclang.compiler.passes.tool.schedules import format_pass -from jaclang.core.constructs import NodeAnchor, ObjectAnchor +from jaclang.core.constructs import Anchor, NodeAnchor from jaclang.plugin.builtin import dotgen from jaclang.plugin.feature import JacCmd as Cmd from jaclang.plugin.feature import JacFeature as Jac @@ -141,7 +141,7 @@ def get_object(id: str, session: str = "") -> dict[str, object]: if id == "root": super_root = jctx.super_root response = super_root.serialize() - elif (anchor := ObjectAnchor.ref(id)) and anchor.sync(): + elif (anchor := Anchor.ref(id)) and anchor.sync(): response = anchor.serialize() jctx.close() diff --git a/jaclang/core/architype.py b/jaclang/core/architype.py index f94758a58..bc3c06f61 100644 --- a/jaclang/core/architype.py +++ b/jaclang/core/architype.py @@ -140,7 +140,7 @@ def deserialize(cls, data: dict[str, Any]) -> Permission: @dataclass(eq=False) -class ObjectAnchor: +class Anchor: """Object Anchor.""" type: ObjectType = ObjectType.generic @@ -160,10 +160,10 @@ def ref_id(self) -> str: return f"{self.type.value}:{self.name}:{self.id}" @staticmethod - def ref(ref_id: str) -> Optional[ObjectAnchor]: + def ref(ref_id: str) -> Optional[Anchor]: """Return ObjectAnchor instance if .""" if matched := GENERIC_ID_REGEX.search(ref_id): - cls: type = ObjectAnchor + cls: type = Anchor match ObjectType(matched.group(1)): case ObjectType.node: cls = NodeAnchor @@ -209,19 +209,19 @@ def allocate(self) -> None: self.root = jctx.root.id jctx.datasource.set(self, True) - def has_read_access(self, to: ObjectAnchor) -> bool: + def has_read_access(self, to: Anchor) -> bool: """Read Access Validation.""" return self.access_level(to) > -1 - def has_connect_access(self, to: ObjectAnchor) -> bool: + def has_connect_access(self, to: Anchor) -> bool: """Write Access Validation.""" return self.access_level(to) > 0 - def has_write_access(self, to: ObjectAnchor) -> bool: + def has_write_access(self, to: Anchor) -> bool: """Write Access Validation.""" return self.access_level(to) > 1 - def access_level(self, to: ObjectAnchor) -> int: + def access_level(self, to: Anchor) -> int: """Access validation.""" from .context import ExecutionContext @@ -301,7 +301,7 @@ def __hash__(self) -> int: def __eq__(self, other: object) -> bool: """Override equal implementation.""" - if isinstance(other, ObjectAnchor): + if isinstance(other, Anchor): return ( self.type == other.type and self.name == other.name @@ -316,7 +316,7 @@ def __eq__(self, other: object) -> bool: @dataclass(eq=False) -class NodeAnchor(ObjectAnchor): +class NodeAnchor(Anchor): """Node Anchor.""" type: ObjectType = ObjectType.node @@ -480,7 +480,7 @@ def serialize(self) -> dict[str, object]: @dataclass(eq=False) -class EdgeAnchor(ObjectAnchor): +class EdgeAnchor(Anchor): """Edge Anchor.""" type: ObjectType = ObjectType.edge @@ -585,14 +585,14 @@ def serialize(self) -> dict[str, object]: @dataclass(eq=False) -class WalkerAnchor(ObjectAnchor): +class WalkerAnchor(Anchor): """Walker Anchor.""" type: ObjectType = ObjectType.walker architype: Optional[WalkerArchitype] = None - path: list[ObjectAnchor] = field(default_factory=list) - next: list[ObjectAnchor] = field(default_factory=list) - ignores: list[ObjectAnchor] = field(default_factory=list) + path: list[Anchor] = field(default_factory=list) + next: list[Anchor] = field(default_factory=list) + ignores: list[Anchor] = field(default_factory=list) disengaged: bool = False persistent: bool = False # Disabled initially but can be adjusted @@ -667,7 +667,7 @@ def disengage_now(self) -> None: """Disengage walker from traversal.""" self.disengaged = True - def spawn_call(self, nd: ObjectAnchor) -> WalkerArchitype: + def spawn_call(self, nd: Anchor) -> WalkerArchitype: """Invoke data spatial call.""" if walker := self.sync(): self.path = [] @@ -718,16 +718,16 @@ class Architype: _jac_exit_funcs_: list[DSFunc] _jac_classes_: dict[str, type[Architype]] - def __init__(self, __jac__: Optional[ObjectAnchor] = None) -> None: + def __init__(self, __jac__: Optional[Anchor] = None) -> None: """Create default architype.""" - self.__jac__ = __jac__ or ObjectAnchor(architype=self) + self.__jac__ = __jac__ or Anchor(architype=self) self.__jac__.allocate() def __eq__(self, other: object) -> bool: """Override equal implementation.""" if isinstance(other, Architype): return super().__eq__(other) - elif isinstance(other, ObjectAnchor): + elif isinstance(other, Anchor): return self.__jac__ == other return False diff --git a/jaclang/core/constructs.py b/jaclang/core/constructs.py index 37bcd3477..655bd4bc0 100644 --- a/jaclang/core/constructs.py +++ b/jaclang/core/constructs.py @@ -4,6 +4,7 @@ from .architype import ( + Anchor, Architype, DSFunc, EdgeAnchor, @@ -11,7 +12,6 @@ GenericEdge, NodeAnchor, NodeArchitype, - ObjectAnchor, Root, WalkerAnchor, WalkerArchitype, @@ -21,7 +21,7 @@ from .test import JacTestCheck, JacTestResult, JacTextTestRunner __all__ = [ - "ObjectAnchor", + "Anchor", "NodeAnchor", "EdgeAnchor", "WalkerAnchor", diff --git a/jaclang/core/memory.py b/jaclang/core/memory.py index b766242cb..45099a5f8 100644 --- a/jaclang/core/memory.py +++ b/jaclang/core/memory.py @@ -7,13 +7,13 @@ from uuid import UUID from .architype import ( + Anchor, Architype, EdgeAnchor, EdgeArchitype, MANUAL_SAVE, NodeAnchor, NodeArchitype, - ObjectAnchor, ObjectType, Permission, WalkerAnchor, @@ -27,7 +27,7 @@ class Memory: """Generic Memory Handler.""" - __mem__: dict[str, ObjectAnchor] = field(default_factory=dict) + __mem__: dict[str, Anchor] = field(default_factory=dict) __gc__: set[str] = field(default_factory=set) def close(self) -> None: @@ -40,8 +40,8 @@ def __del__(self) -> None: self.close() def find( - self, ids: IDS, filter: Optional[Callable[[ObjectAnchor], ObjectAnchor]] = None - ) -> Generator[ObjectAnchor, None, None]: + self, ids: IDS, filter: Optional[Callable[[Anchor], Anchor]] = None + ) -> Generator[Anchor, None, None]: """Find anchors from memory by ids with filter.""" if not isinstance(ids, list): ids = [ids] @@ -55,12 +55,12 @@ def find( def find_one( self, ids: IDS, - filter: Optional[Callable[[ObjectAnchor], ObjectAnchor]] = None, - ) -> Optional[ObjectAnchor]: + filter: Optional[Callable[[Anchor], Anchor]] = None, + ) -> Optional[Anchor]: """Find one anchor from memory by ids with filter.""" return next(self.find(ids, filter), None) - def set(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None: + def set(self, data: Union[Anchor, list[Anchor]]) -> None: """Save anchor/s to memory.""" if isinstance(data, list): for d in data: @@ -69,7 +69,7 @@ def set(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None: elif str(data.id) not in self.__gc__: self.__mem__[str(data.id)] = data - def remove(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None: + def remove(self, data: Union[Anchor, list[Anchor]]) -> None: """Remove anchor/s from memory.""" if isinstance(data, list): for d in data: @@ -106,8 +106,8 @@ def close(self) -> None: super().close() def find( - self, ids: IDS, filter: Optional[Callable[[ObjectAnchor], ObjectAnchor]] = None - ) -> Generator[ObjectAnchor, None, None]: + self, ids: IDS, filter: Optional[Callable[[Anchor], Anchor]] = None + ) -> Generator[Anchor, None, None]: """Find anchors from datasource by ids with filter.""" if not isinstance(ids, list): ids = [ids] @@ -128,9 +128,7 @@ def find( else: yield from super().find(ids, filter) - def set( - self, data: Union[ObjectAnchor, list[ObjectAnchor]], mem_only: bool = False - ) -> None: + def set(self, data: Union[Anchor, list[Anchor]], mem_only: bool = False) -> None: """Save anchor/s to datasource.""" super().set(data) @@ -146,7 +144,7 @@ def set( if d.current_access_level > 1: self.__shelf__[_id]["architype"] = json["architype"] - def remove(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None: + def remove(self, data: Union[Anchor, list[Anchor]]) -> None: """Remove anchor/s from datasource.""" super().remove(data) if isinstance(self.__shelf__, Shelf) and MANUAL_SAVE: @@ -156,7 +154,7 @@ def remove(self, data: Union[ObjectAnchor, list[ObjectAnchor]]) -> None: else: self.__shelf__.pop(str(data.id), None) - def get(self, anchor: dict[str, Any]) -> ObjectAnchor: + def get(self, anchor: dict[str, Any]) -> Anchor: """Get Anchor Instance.""" name = cast(str, anchor.get("name")) architype = anchor.pop("architype") @@ -191,6 +189,6 @@ def get(self, anchor: dict[str, Any]) -> ObjectAnchor: wanch.architype = WalkerArchitype.get(name)(__jac__=wanch, **architype) return wanch case _: - oanch = ObjectAnchor(access=access, **anchor) + oanch = Anchor(access=access, **anchor) oanch.architype = Architype(__jac__=oanch) return oanch diff --git a/jaclang/plugin/default.py b/jaclang/plugin/default.py index ce35a96c1..4a25fe153 100644 --- a/jaclang/plugin/default.py +++ b/jaclang/plugin/default.py @@ -16,6 +16,7 @@ from jaclang.compiler.constant import EdgeDir, T, colors from jaclang.compiler.semtable import SemInfo, SemRegistry, SemScope from jaclang.core.constructs import ( + Anchor, Architype, DSFunc, EdgeAnchor, @@ -24,7 +25,6 @@ JacTestCheck, NodeAnchor, NodeArchitype, - ObjectAnchor, Root, WalkerAnchor, WalkerArchitype, @@ -43,7 +43,7 @@ "GenericEdge", "JacTestCheck", "NodeAnchor", - "ObjectAnchor", + "Anchor", "WalkerAnchor", "NodeArchitype", "EdgeArchitype", @@ -112,7 +112,7 @@ def make_architype( def new_init( self: Architype, *args: object, - __jac__: Optional[ObjectAnchor] = None, + __jac__: Optional[Anchor] = None, **kwargs: object, ) -> None: arch_base.__init__(self, __jac__) diff --git a/stubs/jaclang/plugin/default.pyi b/stubs/jaclang/plugin/default.pyi index 417b9a672..69d77d9d3 100644 --- a/stubs/jaclang/plugin/default.pyi +++ b/stubs/jaclang/plugin/default.pyi @@ -1,6 +1,7 @@ import types from jaclang.compiler.constant import EdgeDir from jaclang.core.constructs import ( + Anchor as Anchor, Architype as Architype, DSFunc as DSFunc, EdgeAnchor as EdgeAnchor, @@ -9,7 +10,6 @@ from jaclang.core.constructs import ( JacTestCheck as JacTestCheck, NodeAnchor as NodeAnchor, NodeArchitype as NodeArchitype, - ObjectAnchor as ObjectAnchor, Root as Root, WalkerAnchor as WalkerAnchor, WalkerArchitype as WalkerArchitype, @@ -25,7 +25,7 @@ __all__ = [ "GenericEdge", "JacTestCheck", "NodeAnchor", - "ObjectAnchor", + "Anchor", "WalkerAnchor", "NodeArchitype", "EdgeArchitype",