You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Telosys代码生成器提供输入?Python集成咨询

关于Telosys模板输入与Python集成的解决方案

一、解决gen命令报错:正确提供模板输入

Telosys是模型驱动的代码生成工具,模板的输入是实体结构模型(而非具体的实体实例),核心流程是:定义实体结构 → 编写对应模板 → 通过CLI命令触发生成。

1. 正确存放模型文件

将你的Feature实体定义保存为model/feature.entity文件(model是Telosys默认模型目录,不存在则自行创建):

Feature {
  name : string;
  introduced: string;
  present: boolean;
}

2. 修正模板变量

Telosys渲染单个实体模板时,会将当前实体结构对象暴露为entity变量,因此你的模板文件(如templates/feature.vm)需调整为:

Current entity name is ${entity.name}

如果需要遍历实体属性,可使用Velocity的循环语法:

Entity: ${entity.name}
Attributes:
#foreach( $attr in $entity.attributes )
  - ${attr.name} : ${attr.type}
#end

3. 执行正确的gen命令

在命令行进入Telosys项目根目录,执行以下命令:

telosys gen -m model/feature.entity -t templates/feature.vm -o output

参数说明:

  • -m:指定模型文件路径
  • -t:指定模板文件路径
  • -o:指定输出目录(生成的文件将存放在此)

执行后,output目录会生成渲染后的结果文件。

二、集成到Python脚本的方法

Telosys基于Java开发,无官方Python API,但可通过Python的subprocess模块调用其CLI命令,支持动态生成模型后触发生成。

示例1:直接调用Telosys CLI生成

若已有固定的模型和模板,可直接在Python中执行生成命令:

import subprocess

def main():
    # 定义Telosys命令参数
    telosys_cmd = [
        "telosys",
        "gen",
        "-m", "model/feature.entity",
        "-t", "templates/feature.vm",
        "-o", "output"
    ]
    
    # 执行命令并捕获输出
    result = subprocess.run(telosys_cmd, capture_output=True, text=True)
    
    print("生成输出:")
    print(result.stdout)
    if result.stderr:
        print("错误信息:")
        print(result.stderr)

if __name__ == "__main__":
    main()

示例2:动态生成模型后调用Telosys

若需要根据Python数据动态定义实体结构,可先生成临时模型文件,再执行生成命令:

import subprocess
import os

def generate_model_file(feature_def):
    # 拼接实体结构内容
    model_content = f"{feature_def['name']} {{\n"
    for attr_name, attr_type in feature_def['attributes'].items():
        model_content += f"  {attr_name} : {attr_type};\n"
    model_content += "}\n"
    
    # 写入模型文件
    model_path = "model/dynamic_feature.entity"
    os.makedirs(os.path.dirname(model_path), exist_ok=True)
    with open(model_path, 'w') as f:
        f.write(model_content)
    return model_path

def main():
    # 动态定义实体结构
    dynamic_feature = {
        "name": "CustomFeature",
        "attributes": {
            "name": "string",
            "introduced": "string",
            "present": "boolean",
            "version": "float"  # 动态新增属性
        }
    }
    
    # 生成模型文件
    model_path = generate_model_file(dynamic_feature)
    
    # 调用Telosys生成
    telosys_cmd = [
        "telosys",
        "gen",
        "-m", model_path,
        "-t", "templates/feature.vm",
        "-o", "output"
    ]
    subprocess.run(telosys_cmd, check=True)
    print("生成完成!")

if __name__ == "__main__":
    main()

三、澄清核心误解

你之前混淆了实体结构和实体实例:

  • Telosys的模型用于定义实体的结构(如Feature有哪些属性、类型是什么),而非存储具体的实例数据(如f1.name = "My feature"这类对象)。
  • 若需求是根据具体实例数据生成文本(如批量生成Feature实例描述),更适合用Python自带的模板引擎(如Jinja2),示例如下:
from jinja2 import Template

def main():
    # 定义实例数据
    features = [
        {"name": "My feature", "present": False, "introduced": "This was my first feature"},
        {"name": "Advanced feature", "present": True, "introduced": "Added in v2.0"}
    ]
    
    # 定义模板
    template_str = """{% for f in features %}
Current feature: {{ f.name }} 
Introduced: {{ f.introduced }} | Present: {{ f.present }}
{% endfor %}"""
    
    # 渲染模板
    template = Template(template_str)
    print(template.render(features=features))

if __name__ == "__main__":
    main()

内容的提问来源于stack exchange,提问作者Har

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 03:25:01