用python 脚本将protocol.json中的json按照templete.md模版生成,结果在protocol.md中
Python:
#!/usr/bin/python # -*- coding: UTF-8 -*- import json import sys import os reload(sys) sys.setdefaultencoding("utf-8") CONFIG_FILE = 'protocol.json' MD_FILE = 'protocol.md' TEMPLATE_FILE = 'templete.md' TEMPLATE_STR_ON = ' on:integer, 1、{classNameCN}开;0、{classNameCN}关。可缺省。' def writeDoc(item): for k in item.keys(): classname_cn = '' #去除name if 'name' in json.dumps(item[k]): classname_cn = item[k]['name'] del item[k]['name'] values = json.dumps(item[k]); with open(TEMPLATE_FILE,'rw+') as template_file: templateContent = template_file.readlines() resultFile = open(MD_FILE,'a+'); print(templateContent); for i in templateContent: if '{classNameCN}' in i: i = i.replace('{classNameCN}',classname_cn) if '{className}' in i: i = i.replace('{className}',k) if '{classValue}' in i: i = i.replace('{classValue}',values) if '{property}' in i: i = i.replace('{property}',writeSummary(item[k],classname_cn)) #写说明 resultFile.write(i) resultFile.close() template_file.close(); def writeSummary(parm,classname_cn=''): result_str = '' for k in parm.keys(): if(k == 'on'): templete_str = TEMPLATE_STR_ON.replace('{classNameCN}',classname_cn) result_str = result_str+templete_str else: temp_str = "\n * " + k+":"+type(parm[k]).__name__ result_str = result_str + temp_str return result_str if __name__ == '__main__': if os.path.exists(CONFIG_FILE): fileContent = open(CONFIG_FILE).read() # 清空文件 with open(MD_FILE, 'wb+') as file: file.close(); jsonDic = json.loads(fileContent) writeDoc(jsonDic) else: print ('json 配置文件不存在')
protocol.json
{ "Remind": { "name":"提醒", "on":1, "pd":30 } }
templete.md
## {className} ## 获取{classNameCN}的相关信息 ### [Name] get{className} ### [Request] ```json { "body":{} } ``` ### [Response] ```json { "body":{ "code":0, "msg":"ok", "data":{classValue} } } ``` ## 设置{classNameCN}相关信息 ### [Name] set{className} ### [Request] ```json { "body":{ "data":{classValue} } } ``` ### [Response] ```json { "body":{ "code":0, "msg":"ok", } } ``` ## report{classNameCN}信息变化 ### [Name] on{className} ### [Report] ```json { "body":{ "data":{classValue} } } ``` ### [Parameters] * {className} 结构说明: * {property} * 具体的json schema 可参考:<br/> http://rcp-schema.ecouser.net/Data/Config/{className}.json