jaclang 0.7.22__py3-none-any.whl → 0.7.23__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of jaclang might be problematic. Click here for more details.

jaclang/plugin/feature.py CHANGED
@@ -4,57 +4,265 @@ from __future__ import annotations
4
4
 
5
5
  import ast as ast3
6
6
  import types
7
- from typing import Any, Callable, Mapping, Optional, Sequence, Type, TypeAlias, Union
7
+ from typing import (
8
+ Any,
9
+ Callable,
10
+ ClassVar,
11
+ Mapping,
12
+ Optional,
13
+ Sequence,
14
+ Type,
15
+ TypeAlias,
16
+ Union,
17
+ )
18
+ from uuid import UUID
8
19
 
9
- import jaclang.compiler.absyntree as ast
10
- from jaclang.compiler.passes.main.pyast_gen_pass import PyastGenPass
11
- from jaclang.plugin.spec import JacBuiltin, JacCmdSpec, JacFeatureSpec, P, T
12
- from jaclang.runtimelib.constructs import (
20
+ from jaclang.plugin.spec import (
21
+ AccessLevel,
22
+ Anchor,
13
23
  Architype,
24
+ DSFunc,
25
+ EdgeAnchor,
14
26
  EdgeArchitype,
27
+ EdgeDir,
28
+ ExecutionContext,
15
29
  NodeAnchor,
16
30
  NodeArchitype,
31
+ P,
32
+ PyastGenPass,
17
33
  Root,
34
+ T,
18
35
  WalkerArchitype,
36
+ ast,
37
+ plugin_manager,
19
38
  )
20
- from jaclang.runtimelib.context import ExecutionContext
21
39
 
22
- import pluggy
23
40
 
24
- pm = pluggy.PluginManager("jac")
25
- pm.add_hookspecs(JacFeatureSpec)
26
- pm.add_hookspecs(JacCmdSpec)
27
- pm.add_hookspecs(JacBuiltin)
41
+ class JacAccessValidation:
42
+ """Jac Access Validation Specs."""
28
43
 
44
+ @staticmethod
45
+ def allow_root(
46
+ architype: Architype,
47
+ root_id: UUID,
48
+ level: AccessLevel | int | str = AccessLevel.READ,
49
+ ) -> None:
50
+ """Allow all access from target root graph to current Architype."""
51
+ plugin_manager.hook.allow_root(
52
+ architype=architype, root_id=root_id, level=level
53
+ )
29
54
 
30
- class JacFeature:
31
- """Jac Feature."""
55
+ @staticmethod
56
+ def disallow_root(
57
+ architype: Architype,
58
+ root_id: UUID,
59
+ level: AccessLevel | int | str = AccessLevel.READ,
60
+ ) -> None:
61
+ """Disallow all access from target root graph to current Architype."""
62
+ plugin_manager.hook.disallow_root(
63
+ architype=architype, root_id=root_id, level=level
64
+ )
65
+
66
+ @staticmethod
67
+ def unrestrict(
68
+ architype: Architype, level: AccessLevel | int | str = AccessLevel.READ
69
+ ) -> None:
70
+ """Allow everyone to access current Architype."""
71
+ plugin_manager.hook.unrestrict(architype=architype, level=level)
72
+
73
+ @staticmethod
74
+ def restrict(architype: Architype) -> None:
75
+ """Disallow others to access current Architype."""
76
+ plugin_manager.hook.restrict(architype=architype)
77
+
78
+ @staticmethod
79
+ def check_read_access(to: Anchor) -> bool:
80
+ """Read Access Validation."""
81
+ return plugin_manager.hook.check_read_access(to=to)
82
+
83
+ @staticmethod
84
+ def check_connect_access(to: Anchor) -> bool:
85
+ """Write Access Validation."""
86
+ return plugin_manager.hook.check_connect_access(to=to)
87
+
88
+ @staticmethod
89
+ def check_write_access(to: Anchor) -> bool:
90
+ """Write Access Validation."""
91
+ return plugin_manager.hook.check_write_access(to=to)
92
+
93
+ @staticmethod
94
+ def check_access_level(to: Anchor) -> AccessLevel:
95
+ """Access validation."""
96
+ return plugin_manager.hook.check_access_level(to=to)
97
+
98
+
99
+ class JacNode:
100
+ """Jac Node Operations."""
101
+
102
+ @staticmethod
103
+ def node_dot(node: NodeArchitype, dot_file: Optional[str] = None) -> str:
104
+ """Generate Dot file for visualizing nodes and edges."""
105
+ return plugin_manager.hook.node_dot(node=node, dot_file=dot_file)
106
+
107
+ @staticmethod
108
+ def get_edges(
109
+ node: NodeAnchor,
110
+ dir: EdgeDir,
111
+ filter_func: Optional[Callable[[list[EdgeArchitype]], list[EdgeArchitype]]],
112
+ target_obj: Optional[list[NodeArchitype]],
113
+ ) -> list[EdgeArchitype]:
114
+ """Get edges connected to this node."""
115
+ return plugin_manager.hook.get_edges(
116
+ node=node, dir=dir, filter_func=filter_func, target_obj=target_obj
117
+ )
118
+
119
+ @staticmethod
120
+ def edges_to_nodes(
121
+ node: NodeAnchor,
122
+ dir: EdgeDir,
123
+ filter_func: Optional[Callable[[list[EdgeArchitype]], list[EdgeArchitype]]],
124
+ target_obj: Optional[list[NodeArchitype]],
125
+ ) -> list[NodeArchitype]:
126
+ """Get set of nodes connected to this node."""
127
+ return plugin_manager.hook.edges_to_nodes(
128
+ node=node, dir=dir, filter_func=filter_func, target_obj=target_obj
129
+ )
130
+
131
+ @staticmethod
132
+ def remove_edge(node: NodeAnchor, edge: EdgeAnchor) -> None:
133
+ """Remove reference without checking sync status."""
134
+ return plugin_manager.hook.remove_edge(node=node, edge=edge)
135
+
136
+
137
+ class JacEdge:
138
+ """Jac Edge Operations."""
139
+
140
+ @staticmethod
141
+ def detach(edge: EdgeAnchor) -> None:
142
+ """Detach edge from nodes."""
143
+ return plugin_manager.hook.detach(edge=edge)
144
+
145
+
146
+ class JacWalker:
147
+ """Jac Edge Operations."""
148
+
149
+ @staticmethod
150
+ def visit_node(
151
+ walker: WalkerArchitype,
152
+ expr: (
153
+ list[NodeArchitype | EdgeArchitype]
154
+ | list[NodeArchitype]
155
+ | list[EdgeArchitype]
156
+ | NodeArchitype
157
+ | EdgeArchitype
158
+ ),
159
+ ) -> bool: # noqa: ANN401
160
+ """Jac's visit stmt feature."""
161
+ return plugin_manager.hook.visit_node(walker=walker, expr=expr)
162
+
163
+ @staticmethod
164
+ def ignore(
165
+ walker: WalkerArchitype,
166
+ expr: (
167
+ list[NodeArchitype | EdgeArchitype]
168
+ | list[NodeArchitype]
169
+ | list[EdgeArchitype]
170
+ | NodeArchitype
171
+ | EdgeArchitype
172
+ ),
173
+ ) -> bool: # noqa: ANN401
174
+ """Jac's ignore stmt feature."""
175
+ return plugin_manager.hook.ignore(walker=walker, expr=expr)
176
+
177
+ @staticmethod
178
+ def spawn_call(op1: Architype, op2: Architype) -> WalkerArchitype:
179
+ """Jac's spawn operator feature."""
180
+ return plugin_manager.hook.spawn_call(op1=op1, op2=op2)
181
+
182
+ @staticmethod
183
+ def disengage(walker: WalkerArchitype) -> bool:
184
+ """Jac's disengage stmt feature."""
185
+ return plugin_manager.hook.disengage(walker=walker)
186
+
187
+
188
+ class JacClassReferences:
189
+ """Default Classes References."""
190
+
191
+ EdgeDir: ClassVar[TypeAlias] = EdgeDir
192
+ DSFunc: ClassVar[TypeAlias] = DSFunc
193
+ RootType: ClassVar[TypeAlias] = Root
194
+ Obj: ClassVar[TypeAlias] = Architype
195
+ Node: ClassVar[TypeAlias] = NodeArchitype
196
+ Edge: ClassVar[TypeAlias] = EdgeArchitype
197
+ Walker: ClassVar[TypeAlias] = WalkerArchitype
198
+
199
+
200
+ class JacBuiltin:
201
+ """Jac Builtins."""
202
+
203
+ @staticmethod
204
+ def dotgen(
205
+ node: NodeArchitype,
206
+ depth: int,
207
+ traverse: bool,
208
+ edge_type: Optional[list[str]],
209
+ bfs: bool,
210
+ edge_limit: int,
211
+ node_limit: int,
212
+ dot_file: Optional[str],
213
+ ) -> str:
214
+ """Generate Dot file for visualizing nodes and edges."""
215
+ return plugin_manager.hook.dotgen(
216
+ node=node,
217
+ depth=depth,
218
+ traverse=traverse,
219
+ edge_type=edge_type,
220
+ bfs=bfs,
221
+ edge_limit=edge_limit,
222
+ node_limit=node_limit,
223
+ dot_file=dot_file,
224
+ )
225
+
226
+
227
+ class JacCmd:
228
+ """Jac CLI command."""
32
229
 
33
- from jaclang.compiler.constant import EdgeDir as EdgeDirType
34
- from jaclang.runtimelib.constructs import DSFunc as DSFuncType
230
+ @staticmethod
231
+ def create_cmd() -> None:
232
+ """Create Jac CLI cmds."""
233
+ return plugin_manager.hook.create_cmd()
234
+
235
+
236
+ class JacFeature(
237
+ JacClassReferences,
238
+ JacAccessValidation,
239
+ JacNode,
240
+ JacEdge,
241
+ JacWalker,
242
+ JacBuiltin,
243
+ JacCmd,
244
+ ):
245
+ """Jac Feature."""
35
246
 
36
- EdgeDir: TypeAlias = EdgeDirType
37
- DSFunc: TypeAlias = DSFuncType
38
- RootType: TypeAlias = Root
39
- Obj: TypeAlias = Architype
40
- Node: TypeAlias = NodeArchitype
41
- Edge: TypeAlias = EdgeArchitype
42
- Walker: TypeAlias = WalkerArchitype
247
+ @staticmethod
248
+ def setup() -> None:
249
+ """Set Class References."""
250
+ plugin_manager.hook.setup()
43
251
 
44
252
  @staticmethod
45
253
  def get_context() -> ExecutionContext:
46
254
  """Get current execution context."""
47
- return pm.hook.get_context()
255
+ return plugin_manager.hook.get_context()
48
256
 
49
257
  @staticmethod
50
258
  def get_object(id: str) -> Architype | None:
51
259
  """Get object given id."""
52
- return pm.hook.get_object(id=id)
260
+ return plugin_manager.hook.get_object(id=id)
53
261
 
54
262
  @staticmethod
55
263
  def object_ref(obj: Architype) -> str:
56
264
  """Get object reference id."""
57
- return pm.hook.object_ref(obj=obj)
265
+ return plugin_manager.hook.object_ref(obj=obj)
58
266
 
59
267
  @staticmethod
60
268
  def make_architype(
@@ -64,7 +272,7 @@ class JacFeature:
64
272
  on_exit: list[DSFunc],
65
273
  ) -> Type[Architype]:
66
274
  """Create a obj architype."""
67
- return pm.hook.make_architype(
275
+ return plugin_manager.hook.make_architype(
68
276
  cls=cls, on_entry=on_entry, on_exit=on_exit, arch_base=arch_base
69
277
  )
70
278
 
@@ -73,35 +281,35 @@ class JacFeature:
73
281
  on_entry: list[DSFunc], on_exit: list[DSFunc]
74
282
  ) -> Callable[[type], type]:
75
283
  """Create a obj architype."""
76
- return pm.hook.make_obj(on_entry=on_entry, on_exit=on_exit)
284
+ return plugin_manager.hook.make_obj(on_entry=on_entry, on_exit=on_exit)
77
285
 
78
286
  @staticmethod
79
287
  def make_node(
80
288
  on_entry: list[DSFunc], on_exit: list[DSFunc]
81
289
  ) -> Callable[[type], type]:
82
290
  """Create a node architype."""
83
- return pm.hook.make_node(on_entry=on_entry, on_exit=on_exit)
291
+ return plugin_manager.hook.make_node(on_entry=on_entry, on_exit=on_exit)
84
292
 
85
293
  @staticmethod
86
294
  def make_edge(
87
295
  on_entry: list[DSFunc], on_exit: list[DSFunc]
88
296
  ) -> Callable[[type], type]:
89
297
  """Create a edge architype."""
90
- return pm.hook.make_edge(on_entry=on_entry, on_exit=on_exit)
298
+ return plugin_manager.hook.make_edge(on_entry=on_entry, on_exit=on_exit)
91
299
 
92
300
  @staticmethod
93
301
  def make_walker(
94
302
  on_entry: list[DSFunc], on_exit: list[DSFunc]
95
303
  ) -> Callable[[type], type]:
96
304
  """Create a walker architype."""
97
- return pm.hook.make_walker(on_entry=on_entry, on_exit=on_exit)
305
+ return plugin_manager.hook.make_walker(on_entry=on_entry, on_exit=on_exit)
98
306
 
99
307
  @staticmethod
100
308
  def impl_patch_filename(
101
309
  file_loc: str,
102
310
  ) -> Callable[[Callable[P, T]], Callable[P, T]]:
103
311
  """Update impl file location."""
104
- return pm.hook.impl_patch_filename(file_loc=file_loc)
312
+ return plugin_manager.hook.impl_patch_filename(file_loc=file_loc)
105
313
 
106
314
  @staticmethod
107
315
  def jac_import(
@@ -116,7 +324,7 @@ class JacFeature:
116
324
  reload_module: Optional[bool] = False,
117
325
  ) -> tuple[types.ModuleType, ...]:
118
326
  """Core Import Process."""
119
- return pm.hook.jac_import(
327
+ return plugin_manager.hook.jac_import(
120
328
  target=target,
121
329
  base_path=base_path,
122
330
  absorb=absorb,
@@ -131,7 +339,7 @@ class JacFeature:
131
339
  @staticmethod
132
340
  def create_test(test_fun: Callable) -> Callable:
133
341
  """Create a test."""
134
- return pm.hook.create_test(test_fun=test_fun)
342
+ return plugin_manager.hook.create_test(test_fun=test_fun)
135
343
 
136
344
  @staticmethod
137
345
  def run_test(
@@ -143,7 +351,7 @@ class JacFeature:
143
351
  verbose: bool = False,
144
352
  ) -> int:
145
353
  """Run the test suite in the specified .jac file."""
146
- return pm.hook.run_test(
354
+ return plugin_manager.hook.run_test(
147
355
  filepath=filepath,
148
356
  filter=filter,
149
357
  xit=xit,
@@ -155,55 +363,17 @@ class JacFeature:
155
363
  @staticmethod
156
364
  def elvis(op1: Optional[T], op2: T) -> T:
157
365
  """Jac's elvis operator feature."""
158
- return pm.hook.elvis(op1=op1, op2=op2)
366
+ return plugin_manager.hook.elvis(op1=op1, op2=op2)
159
367
 
160
368
  @staticmethod
161
369
  def has_instance_default(gen_func: Callable[[], T]) -> T:
162
370
  """Jac's has container default feature."""
163
- return pm.hook.has_instance_default(gen_func=gen_func)
164
-
165
- @staticmethod
166
- def spawn_call(op1: Architype, op2: Architype) -> WalkerArchitype:
167
- """Jac's spawn operator feature."""
168
- return pm.hook.spawn_call(op1=op1, op2=op2)
371
+ return plugin_manager.hook.has_instance_default(gen_func=gen_func)
169
372
 
170
373
  @staticmethod
171
374
  def report(expr: Any) -> Any: # noqa: ANN401
172
375
  """Jac's report stmt feature."""
173
- return pm.hook.report(expr=expr)
174
-
175
- @staticmethod
176
- def ignore(
177
- walker: WalkerArchitype,
178
- expr: (
179
- list[NodeArchitype | EdgeArchitype]
180
- | list[NodeArchitype]
181
- | list[EdgeArchitype]
182
- | NodeArchitype
183
- | EdgeArchitype
184
- ),
185
- ) -> bool: # noqa: ANN401
186
- """Jac's ignore stmt feature."""
187
- return pm.hook.ignore(walker=walker, expr=expr)
188
-
189
- @staticmethod
190
- def visit_node(
191
- walker: WalkerArchitype,
192
- expr: (
193
- list[NodeArchitype | EdgeArchitype]
194
- | list[NodeArchitype]
195
- | list[EdgeArchitype]
196
- | NodeArchitype
197
- | EdgeArchitype
198
- ),
199
- ) -> bool: # noqa: ANN401
200
- """Jac's visit stmt feature."""
201
- return pm.hook.visit_node(walker=walker, expr=expr)
202
-
203
- @staticmethod
204
- def disengage(walker: WalkerArchitype) -> bool: # noqa: ANN401
205
- """Jac's disengage stmt feature."""
206
- return pm.hook.disengage(walker=walker)
376
+ return plugin_manager.hook.report(expr=expr)
207
377
 
208
378
  @staticmethod
209
379
  def edge_ref(
@@ -214,7 +384,7 @@ class JacFeature:
214
384
  edges_only: bool = False,
215
385
  ) -> list[NodeArchitype] | list[EdgeArchitype]:
216
386
  """Jac's apply_dir stmt feature."""
217
- return pm.hook.edge_ref(
387
+ return plugin_manager.hook.edge_ref(
218
388
  node_obj=node_obj,
219
389
  target_obj=target_obj,
220
390
  dir=dir,
@@ -233,7 +403,7 @@ class JacFeature:
233
403
 
234
404
  Note: connect needs to call assign compr with tuple in op
235
405
  """
236
- return pm.hook.connect(
406
+ return plugin_manager.hook.connect(
237
407
  left=left, right=right, edge_spec=edge_spec, edges_only=edges_only
238
408
  )
239
409
 
@@ -245,7 +415,7 @@ class JacFeature:
245
415
  filter_func: Optional[Callable[[list[EdgeArchitype]], list[EdgeArchitype]]],
246
416
  ) -> bool:
247
417
  """Jac's disconnect operator feature."""
248
- return pm.hook.disconnect(
418
+ return plugin_manager.hook.disconnect(
249
419
  left=left,
250
420
  right=right,
251
421
  dir=dir,
@@ -257,17 +427,17 @@ class JacFeature:
257
427
  target: list[T], attr_val: tuple[tuple[str], tuple[Any]]
258
428
  ) -> list[T]:
259
429
  """Jac's assign comprehension feature."""
260
- return pm.hook.assign_compr(target=target, attr_val=attr_val)
430
+ return plugin_manager.hook.assign_compr(target=target, attr_val=attr_val)
261
431
 
262
432
  @staticmethod
263
433
  def get_root() -> Root:
264
434
  """Jac's root getter."""
265
- return pm.hook.get_root()
435
+ return plugin_manager.hook.get_root()
266
436
 
267
437
  @staticmethod
268
438
  def get_root_type() -> Type[Root]:
269
439
  """Jac's root type getter."""
270
- return pm.hook.get_root_type()
440
+ return plugin_manager.hook.get_root_type()
271
441
 
272
442
  @staticmethod
273
443
  def build_edge(
@@ -276,28 +446,42 @@ class JacFeature:
276
446
  conn_assign: Optional[tuple[tuple, tuple]],
277
447
  ) -> Callable[[NodeAnchor, NodeAnchor], EdgeArchitype]:
278
448
  """Jac's root getter."""
279
- return pm.hook.build_edge(
449
+ return plugin_manager.hook.build_edge(
280
450
  is_undirected=is_undirected, conn_type=conn_type, conn_assign=conn_assign
281
451
  )
282
452
 
453
+ @staticmethod
454
+ def save(
455
+ obj: Architype | Anchor,
456
+ ) -> None:
457
+ """Destroy object."""
458
+ plugin_manager.hook.save(obj=obj)
459
+
460
+ @staticmethod
461
+ def destroy(
462
+ obj: Architype | Anchor,
463
+ ) -> None:
464
+ """Destroy object."""
465
+ plugin_manager.hook.destroy(obj=obj)
466
+
283
467
  @staticmethod
284
468
  def get_semstr_type(
285
469
  file_loc: str, scope: str, attr: str, return_semstr: bool
286
470
  ) -> Optional[str]:
287
471
  """Jac's get_semstr_type feature."""
288
- return pm.hook.get_semstr_type(
472
+ return plugin_manager.hook.get_semstr_type(
289
473
  file_loc=file_loc, scope=scope, attr=attr, return_semstr=return_semstr
290
474
  )
291
475
 
292
476
  @staticmethod
293
477
  def obj_scope(file_loc: str, attr: str) -> str:
294
478
  """Jac's get_semstr_type feature."""
295
- return pm.hook.obj_scope(file_loc=file_loc, attr=attr)
479
+ return plugin_manager.hook.obj_scope(file_loc=file_loc, attr=attr)
296
480
 
297
481
  @staticmethod
298
482
  def get_sem_type(file_loc: str, attr: str) -> tuple[str | None, str | None]:
299
483
  """Jac's get_semstr_type feature."""
300
- return pm.hook.get_sem_type(file_loc=file_loc, attr=attr)
484
+ return plugin_manager.hook.get_sem_type(file_loc=file_loc, attr=attr)
301
485
 
302
486
  @staticmethod
303
487
  def with_llm(
@@ -314,7 +498,7 @@ class JacFeature:
314
498
  _locals: Mapping,
315
499
  ) -> Any: # noqa: ANN401
316
500
  """Jac's with_llm feature."""
317
- return pm.hook.with_llm(
501
+ return plugin_manager.hook.with_llm(
318
502
  file_loc=file_loc,
319
503
  model=model,
320
504
  model_params=model_params,
@@ -331,7 +515,7 @@ class JacFeature:
331
515
  @staticmethod
332
516
  def gen_llm_body(_pass: PyastGenPass, node: ast.Ability) -> list[ast3.AST]:
333
517
  """Generate the by LLM body."""
334
- return pm.hook.gen_llm_body(_pass=_pass, node=node)
518
+ return plugin_manager.hook.gen_llm_body(_pass=_pass, node=node)
335
519
 
336
520
  @staticmethod
337
521
  def by_llm_call(
@@ -346,7 +530,7 @@ class JacFeature:
346
530
  exclude_info: list[tuple[str, ast3.AST]],
347
531
  ) -> ast3.Call:
348
532
  """Return the LLM Call, e.g. _Jac.with_llm()."""
349
- return pm.hook.by_llm_call(
533
+ return plugin_manager.hook.by_llm_call(
350
534
  _pass=_pass,
351
535
  model=model,
352
536
  model_params=model_params,
@@ -361,13 +545,4 @@ class JacFeature:
361
545
  @staticmethod
362
546
  def get_by_llm_call_args(_pass: PyastGenPass, node: ast.FuncCall) -> dict:
363
547
  """Get the by LLM call args."""
364
- return pm.hook.get_by_llm_call_args(_pass=_pass, node=node)
365
-
366
-
367
- class JacCmd:
368
- """Jac CLI command."""
369
-
370
- @staticmethod
371
- def create_cmd() -> None:
372
- """Create Jac CLI cmds."""
373
- return pm.hook.create_cmd()
548
+ return plugin_manager.hook.get_by_llm_call_args(_pass=_pass, node=node)