from tornado.template import Template, Loader
import os
import re
from typing import List, Optional, Callable, TypeVar
T = TypeVar("T")
ParseCallable = Callable[["TreeOperate", List[T], int], T]
def require(path):
"""
有时你可能只是需要从文件中读取到json数据,这是require函数将根据
获取到的path,返回dict对象,相当方便,该函数同样类似于json.load
:param path:
:return: dict
"""
import json
fp = open(path, "r")
data = fp.read()
fp.close()
try:
return json.loads(data)
except json.decoder.JSONDecodeError:
return {}
class GenerateCodeEngine:
"""
生成代码引擎类
使用方法::
gec = GenerateCodeEngine()
gec.catch_write("index.html", "template.html", {
"anthor": "systemlight"
})
"""
def __init__(self, template_root_path=""):
"""
构造函数
:param template_root_path: 模板根目录
"""
self.glob_content = {}
self.template_root_path = template_root_path
self.__start_match_tag = r"//start_user_code"
self.__end_match_tag = r"//end_user_code"
self.catch_match = r"{}(.*?){}".format(self.__start_match_tag, self.__end_match_tag)
@property
def start_match_tag(self):
return self.__start_match_tag
@start_match_tag.setter
def start_match_tag(self, value):
self.__start_match_tag = value
self.catch_match = r"{}(.*?){}".format(self.__start_match_tag, self.__end_match_tag)
@property
def end_match_tag(self):
return self.__end_match_tag
@end_match_tag.setter
def end_match_tag(self, value):
self.__end_match_tag = value
self.catch_match = r"{}(.*?){}".format(self.__start_match_tag, self.__end_match_tag)
def register_glob_content(self, name, value):