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

Commit

Permalink
Remove is_origin, rename datasource to mem. prune plugin methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ypkang committed Aug 29, 2024
1 parent 44b823f commit d8d20cb
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 215 deletions.
16 changes: 9 additions & 7 deletions jaclang/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ def run(
mod = mod[:-4]

jctx = ExecutionContext.create(session=session)

if filename.endswith(".jac"):
JacMachine(base).attach_program(JacProgram(mod_bundle=None, bytecode=None))

ret_module = jac_import(
target=mod,
base_path=base,
cachable=cache,
override_name="__main__" if main else None,
is_origin=True,
)

if ret_module is None:
Expand All @@ -117,7 +117,6 @@ def run(
base_path=base,
cachable=cache,
override_name="__main__" if main else None,
is_origin=True,
)
if ret_module is None:
loaded_mod = None
Expand All @@ -129,7 +128,7 @@ def run(

if not node or node == "root":
entrypoint: Architype = jctx.root.architype
elif obj := jctx.datasource.find_by_id(UUID(node)):
elif obj := jctx.mem.find_by_id(UUID(node)):
entrypoint = obj.architype
else:
print(f"Entrypoint {node} not found.")
Expand All @@ -145,6 +144,7 @@ def run(
print(f"Walker {walker} not found.")

jctx.close()
JacMachine.detach()


@cmd_registry.register
Expand All @@ -158,7 +158,7 @@ def get_object(id: str, session: str = "") -> dict:
data = {}
if id == "root":
data = jctx.root.__getstate__()
elif obj := jctx.datasource.find_by_id(UUID(id)):
elif obj := jctx.mem.find_by_id(UUID(id)):
data = obj.__getstate__()
else:
print(f"Object with id {id} not found.")
Expand Down Expand Up @@ -238,13 +238,15 @@ def enter(
base, mod_name = os.path.split(filename)
base = base if base else "./"
mod_name = mod_name[:-4]
(mod,) = jac_import(target=mod_name, base_path=base, is_origin=True)
(mod,) = jac_import(target=mod_name, base_path=base)
JacMachine(base).attach_program(JacProgram(mod_bundle=None, bytecode=None))
if not mod:
print("Errors occurred while importing the module.")
else:
architype = getattr(mod, entrypoint)(*args)
if isinstance(architype, WalkerArchitype):
Jac.spawn_call(jctx.entry.architype, architype)
JacMachine.detach()

else:
print("Not a .jac file.")
Expand Down Expand Up @@ -391,7 +393,7 @@ def dot(

if filename.endswith(".jac"):
jac_machine = JacMachine(base)
jac_import(target=mod, base_path=base, is_origin=True)
jac_import(target=mod, base_path=base)
module = jac_machine.loaded_modules.get(mod)
globals().update(vars(module))
try:
Expand Down
19 changes: 13 additions & 6 deletions jaclang/compiler/tests/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from jaclang import jac_import
from jaclang.cli import cli
from jaclang.runtimelib.machine import JacMachine
from jaclang.runtimelib.machine import JacMachine, JacProgram
from jaclang.utils.test import TestCase


Expand All @@ -14,21 +14,28 @@ class TestLoader(TestCase):

def test_import_basic_python(self) -> None:
"""Test basic self loading."""
(h,) = jac_import("fixtures.hello_world", base_path=__file__, is_origin=True)
JacMachine(self.fixture_abs_path(__file__)).attach_program(
JacProgram(mod_bundle=None, bytecode=None)
)
(h,) = jac_import("fixtures.hello_world", base_path=__file__)
self.assertEqual(h.hello(), "Hello World!") # type: ignore
JacMachine.detach()

def test_modules_correct(self) -> None:
"""Test basic self loading."""
jac_machine = JacMachine(__file__)
jac_import("fixtures.hello_world", base_path=__file__, is_origin=True)
JacMachine(self.fixture_abs_path(__file__)).attach_program(
JacProgram(mod_bundle=None, bytecode=None)
)
jac_import("fixtures.hello_world", base_path=__file__)
self.assertIn(
"module 'fixtures.hello_world'",
str(jac_machine.loaded_modules),
str(JacMachine.get().loaded_modules),
)
self.assertIn(
"/tests/fixtures/hello_world.jac",
str(jac_machine.loaded_modules),
str(JacMachine.get().loaded_modules),
)
JacMachine.detach()

def test_jac_py_import(self) -> None:
"""Basic test for pass."""
Expand Down
31 changes: 5 additions & 26 deletions jaclang/plugin/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
ExecutionContext,
GenericEdge,
JacTestCheck,
Memory,
NodeAnchor,
NodeArchitype,
Root,
Expand Down Expand Up @@ -73,24 +72,6 @@ def get_context() -> ExecutionContext:
"""Get current execution context."""
return ExecutionContext.get()

@staticmethod
@hookimpl
def get_datasource() -> Memory:
"""Get current execution context."""
return ExecutionContext.get().datasource

@staticmethod
@hookimpl
def get_machine(base_path: str) -> JacMachine:
"""Get current execution context."""
return JacMachine.get(base_path)

@staticmethod
@hookimpl
def detach_machine() -> None:
"""Detach current jac machine."""
JacMachine.detach()

@staticmethod
@hookimpl
def make_architype(
Expand Down Expand Up @@ -250,7 +231,6 @@ def jac_import(
lng: Optional[str],
items: Optional[dict[str, Union[str, Optional[str]]]],
reload_module: Optional[bool],
is_origin: bool,
) -> tuple[types.ModuleType, ...]:
"""Core Import Process."""
spec = ImportPathSpec(
Expand All @@ -264,17 +244,16 @@ def jac_import(
items,
)

jac_machine = Jac.get_machine(base_path)
jac_machine = JacMachine.get(base_path)
if not jac_machine.jac_program:
jac_machine.attach_program(JacProgram(mod_bundle=None, bytecode=None))

if lng == "py":
import_result = PythonImporter(jac_machine).run_import(spec)
import_result = PythonImporter(JacMachine.get()).run_import(spec)
else:
import_result = JacImporter(jac_machine).run_import(spec, reload_module)

if is_origin:
Jac.detach_machine()
import_result = JacImporter(JacMachine.get()).run_import(
spec, reload_module
)

return (
(import_result.ret_mod,)
Expand Down
19 changes: 0 additions & 19 deletions jaclang/plugin/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
from jaclang.runtimelib.constructs import (
Architype,
EdgeArchitype,
Memory,
NodeAnchor,
NodeArchitype,
Root,
WalkerArchitype,
)
from jaclang.runtimelib.context import ExecutionContext
from jaclang.runtimelib.machine import JacMachine

import pluggy

Expand Down Expand Up @@ -48,21 +46,6 @@ def get_context() -> ExecutionContext:
"""Get current execution context."""
return pm.hook.get_context()

@staticmethod
def get_datasource() -> Memory:
"""Get current execution context."""
return pm.hook.get_datasource()

@staticmethod
def get_machine(base_path: str = "") -> JacMachine:
"""Get current jac machine."""
return pm.hook.get_machine(base_path=base_path)

@staticmethod
def detach_machine() -> None:
"""Detach current jac machine."""
return pm.hook.detach_machine()

@staticmethod
def make_architype(
cls: type,
Expand Down Expand Up @@ -121,7 +104,6 @@ def jac_import(
lng: Optional[str] = "jac",
items: Optional[dict[str, Union[str, Optional[str]]]] = None,
reload_module: Optional[bool] = False,
is_origin: bool = False,
) -> tuple[types.ModuleType, ...]:
"""Core Import Process."""
return pm.hook.jac_import(
Expand All @@ -134,7 +116,6 @@ def jac_import(
lng=lng,
items=items,
reload_module=reload_module,
is_origin=is_origin,
)

@staticmethod
Expand Down
21 changes: 0 additions & 21 deletions jaclang/plugin/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
)
from jaclang.runtimelib.constructs import EdgeArchitype, NodeAnchor, NodeArchitype
from jaclang.runtimelib.context import ExecutionContext
from jaclang.runtimelib.machine import JacMachine
from jaclang.runtimelib.memory import Memory

import pluggy

Expand All @@ -50,24 +48,6 @@ def get_context() -> ExecutionContext:
"""Get current execution context."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def get_datasource() -> Memory:
"""Get current execution context datasource."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def get_machine(base_path: str) -> JacMachine:
"""Get current jac machine."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def detach_machine() -> None:
"""Detach current jac machine."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def make_architype(
Expand Down Expand Up @@ -131,7 +111,6 @@ def jac_import(
lng: Optional[str],
items: Optional[dict[str, Union[str, Optional[str]]]],
reload_module: Optional[bool],
is_origin: bool,
) -> tuple[types.ModuleType, ...]:
"""Core Import Process."""
raise NotImplementedError
Expand Down
10 changes: 5 additions & 5 deletions jaclang/runtimelib/architype.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def save(self) -> None:
from jaclang.plugin.feature import JacFeature as Jac

self.persistent = True
Jac.get_datasource().set(self.id, self)
Jac.get_context().mem.set(self.id, self)

def destroy(self) -> None:
"""Destroy Anchor."""
from jaclang.plugin.feature import JacFeature as Jac

Jac.get_datasource().remove(self.id)
Jac.get_context().mem.remove(self.id)

def is_populated(self) -> bool:
"""Check if state."""
Expand All @@ -61,7 +61,7 @@ def populate(self) -> None:
"""Retrieve the Architype from db and return."""
from jaclang.plugin.feature import JacFeature as Jac

jsrc = Jac.get_datasource()
jsrc = Jac.get_context().mem

if anchor := jsrc.find_by_id(self.id):
self.__dict__.update(anchor.__dict__)
Expand Down Expand Up @@ -245,7 +245,7 @@ def destroy(self) -> None:
for edge in self.edges:
edge.destroy()

Jac.get_datasource().remove(self.id)
Jac.get_context().mem.remove(self.id)

def __getstate__(self) -> dict[str, object]:
"""Serialize Node Anchor."""
Expand Down Expand Up @@ -285,7 +285,7 @@ def destroy(self) -> None:
from jaclang.plugin.feature import JacFeature as Jac

self.detach()
Jac.get_datasource().remove(self.id)
Jac.get_context().mem.remove(self.id)

def __getstate__(self) -> dict[str, object]:
"""Serialize Node Anchor."""
Expand Down
10 changes: 5 additions & 5 deletions jaclang/runtimelib/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class ExecutionContext:
"""Execution Context."""

datasource: Memory
mem: Memory
reports: list[Any]
system_root: NodeAnchor
root: NodeAnchor
Expand All @@ -37,7 +37,7 @@ def generate_system_root(self) -> NodeAnchor:
id=UUID(SUPER_ROOT_UUID), architype=architype, persistent=True, edges=[]
)
architype.__jac__ = anchor
self.datasource.set(anchor.id, anchor)
self.mem.set(anchor.id, anchor)
return anchor

def init_anchor(
Expand All @@ -47,14 +47,14 @@ def init_anchor(
) -> NodeAnchor:
"""Load initial anchors."""
if anchor_id and isinstance(
anchor := self.datasource.find_by_id(UUID(anchor_id)), NodeAnchor
anchor := self.mem.find_by_id(UUID(anchor_id)), NodeAnchor
):
return anchor
return default() if callable(default) else default

def close(self) -> None:
"""Close current ExecutionContext."""
self.datasource.close()
self.mem.close()
EXECUTION_CONTEXT.set(None)

@staticmethod
Expand All @@ -66,7 +66,7 @@ def create(
) -> ExecutionContext:
"""Create ExecutionContext."""
ctx = ExecutionContext()
ctx.datasource = ShelfStorage(session)
ctx.mem = ShelfStorage(session)
ctx.reports = []
ctx.system_root = ctx.init_anchor(SUPER_ROOT_UUID, ctx.generate_system_root)
ctx.root = ctx.init_anchor(root, ctx.system_root)
Expand Down
Loading

0 comments on commit d8d20cb

Please sign in to comment.