From 02abf412029f9833b22eb4e31be23ccda587a691 Mon Sep 17 00:00:00 2001 From: marsninja Date: Wed, 31 Jan 2024 10:48:38 -0500 Subject: [PATCH] feat: example plugin for walkers --- .../jaclang_walkerapi/__init__.py | 0 .../jaclang_walkerapi/walkerapi.py | 39 +++++++++++++++++++ examples/plugins/jaclang_walkerapi/setup.py | 23 +++++++++++ 3 files changed, 62 insertions(+) create mode 100644 examples/plugins/jaclang_walkerapi/jaclang_walkerapi/__init__.py create mode 100644 examples/plugins/jaclang_walkerapi/jaclang_walkerapi/walkerapi.py create mode 100644 examples/plugins/jaclang_walkerapi/setup.py diff --git a/examples/plugins/jaclang_walkerapi/jaclang_walkerapi/__init__.py b/examples/plugins/jaclang_walkerapi/jaclang_walkerapi/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/examples/plugins/jaclang_walkerapi/jaclang_walkerapi/walkerapi.py b/examples/plugins/jaclang_walkerapi/jaclang_walkerapi/walkerapi.py new file mode 100644 index 000000000..015b11033 --- /dev/null +++ b/examples/plugins/jaclang_walkerapi/jaclang_walkerapi/walkerapi.py @@ -0,0 +1,39 @@ +from jaclang.plugin.default import hookimpl +from jaclang.plugin.spec import ArchBound, WalkerArchitype, DSFunc + +from dataclasses import dataclass +from functools import wraps +from typing import Type, Callable + + +class JacFeature: + @staticmethod + @hookimpl + def make_walker( + on_entry: list[DSFunc], on_exit: list[DSFunc] + ) -> Callable[[type], type]: + """Create a walker architype.""" + + def decorator(cls: Type[ArchBound]) -> Type[ArchBound]: + """Decorate class.""" + cls = dataclass(eq=False)(cls) + for i in on_entry + on_exit: + i.resolve(cls) + arch_cls = WalkerArchitype + if not issubclass(cls, arch_cls): + cls = type(cls.__name__, (cls, arch_cls), {}) + cls._jac_entry_funcs_ = on_entry + cls._jac_exit_funcs_ = on_exit + inner_init = cls.__init__ + + @wraps(inner_init) + def new_init(self: ArchBound, *args: object, **kwargs: object) -> None: + inner_init(self, *args, **kwargs) + arch_cls.__init__(self) + + cls.__init__ = new_init + print("IM IN THE PLUGIN YO!") + return cls + + print("IM IN THE PLUGIN YO!") + return decorator diff --git a/examples/plugins/jaclang_walkerapi/setup.py b/examples/plugins/jaclang_walkerapi/setup.py new file mode 100644 index 000000000..4fa28c255 --- /dev/null +++ b/examples/plugins/jaclang_walkerapi/setup.py @@ -0,0 +1,23 @@ +"""Jaclang setup file.""" +from __future__ import annotations + +from setuptools import find_packages, setup # type: ignore + + +VERSION = "0.0.1" + +setup( + name="jaclang-walkerapi", + version=VERSION, + packages=find_packages(include=["jaclang_walkerapi", "jaclang_walkerapi.*"]), + install_requires=[], + package_data={ + "": ["*.ini"], + }, + entry_points={ + "jac": ["walkerapi = jaclang_walkerapi.walkerapi:JacFeature"], + }, + author="Jason Mars", + author_email="jason@jaseci.org", + url="https://github.com/Jaseci-Labs/jaclang", +)