cognihub-pygotemplate 0.0.1__cp310-cp310-macosx_11_0_x86_64.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.
- cognihub_pygotemplate-0.0.1.data/purelib/cognihub_pygotemplate/__init__.py +3 -0
- cognihub_pygotemplate-0.0.1.data/purelib/cognihub_pygotemplate/engine.py +72 -0
- cognihub_pygotemplate-0.0.1.data/purelib/cognihub_pygotemplate/librenderer.dylib +0 -0
- cognihub_pygotemplate-0.0.1.dist-info/LICENSE +21 -0
- cognihub_pygotemplate-0.0.1.dist-info/LICENSE copy +21 -0
- cognihub_pygotemplate-0.0.1.dist-info/METADATA +118 -0
- cognihub_pygotemplate-0.0.1.dist-info/RECORD +9 -0
- cognihub_pygotemplate-0.0.1.dist-info/WHEEL +5 -0
- cognihub_pygotemplate-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""go包的包装器."""
|
|
2
|
+
import ctypes
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import platform
|
|
6
|
+
from typing import Dict, Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class GoTemplateEngine:
|
|
10
|
+
"""
|
|
11
|
+
A Python interface to Go's text/template engine.
|
|
12
|
+
It relies on a pre-compiled shared library managed by the package installation process.
|
|
13
|
+
"""
|
|
14
|
+
_go_lib = None
|
|
15
|
+
_free_func = None
|
|
16
|
+
|
|
17
|
+
def __init__(self, template_content: str):
|
|
18
|
+
self._load_library()
|
|
19
|
+
self.template_content = template_content
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def _load_library(cls) -> None:
|
|
23
|
+
"""Loads the pre-compiled Go shared library."""
|
|
24
|
+
if cls._go_lib:
|
|
25
|
+
return
|
|
26
|
+
|
|
27
|
+
lib_name = "librenderer.so"
|
|
28
|
+
if platform.system() == "Windows":
|
|
29
|
+
lib_name = "renderer.dll"
|
|
30
|
+
elif platform.system() == "Darwin":
|
|
31
|
+
lib_name = "librenderer.dylib"
|
|
32
|
+
|
|
33
|
+
# 库文件应该和这个Python文件在同一个目录下
|
|
34
|
+
lib_path = os.path.join(os.path.dirname(__file__), lib_name)
|
|
35
|
+
|
|
36
|
+
if not os.path.exists(lib_path):
|
|
37
|
+
raise FileNotFoundError(
|
|
38
|
+
f"Shared library not found at {lib_path}. "
|
|
39
|
+
"The package may not have been installed correctly. "
|
|
40
|
+
"Try reinstalling with 'pip install .'"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
cls._go_lib = ctypes.CDLL(lib_path)
|
|
44
|
+
|
|
45
|
+
cls._go_lib.RenderTemplate.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
|
|
46
|
+
cls._go_lib.RenderTemplate.restype = ctypes.c_char_p
|
|
47
|
+
|
|
48
|
+
cls._go_lib.FreeString.argtypes = [ctypes.c_char_p]
|
|
49
|
+
cls._go_lib.FreeString.restype = None
|
|
50
|
+
|
|
51
|
+
cls._free_func = cls._go_lib.FreeString
|
|
52
|
+
|
|
53
|
+
def render(self, data: Dict[str, Any]) -> str:
|
|
54
|
+
"""Renders the template with the given data."""
|
|
55
|
+
if not self._go_lib:
|
|
56
|
+
raise RuntimeError("Go renderer library is not loaded.")
|
|
57
|
+
|
|
58
|
+
template_bytes = self.template_content.encode('utf-8')
|
|
59
|
+
json_data_bytes = json.dumps(data).encode('utf-8')
|
|
60
|
+
|
|
61
|
+
result_ptr = self._go_lib.RenderTemplate(template_bytes, json_data_bytes)
|
|
62
|
+
|
|
63
|
+
try:
|
|
64
|
+
rendered_string = ctypes.string_at(result_ptr).decode('utf-8')
|
|
65
|
+
finally:
|
|
66
|
+
if self._free_func and result_ptr:
|
|
67
|
+
self._free_func(result_ptr)
|
|
68
|
+
|
|
69
|
+
if rendered_string.startswith(("JSON_ERROR:", "TEMPLATE_PARSE_ERROR:", "TEMPLATE_EXECUTE_ERROR:")):
|
|
70
|
+
raise ValueError(f"Error from Go renderer: {rendered_string}")
|
|
71
|
+
|
|
72
|
+
return rendered_string
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 HUANG SIZHE
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Python-Tools
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: cognihub-pygotemplate
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A Python wrapper for Go's text/template engine via CGo
|
|
5
|
+
Author-email: hsz <hsz1273327@gmail.com>
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
License-File: LICENSE copy
|
|
13
|
+
|
|
14
|
+
# cognihub_pygotemplate
|
|
15
|
+
|
|
16
|
+
A simple, high-performance Python wrapper for Go's native `text/template` engine.
|
|
17
|
+
|
|
18
|
+
*[中文文档](README_CN.md)*
|
|
19
|
+
|
|
20
|
+
This library allows you to use Go templates (like those from Ollama) directly in your Python projects with 100% compatibility, by calling a compiled Go shared library via CGo and Python's `ctypes`.
|
|
21
|
+
|
|
22
|
+
## Prerequisites
|
|
23
|
+
|
|
24
|
+
- Python 3.10+
|
|
25
|
+
- Go compiler (1.21+ recommended) installed and available in your system's PATH
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
### From PyPI (Recommended)
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install cognihub_pygotemplate
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### From Source
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# Clone the repository
|
|
39
|
+
git clone https://github.com/hsz1273327/cognihub_pygotemplate.git
|
|
40
|
+
cd cognihub_pygotemplate
|
|
41
|
+
|
|
42
|
+
# Install the package
|
|
43
|
+
pip install .
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The library will automatically compile the Go source code during installation.
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from cognihub_pygotemplate import GoTemplateEngine
|
|
52
|
+
|
|
53
|
+
# Your Go template string
|
|
54
|
+
template_str = """
|
|
55
|
+
Hello, {{.Name}}!
|
|
56
|
+
{{if .Items}}You have {{len .Items}} items:
|
|
57
|
+
{{range .Items}}- {{.}}
|
|
58
|
+
{{end}}{{else}}You have no items.{{end}}
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
# Data to render
|
|
62
|
+
data = {
|
|
63
|
+
"Name": "World",
|
|
64
|
+
"Items": ["apple", "banana", "cherry"]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
# Create an engine instance
|
|
69
|
+
engine = GoTemplateEngine(template_str)
|
|
70
|
+
|
|
71
|
+
# Render the template
|
|
72
|
+
output = engine.render(data)
|
|
73
|
+
|
|
74
|
+
print(output)
|
|
75
|
+
|
|
76
|
+
except (RuntimeError, ValueError) as e:
|
|
77
|
+
print(f"An error occurred: {e}")
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Development Build
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
python setup.py build_py
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Build Wheels
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
# For current platform
|
|
91
|
+
python setup.py bdist_wheel
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Features
|
|
95
|
+
|
|
96
|
+
- **100% Go Template Compatibility**: Uses Go's native `text/template` engine
|
|
97
|
+
- **High Performance**: Direct CGo bindings with minimal overhead
|
|
98
|
+
- **Cross-Platform**: Supports Windows, macOS (Intel & ARM64), and Linux
|
|
99
|
+
- **Memory Safe**: Proper memory management to prevent leaks
|
|
100
|
+
- **Easy Integration**: Simple Python API that feels native
|
|
101
|
+
|
|
102
|
+
## Error Handling
|
|
103
|
+
|
|
104
|
+
The library provides clear error messages for common issues:
|
|
105
|
+
|
|
106
|
+
- `JSON_ERROR`: Invalid data format
|
|
107
|
+
- `TEMPLATE_PARSE_ERROR`: Template syntax errors
|
|
108
|
+
- `TEMPLATE_EXECUTE_ERROR`: Runtime template execution errors
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
113
|
+
|
|
114
|
+
## Contributing
|
|
115
|
+
|
|
116
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
117
|
+
|
|
118
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
cognihub_pygotemplate-0.0.1.data/purelib/cognihub_pygotemplate/__init__.py,sha256=m7jMRYxrh0lXrkjTDzymWvHEHyOaMty9MW1CJK8MPxU,68
|
|
2
|
+
cognihub_pygotemplate-0.0.1.data/purelib/cognihub_pygotemplate/engine.py,sha256=MVbb9teb4RT31OeaiQYmD5UeatQitjMlmIfz8wlQPS8,2404
|
|
3
|
+
cognihub_pygotemplate-0.0.1.data/purelib/cognihub_pygotemplate/librenderer.dylib,sha256=Xp4kakqqGz_Z3n1sW4QT1Re21Q12EbLD3Dlv1_g96kc,3101832
|
|
4
|
+
cognihub_pygotemplate-0.0.1.dist-info/LICENSE,sha256=3XBD597f722-JFXQre9mI-le0_Ct3-Jcf6d70THzErM,1068
|
|
5
|
+
cognihub_pygotemplate-0.0.1.dist-info/LICENSE copy,sha256=RbdVjM52einQjUtV_pIQV46Dh-4NV3Cy5mWQDO-j3dw,1069
|
|
6
|
+
cognihub_pygotemplate-0.0.1.dist-info/METADATA,sha256=N-g9NusJSnx9e8LRFtXVVeOwrTLi2ohoiRDKIhKotRM,2741
|
|
7
|
+
cognihub_pygotemplate-0.0.1.dist-info/WHEEL,sha256=im94mj9ajrXHvJF8-uHc2evLxe5YxO6Ibw8BaRt-Sug,111
|
|
8
|
+
cognihub_pygotemplate-0.0.1.dist-info/top_level.txt,sha256=JQidWgxvFtP5hdUE_k1A6f-P9ySD1LcxcsFCm5Zo-s4,22
|
|
9
|
+
cognihub_pygotemplate-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cognihub_pygotemplate
|