重构项目

This commit is contained in:
2026-02-28 12:07:17 +08:00
parent f3c1fc98f7
commit c5a2ac3e24
7 changed files with 40 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
include enjoy-5.2.2.jar include enjoy/enjoy-5.2.2.jar
recursive-include templates * recursive-include enjoy/templates *
include README.md include README.md
include LICENSE include LICENSE

View File

@@ -4,11 +4,28 @@ jfinal-enjoy 5.2.2 的python 3.9.0 实现基于jpype1实现。
python port by mrzhou@miw.cn python port by mrzhou@miw.cn
### 使用样例 ### 使用样例
#### 示例1使用项目内置的jar文件推荐
```python ```python
from enjoy import Enjoy from enjoy import Enjoy
if __name__ == "__main__": if __name__ == "__main__":
# 1. 配置参数(必须修改为你的实际路径) # 1. 配置参数(仅需配置模板路径)
TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录如index.html放在这个目录下
# 2. 创建Enjoy实例初始化Engine自动使用内置的enjoy-5.2.2.jar
try:
enjoy = Enjoy(
template_base_path=TEMPLATE_BASE_PATH
)
```
#### 示例2指定自定义的jar文件路径
```python
from enjoy import Enjoy
if __name__ == "__main__":
# 1. 配置参数需配置jar路径和模板路径
JAR_PATH = "enjoy-5.2.2.jar" # enjoy-5.2.2.jar的绝对/相对路径 JAR_PATH = "enjoy-5.2.2.jar" # enjoy-5.2.2.jar的绝对/相对路径
TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录如index.html放在这个目录下 TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录如index.html放在这个目录下

View File

@@ -12,11 +12,17 @@ class Enjoy:
_jvm_started: bool = False _jvm_started: bool = False
_engine: Optional[Any] = None # 保存JFinal Engine实例 _engine: Optional[Any] = None # 保存JFinal Engine实例
def __new__(cls, jar_path: str, template_base_path: str = "./"): def __new__(cls, jar_path: str = None, template_base_path: str = "./"):
"""单例模式全局唯一Engine实例""" """单例模式全局唯一Engine实例"""
if cls._instance is None: if cls._instance is None:
cls._instance = super().__new__(cls) cls._instance = super().__new__(cls)
cls._instance.jar_path = os.path.abspath(jar_path) # 如果未提供jar_path使用项目内置的enjoy-5.2.2.jar
if jar_path is None:
# 获取当前文件所在目录然后拼接jar文件名
current_dir = os.path.dirname(os.path.abspath(__file__))
cls._instance.jar_path = os.path.join(current_dir, "enjoy-5.2.2.jar")
else:
cls._instance.jar_path = os.path.abspath(jar_path)
cls._instance.template_base_path = os.path.abspath(template_base_path) cls._instance.template_base_path = os.path.abspath(template_base_path)
cls._instance._init_jvm() cls._instance._init_jvm()
cls._instance._init_engine() # 初始化JFinal Engine cls._instance._init_engine() # 初始化JFinal Engine
@@ -26,14 +32,12 @@ class Enjoy:
"""启动JVM并加载enjoy-5.2.2.jar""" """启动JVM并加载enjoy-5.2.2.jar"""
if not self._jvm_started: if not self._jvm_started:
try: try:
jvm_path = jpype.getDefaultJVMPath() # 使用JPype1的自动类路径管理让JPype1自己处理其支持库
# 修复:所有位置参数在前,关键字参数在后
jpype.startJVM( jpype.startJVM(
jvm_path, # 位置参数1 "-ea",
"-ea", # 位置参数2 f"-Djava.class.path={self.jar_path}",
f"-Djava.class.path={self.jar_path}", # 位置参数3 "-Dfile.encoding=UTF-8",
"-Dfile.encoding=UTF-8", # 位置参数4移到关键字参数前 convertStrings=False
convertStrings=False # 关键字参数(必须在最后)
) )
self._jvm_started = True self._jvm_started = True
print(f"JVM启动成功 | 工作目录: {jpype.java.lang.System.getProperty('user.dir')}") print(f"JVM启动成功 | 工作目录: {jpype.java.lang.System.getProperty('user.dir')}")
@@ -123,14 +127,12 @@ class Enjoy:
# 标准调用示例适配enjoy-5.2.2.jar # 标准调用示例适配enjoy-5.2.2.jar
# ------------------------------ # ------------------------------
if __name__ == "__main__": if __name__ == "__main__":
# 1. 配置参数(必须修改为你的实际路径 # 1. 配置参数(仅需配置模板路径jar文件自动使用内置版本
JAR_PATH = "enjoy-5.2.2.jar" # enjoy-5.2.2.jar的绝对/相对路径
TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录如index.html放在这个目录下 TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录如index.html放在这个目录下
# 2. 创建Enjoy实例初始化Engine # 2. 创建Enjoy实例初始化Engine自动使用内置的enjoy-5.2.2.jar
try: try:
enjoy = Enjoy( enjoy = Enjoy(
jar_path=JAR_PATH,
template_base_path=TEMPLATE_BASE_PATH template_base_path=TEMPLATE_BASE_PATH
) )

View File

@@ -4,11 +4,11 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "py-enjoy" name = "py-enjoy"
version = "0.1.0" version = "0.1.2"
authors = [ authors = [
{ name = "mrzhou", email = "mrzhou@miw.cn" } { name = "mrzhou", email = "mrzhou@miw.cn" }
] ]
description = "jfinal-enjoy 5.2.2 的python 3.9.0 实现基于jpype1实现" description = "jfinal-enjoy 5.2.2 的python 3.9.0 实现基于jpype1实现。 实现mrzhou@miw.cn"
readme = "README.md" readme = "README.md"
requires-python = ">=3.9" requires-python = ">=3.9"
classifiers = [ classifiers = [

View File

@@ -2,18 +2,18 @@ from setuptools import setup, find_packages
setup( setup(
name="py-enjoy", name="py-enjoy",
version="0.1.0", version="0.1.2",
packages=find_packages(), packages=find_packages(),
include_package_data=True, include_package_data=True,
package_data={ package_data={
"": ["*.jar", "templates/*"] "enjoy": ["*.jar", "templates/*"]
}, },
install_requires=[ install_requires=[
"jpype1>=1.4.0" "jpype1>=1.4.0"
], ],
author="mrzhou", author="mrzhou",
author_email="mrzhou@miw.cn", author_email="mrzhou@miw.cn",
description="jfinal-enjoy 5.2.2 的python 3.9.0 实现基于jpype1实现", description="jfinal-enjoy 5.2.2 的python 3.9.0 实现基于jpype1实现。 mrzhou@miw.cn",
long_description=open("README.md").read(), long_description=open("README.md").read(),
long_description_content_type="text/markdown", long_description_content_type="text/markdown",
python_requires=">=3.9", python_requires=">=3.9",