PCM_Report/test_global_params.py

52 lines
1.7 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
测试全局参数功能
"""
import json
from pathlib import Path
def test_global_params():
# 读取配置文件
config_file = Path("default.json")
if config_file.exists():
with open(config_file, 'r', encoding='utf-8') as f:
config = json.load(f)
# 检查全局参数配置
if "globalParameters" in config:
params = config["globalParameters"].get("parameters", {})
print("当前全局参数:")
for key, value in params.items():
print(f" {key}: {value}")
else:
print("配置中没有全局参数部分")
# 测试@变量替换示例
print("\n测试文本替换示例:")
test_texts = [
"工单号: @work_order_no",
"零件号: @part_no, 执行人: @executor",
"工序: @process_no",
"这是普通文本,没有参数引用"
]
# 模拟参数替换
for text in test_texts:
result = text
if "@" in text and "globalParameters" in config:
params = config["globalParameters"].get("parameters", {})
import re
pattern = r'@(\w+)'
matches = re.findall(pattern, text)
for param_name in matches:
if param_name in params:
result = result.replace(f'@{param_name}', params[param_name])
print(f" 原文: {text}")
print(f" 结果: {result}")
else:
print("找不到 default.json 配置文件")
if __name__ == "__main__":
test_global_params()