def _make_sub_file(self, sub_state_dict, file_name, enum_name): """ :param sub_state_dict: 错误码的字典 :param file_name: 存储的文件名 :param enum_name: 存储文件中的的枚举名 :return: """ enum_name = self.state_enum_name(enum_name) file_path = self._path enum_content = "" # 枚举的名字 template_file_content = self._template_file_content.replace( self._root_template, enum_name) # 枚举的头部 re_header_rule = r"%s.*%s" % (FileMaker._root_header_start, FileMaker._root_header_end) h = Util.re_search(re_header_rule, template_file_content) if h: self._template_file_content = template_file_content \ .replace(FileMaker._root_header_start, "") \ .replace(FileMaker._root_header_end, "") # 枚举的内容体 re_content_rule = r"%s(.*)%s" % (FileMaker._root_content_start, FileMaker._root_content_end) c = Util.re_search(re_content_rule, template_file_content) if c: for (k, v) in sub_state_dict.items(): transform = self._transform_config.get(v) if transform: k = transform content = c.group(1) \ .replace(FileMaker._root_key, self.state_key_name(k)) \ .replace(FileMaker._root_value, "%d" % v) \ .replace("\n", "", -1) enum_content = "%s%s\n" % (enum_content, content) template_file_content = Util.re_sub(re_content_rule, enum_content, template_file_content) self._write_to_file(template_file_content, "%s/%s" % (file_path, file_name), "w")
def make_file(self, root_name=FileMaker._root_name, enum_name=FileMaker._root_name, state_dict=None): Util.rm_file("%s/%s" % (self._path, self.file_name(""))) template_file_content = self._template_file_content # 枚举的头部 re_header_rule = r"%s(.*)%s" % (FileMaker._root_header_start, FileMaker._root_header_end) h = Util.re_search(re_header_rule, template_file_content) header = "" if h: self._template_file_content = template_file_content.replace( h.group(0), "") header = h.group(1).strip() super().make_file(root_name, enum_name, state_dict) with open("%s/%s" % (self._path, self.file_name("")), "r+") as f: old = f.read() f.seek(0) f.write("%s%s" % (header, old))