luawrap 0.1.0__tar.gz

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.
luawrap-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,62 @@
1
+ Metadata-Version: 2.4
2
+ Name: luawrap
3
+ Version: 0.1.0
4
+ Summary: Write and run Lua code directly from Python files.
5
+ License: LicenseRef-LuaWrap-Custom
6
+ Author: Your Name V011DZ
7
+ Requires-Python: >=3.8,<4.0
8
+ Classifier: License :: Other/Proprietary License
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.8
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Requires-Dist: lupa (>=2.0,<3.0)
18
+ Description-Content-Type: text/markdown
19
+
20
+ # LuaWrap
21
+
22
+ Write and run Lua code directly inside your Python files — no separate Lua install needed.
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ poetry add lupa
28
+ poetry install
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ```python
34
+ from luawrap import lua, run_lua, lua_function, LuaBlock
35
+
36
+ # Run Lua directly
37
+ lua("print('Hello from Lua!')")
38
+
39
+ # Get a return value back in Python
40
+ result = run_lua("return 2 + 2")
41
+ print(result) # 4
42
+
43
+ # Decorator style
44
+ @lua_function
45
+ def my_script():
46
+ return "print('I am a Lua function!')"
47
+
48
+ my_script()
49
+
50
+ # Context manager style
51
+ with LuaBlock() as lb:
52
+ lb.code = "print('Runs when the block exits!')"
53
+ ```
54
+
55
+ ## API
56
+
57
+ | Function | Description |
58
+ |---|---|
59
+ | `lua(code)` | Run a Lua string |
60
+ | `run_lua(code)` | Same as `lua()`, alias |
61
+ | `@lua_function` | Decorator — function returns Lua code as a string |
62
+ | `LuaBlock` | Context manager — set `lb.code` to your Lua string |
@@ -0,0 +1,43 @@
1
+ # LuaWrap
2
+
3
+ Write and run Lua code directly inside your Python files — no separate Lua install needed.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ poetry add lupa
9
+ poetry install
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```python
15
+ from luawrap import lua, run_lua, lua_function, LuaBlock
16
+
17
+ # Run Lua directly
18
+ lua("print('Hello from Lua!')")
19
+
20
+ # Get a return value back in Python
21
+ result = run_lua("return 2 + 2")
22
+ print(result) # 4
23
+
24
+ # Decorator style
25
+ @lua_function
26
+ def my_script():
27
+ return "print('I am a Lua function!')"
28
+
29
+ my_script()
30
+
31
+ # Context manager style
32
+ with LuaBlock() as lb:
33
+ lb.code = "print('Runs when the block exits!')"
34
+ ```
35
+
36
+ ## API
37
+
38
+ | Function | Description |
39
+ |---|---|
40
+ | `lua(code)` | Run a Lua string |
41
+ | `run_lua(code)` | Same as `lua()`, alias |
42
+ | `@lua_function` | Decorator — function returns Lua code as a string |
43
+ | `LuaBlock` | Context manager — set `lb.code` to your Lua string |
@@ -0,0 +1,19 @@
1
+ [tool.poetry]
2
+ name = "luawrap"
3
+ version = "0.1.0"
4
+ description = "Write and run Lua code directly from Python files."
5
+ authors = ["Your Name V011DZ"]
6
+ readme = "README.md"
7
+ license = "LicenseRef-LuaWrap-Custom"
8
+ packages = [{include = "luawrap", from = "src"}]
9
+
10
+ [tool.poetry.dependencies]
11
+ python = "^3.8"
12
+ lupa = "^2.0"
13
+
14
+ [tool.poetry.dev-dependencies]
15
+ pytest = "^7.0"
16
+
17
+ [build-system]
18
+ requires = ["poetry-core"]
19
+ build-backend = "poetry.core.masonry.api"
@@ -0,0 +1,11 @@
1
+ """
2
+ LuaWrap - Write and run Lua code directly from Python files!
3
+
4
+ Import:
5
+ from luawrap import lua, run_lua, lua_function, LuaBlock
6
+ """
7
+
8
+ from .main import lua, run_lua, lua_function, LuaBlock
9
+
10
+ __version__ = "0.1.0"
11
+ __all__ = ["lua", "run_lua", "lua_function", "LuaBlock"]
@@ -0,0 +1,98 @@
1
+ """
2
+ LuaWrap core - internal implementation.
3
+ Import from the top-level package instead:
4
+ from luawrap import lua, run_lua, lua_function, LuaBlock
5
+ """
6
+
7
+ from functools import wraps
8
+ from typing import Optional
9
+
10
+ try:
11
+ from lupa import LuaRuntime
12
+ except ImportError:
13
+ raise ImportError(
14
+ "\n[LuaWrap] The 'lupa' package is not installed!\n"
15
+ "Fix it by running:\n"
16
+ " poetry add lupa\n"
17
+ )
18
+
19
+
20
+ _runtime: Optional[LuaRuntime] = None
21
+
22
+
23
+ def _get_runtime() -> LuaRuntime:
24
+ """Get (or create) the shared Lua runtime."""
25
+ global _runtime
26
+ if _runtime is None:
27
+ _runtime = LuaRuntime(unpack_returned_tuples=True)
28
+ return _runtime
29
+
30
+
31
+ def run_lua(code: str) -> object:
32
+ """
33
+ Run a string of Lua code and return the result.
34
+
35
+ Example:
36
+ run_lua("print('Hello from Lua!')")
37
+ result = run_lua("return 2 + 2")
38
+ print(result) # 4
39
+ """
40
+ rt = _get_runtime()
41
+ return rt.execute(code)
42
+
43
+
44
+ def lua(code: str) -> object:
45
+ """
46
+ Shorthand for run_lua(). Run Lua code from a Python string.
47
+
48
+ Example:
49
+ lua("print('Hello!')")
50
+ """
51
+ return run_lua(code)
52
+
53
+
54
+ def lua_function(func):
55
+ """
56
+ Decorator that turns a Python function into a Lua runner.
57
+ The function should return a string of Lua code when called.
58
+
59
+ Example:
60
+ @lua_function
61
+ def say_hello():
62
+ return "print('Hello from a Lua function!')"
63
+
64
+ say_hello()
65
+ """
66
+ @wraps(func)
67
+ def wrapper(*args, **kwargs):
68
+ lua_code = func(*args, **kwargs)
69
+ if not isinstance(lua_code, str):
70
+ raise TypeError(
71
+ "[LuaWrap] @lua_function '{}' must return a string of Lua code, got {} instead.".format(
72
+ func.__name__, type(lua_code).__name__
73
+ )
74
+ )
75
+ return run_lua(lua_code)
76
+ return wrapper
77
+
78
+
79
+ class LuaBlock:
80
+ """
81
+ Context manager for writing a block of Lua code.
82
+ Set lb.code inside the with-block and it runs automatically on exit.
83
+
84
+ Example:
85
+ with LuaBlock() as lb:
86
+ lb.code = "print('Runs when the block exits!')"
87
+ """
88
+
89
+ def __init__(self):
90
+ self.code: str = ""
91
+
92
+ def __enter__(self):
93
+ return self
94
+
95
+ def __exit__(self, exc_type, exc_val, exc_tb):
96
+ if exc_type is None and self.code.strip():
97
+ run_lua(self.code)
98
+ return False