- 创建项目目录结构和主要文件 - 实现 Docker 和 Nginx 配置生成的基本功能 - 添加命令行参数解析和默认行为逻辑 - 实现配置文件读取和解析功能 - 添加 Jinja2 模板渲染功能 - 创建 Nginx 和 Docker Compose 配置模板
14 lines
634 B
Python
14 lines
634 B
Python
from pathlib import Path
|
|
from dataclasses import asdict, dataclass
|
|
from jinja2 import Environment, FileSystemLoader, Template
|
|
|
|
def render_dataclass(configs: list[dataclass], tmp_path: Path, out_path: Path, other = None) -> str:
|
|
"from dataclasses to jinja template"
|
|
env = Environment(loader=FileSystemLoader(str(tmp_path.parent),
|
|
encoding='utf-8'))
|
|
template: Template = env.get_template(str(tmp_path.name))
|
|
data = [asdict(config) for config in configs]
|
|
res = template.render({'autoconfigs': data}, **other)
|
|
out_path.write_text(res, encoding='utf-8')
|
|
return res
|