From c5a2ac3e24448a21217b09de389afef12ff9268a Mon Sep 17 00:00:00 2001 From: mrzhou Date: Sat, 28 Feb 2026 12:07:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MANIFEST.in | 4 ++-- README.md | 19 ++++++++++++++- enjoy.py => enjoy/__init__.py | 28 ++++++++++++---------- enjoy-5.2.2.jar => enjoy/enjoy-5.2.2.jar | Bin {templates => enjoy/templates}/index.html | 0 pyproject.toml | 4 ++-- setup.py | 6 ++--- 7 files changed, 40 insertions(+), 21 deletions(-) rename enjoy.py => enjoy/__init__.py (85%) rename enjoy-5.2.2.jar => enjoy/enjoy-5.2.2.jar (100%) rename {templates => enjoy/templates}/index.html (100%) diff --git a/MANIFEST.in b/MANIFEST.in index aea1b1b..86ea38b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ -include enjoy-5.2.2.jar -recursive-include templates * +include enjoy/enjoy-5.2.2.jar +recursive-include enjoy/templates * include README.md include LICENSE \ No newline at end of file diff --git a/README.md b/README.md index b4a5bb9..7ad4d83 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,28 @@ jfinal-enjoy 5.2.2 的python 3.9.0 实现,基于jpype1实现。 python port by mrzhou@miw.cn ### 使用样例 + +#### 示例1:使用项目内置的jar文件(推荐) ```python from enjoy import Enjoy 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的绝对/相对路径 TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录(如index.html放在这个目录下) diff --git a/enjoy.py b/enjoy/__init__.py similarity index 85% rename from enjoy.py rename to enjoy/__init__.py index 7f6cbee..a8ccaf5 100644 --- a/enjoy.py +++ b/enjoy/__init__.py @@ -12,11 +12,17 @@ class Enjoy: _jvm_started: bool = False _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实例""" if cls._instance is None: 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._init_jvm() cls._instance._init_engine() # 初始化JFinal Engine @@ -26,14 +32,12 @@ class Enjoy: """启动JVM并加载enjoy-5.2.2.jar""" if not self._jvm_started: try: - jvm_path = jpype.getDefaultJVMPath() - # 修复:所有位置参数在前,关键字参数在后 + # 使用JPype1的自动类路径管理,让JPype1自己处理其支持库 jpype.startJVM( - jvm_path, # 位置参数1 - "-ea", # 位置参数2 - f"-Djava.class.path={self.jar_path}", # 位置参数3 - "-Dfile.encoding=UTF-8", # 位置参数4(移到关键字参数前) - convertStrings=False # 关键字参数(必须在最后) + "-ea", + f"-Djava.class.path={self.jar_path}", + "-Dfile.encoding=UTF-8", + convertStrings=False ) self._jvm_started = True print(f"JVM启动成功 | 工作目录: {jpype.java.lang.System.getProperty('user.dir')}") @@ -123,14 +127,12 @@ class Enjoy: # 标准调用示例(适配enjoy-5.2.2.jar) # ------------------------------ if __name__ == "__main__": - # 1. 配置参数(必须修改为你的实际路径) - JAR_PATH = "enjoy-5.2.2.jar" # enjoy-5.2.2.jar的绝对/相对路径 + # 1. 配置参数(仅需配置模板路径,jar文件自动使用内置版本) TEMPLATE_BASE_PATH = "./templates" # 模板文件所在目录(如index.html放在这个目录下) - # 2. 创建Enjoy实例(初始化Engine) + # 2. 创建Enjoy实例(初始化Engine,自动使用内置的enjoy-5.2.2.jar) try: enjoy = Enjoy( - jar_path=JAR_PATH, template_base_path=TEMPLATE_BASE_PATH ) diff --git a/enjoy-5.2.2.jar b/enjoy/enjoy-5.2.2.jar similarity index 100% rename from enjoy-5.2.2.jar rename to enjoy/enjoy-5.2.2.jar diff --git a/templates/index.html b/enjoy/templates/index.html similarity index 100% rename from templates/index.html rename to enjoy/templates/index.html diff --git a/pyproject.toml b/pyproject.toml index 595661c..00b9b5a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,11 +4,11 @@ build-backend = "setuptools.build_meta" [project] name = "py-enjoy" -version = "0.1.0" +version = "0.1.2" authors = [ { 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" requires-python = ">=3.9" classifiers = [ diff --git a/setup.py b/setup.py index 967d5f3..d594e0f 100644 --- a/setup.py +++ b/setup.py @@ -2,18 +2,18 @@ from setuptools import setup, find_packages setup( name="py-enjoy", - version="0.1.0", + version="0.1.2", packages=find_packages(), include_package_data=True, package_data={ - "": ["*.jar", "templates/*"] + "enjoy": ["*.jar", "templates/*"] }, install_requires=[ "jpype1>=1.4.0" ], author="mrzhou", 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_content_type="text/markdown", python_requires=">=3.9",