重构项目
This commit is contained 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
|
||||
19
README.md
19
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放在这个目录下)
|
||||
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
6
setup.py
6
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",
|
||||
|
||||
Reference in New Issue
Block a user