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

Commit 410bebb

Browse files
committed
[MINOR]: Address PR Comments
1 parent bf54383 commit 410bebb

File tree

16 files changed

+52
-1620
lines changed

16 files changed

+52
-1620
lines changed

jaclang/cli/cli.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ast as ast3
44
import importlib
5-
import inspect
65
import marshal
76
import os
87
import pickle
@@ -18,7 +17,7 @@
1817
from jaclang.compiler.passes.main.pyast_load_pass import PyastBuildPass
1918
from jaclang.compiler.passes.main.schedules import py_code_gen_typed
2019
from jaclang.compiler.passes.tool.schedules import format_pass
21-
from jaclang.core.constructs import Anchor, NodeAnchor
20+
from jaclang.core.constructs import Anchor, NodeAnchor, WalkerArchitype
2221
from jaclang.plugin.builtin import dotgen
2322
from jaclang.plugin.feature import JacCmd as Cmd
2423
from jaclang.plugin.feature import JacFeature as Jac
@@ -66,13 +65,7 @@ def format_file(filename: str) -> None:
6665

6766
@cmd_registry.register
6867
def run(
69-
filename: str,
70-
session: str = "",
71-
main: bool = True,
72-
cache: bool = True,
73-
walker: str = "",
74-
node: str = "",
75-
root: str = "",
68+
filename: str, session: str = "", main: bool = True, cache: bool = True
7669
) -> None:
7770
"""Run the specified .jac file."""
7871
# if no session specified, check if it was defined when starting the command shell
@@ -86,58 +79,32 @@ def run(
8679
else ""
8780
)
8881

89-
jctx = Jac.context(
90-
{
91-
"session": session,
92-
"root": NodeAnchor.ref(root),
93-
"entry": NodeAnchor.ref(node),
94-
}
95-
)
82+
jctx = Jac.context({"session": session})
9683

9784
base, mod = os.path.split(filename)
9885
base = base if base else "./"
9986
mod = mod[:-4]
10087
if filename.endswith(".jac"):
101-
ret_module = jac_import(
88+
jac_import(
10289
target=mod,
10390
base_path=base,
10491
cachable=cache,
10592
override_name="__main__" if main else None,
10693
)
107-
if ret_module is None:
108-
loaded_mod = None
109-
else:
110-
(loaded_mod,) = ret_module
11194
elif filename.endswith(".jir"):
11295
with open(filename, "rb") as f:
11396
ir = pickle.load(f)
114-
ret_module = jac_import(
97+
jac_import(
11598
target=mod,
11699
base_path=base,
117100
cachable=cache,
118101
override_name="__main__" if main else None,
119102
mod_bundle=ir,
120103
)
121-
if ret_module is None:
122-
loaded_mod = None
123-
else:
124-
(loaded_mod,) = ret_module
125104
else:
126105
print("Not a .jac file.")
127106
return
128107

129-
# TODO: handle no override name
130-
if walker:
131-
walker_module = dict(inspect.getmembers(loaded_mod)).get(walker)
132-
if (
133-
walker_module
134-
and jctx.validate_access()
135-
and (architype := jctx.entry.architype)
136-
):
137-
Jac.spawn_call(architype, walker_module())
138-
else:
139-
print(f"Walker {walker} not found.")
140-
141108
jctx.close()
142109

143110

@@ -207,13 +174,29 @@ def lsp() -> None:
207174

208175

209176
@cmd_registry.register
210-
def enter(filename: str, entrypoint: str, args: list) -> None:
211-
"""Run the specified entrypoint function in the given .jac file.
177+
def enter(
178+
filename: str,
179+
session: str = "",
180+
walker: str = "",
181+
node: str = "",
182+
root: str = "",
183+
args: Optional[list] = None,
184+
) -> None:
185+
"""
186+
Run the specified entrypoint function in the given .jac file.
212187
213188
:param filename: The path to the .jac file.
214189
:param entrypoint: The name of the entrypoint function.
215190
:param args: Arguments to pass to the entrypoint function.
216191
"""
192+
jctx = Jac.context(
193+
{
194+
"session": session,
195+
"root": NodeAnchor.ref(root),
196+
"entry": NodeAnchor.ref(node),
197+
}
198+
)
199+
217200
if filename.endswith(".jac"):
218201
base, mod_name = os.path.split(filename)
219202
base = base if base else "./"
@@ -223,10 +206,24 @@ def enter(filename: str, entrypoint: str, args: list) -> None:
223206
print("Errors occurred while importing the module.")
224207
return
225208
else:
226-
getattr(mod, entrypoint)(*args)
209+
walker_architype: WalkerArchitype | None = getattr(mod[0], walker)(
210+
*args or []
211+
)
212+
if (
213+
walker_architype
214+
and jctx.validate_access()
215+
and (architype := jctx.entry.architype)
216+
):
217+
Jac.spawn_call(architype, walker_architype)
218+
else:
219+
print(f"Invalid Walker {walker} execution.")
220+
221+
getattr(mod[0], walker)(*args or [])
227222
else:
228223
print("Not a .jac file.")
229224

225+
jctx.close()
226+
230227

231228
@cmd_registry.register
232229
def test(
@@ -444,8 +441,6 @@ def start_cli() -> None:
444441
args_dict = vars(args)
445442
args_dict.pop("command")
446443
args_dict.pop("version", None)
447-
if command.func.__name__ != "run":
448-
args_dict.pop("session")
449444
ret = command.call(**args_dict)
450445
if ret:
451446
print(ret)

jaclang/core/architype.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
from jaclang.compiler.constant import EdgeDir
2323
from jaclang.core.utils import collect_node_connections
24-
from jaclang.vendor.orjson import dumps
24+
25+
from orjson import dumps
2526

2627
GENERIC_ID_REGEX = compile(
2728
r"^(g|n|e|w):([^:]*):([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$",
@@ -196,7 +197,7 @@ def sync(self, node: Optional["NodeAnchor"] = None) -> Optional[Architype]:
196197

197198
from .context import ExecutionContext
198199

199-
jsrc = ExecutionContext.get().datasource
200+
jsrc = ExecutionContext.get_or_create().datasource
200201
anchor = jsrc.find_one(self.id)
201202

202203
if anchor and (node or self).has_read_access(anchor):
@@ -208,7 +209,7 @@ def allocate(self) -> None:
208209
"""Allocate hashes and memory."""
209210
from .context import ExecutionContext
210211

211-
jctx = ExecutionContext.get()
212+
jctx = ExecutionContext.get_or_create()
212213
self.root = jctx.root.id
213214
jctx.datasource.set(self, True)
214215

@@ -228,7 +229,7 @@ def access_level(self, to: Anchor) -> int:
228229
"""Access validation."""
229230
from .context import ExecutionContext
230231

231-
jctx = ExecutionContext.get()
232+
jctx = ExecutionContext.get_or_create()
232233
jroot = jctx.root
233234
to.current_access_level = -1
234235

@@ -347,7 +348,7 @@ def ref(cls, ref_id: str) -> Optional[NodeAnchor]:
347348
def _save(self) -> None:
348349
from .context import ExecutionContext
349350

350-
jsrc = ExecutionContext.get().datasource
351+
jsrc = ExecutionContext.get_or_create().datasource
351352

352353
for edge in self.edges:
353354
edge.save()
@@ -359,7 +360,7 @@ def destroy(self) -> None:
359360
if self.architype and self.current_access_level > 1:
360361
from .context import ExecutionContext
361362

362-
jsrc = ExecutionContext.get().datasource
363+
jsrc = ExecutionContext.get_or_create().datasource
363364
for edge in self.edges:
364365
edge.destroy()
365366

@@ -507,7 +508,7 @@ def ref(cls, ref_id: str) -> Optional[EdgeAnchor]:
507508
def _save(self) -> None:
508509
from .context import ExecutionContext
509510

510-
jsrc = ExecutionContext.get().datasource
511+
jsrc = ExecutionContext.get_or_create().datasource
511512

512513
if source := self.source:
513514
source.save()
@@ -522,7 +523,7 @@ def destroy(self) -> None:
522523
if self.architype and self.current_access_level == 1:
523524
from .context import ExecutionContext
524525

525-
jsrc = ExecutionContext.get().datasource
526+
jsrc = ExecutionContext.get_or_create().datasource
526527

527528
source = self.source
528529
target = self.target
@@ -601,14 +602,14 @@ def ref(cls, ref_id: str) -> Optional[WalkerAnchor]:
601602
def _save(self) -> None:
602603
from .context import ExecutionContext
603604

604-
ExecutionContext.get().datasource.set(self)
605+
ExecutionContext.get_or_create().datasource.set(self)
605606

606607
def destroy(self) -> None:
607608
"""Delete Anchor."""
608609
if self.architype and self.current_access_level > 1:
609610
from .context import ExecutionContext
610611

611-
ExecutionContext.get().datasource.remove(self)
612+
ExecutionContext.get_or_create().datasource.remove(self)
612613

613614
def sync(self, node: Optional["NodeAnchor"] = None) -> Optional[WalkerArchitype]:
614615
"""Retrieve the Architype from db and return."""

jaclang/core/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def validate_access(self) -> bool:
6363
return self.root.has_read_access(self.entry)
6464

6565
@staticmethod
66-
def get(options: Optional[dict[str, Any]] = None) -> ExecutionContext:
66+
def get_or_create(options: Optional[dict[str, Any]] = None) -> ExecutionContext:
6767
"""Get or create execution context."""
6868
if not isinstance(ctx := EXECUTION_CONTEXT.get(None), ExecutionContext):
6969
EXECUTION_CONTEXT.set(ctx := ExecutionContext(**options or {}))

jaclang/plugin/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class JacFeatureDefaults:
6868
@hookimpl
6969
def context(options: Optional[dict[str, Any]]) -> ExecutionContext:
7070
"""Get the execution context."""
71-
return ExecutionContext.get(options)
71+
return ExecutionContext.get_or_create(options)
7272

7373
@staticmethod
7474
@hookimpl

jaclang/vendor/orjson-3.10.6.dist-info/INSTALLER

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)