Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 import os
00021 import re
00022 import time
00023 import yat
00024 import StringIO
00025 class gen_base:
00026
00027 def check_overwrite(self, fname, wmode="w"):
00028 """
00029 Check file exist or not.
00030 """
00031 msg = " already exists. Overwrite or merge? (y/n/m)"
00032 if (os.access(fname, os.F_OK)):
00033 ans = raw_input("\"" + fname + "\"" + msg)
00034 if (ans == "y" or ans == "Y"):
00035 return file(fname, wmode), None
00036 elif (ans == "m" or ans == "M"):
00037 f = file(fname, "r")
00038 lines = []
00039 for l in f.readlines():
00040 lines.append(l.rstrip())
00041 f.close()
00042 oldfname = fname + ".old." + time.strftime("%y%m%d%H%M%S")
00043 os.rename(fname, oldfname)
00044 return file(fname, wmode), lines
00045 else:
00046 return None, None
00047 else:
00048 return file(fname, wmode), None
00049 return
00050
00051 def replace_tags(self, lines, data):
00052 in_tag = False
00053 tag_name = ""
00054 ret_lines = ""
00055
00056 for l in lines:
00057 m = re.search("<rtc-template block=\"(.*?)\">", l)
00058 if m:
00059 in_tag = True
00060 tag_name = m.group(1)
00061 ret_lines += l + "\n"
00062 continue
00063
00064 m = re.search("</rtc-template>", l)
00065 if m:
00066 in_tag = False
00067 if data.has_key(tag_name):
00068 ret_lines += data[tag_name] + "\n"
00069 ret_lines += l + "\n"
00070 tag_name = ""
00071 continue
00072
00073 if in_tag != True:
00074 ret_lines += l + "\n"
00075
00076 return ret_lines
00077
00078
00079 def gen_tags(self, tags):
00080 for key in tags.keys():
00081 t = yat.Template(tags[key])
00082 text=t.generate(self.data)
00083 tags[key] = text
00084 return
00085
00086 def gen(self, fname, temp_txt, data, tags):
00087 f, lines = self.check_overwrite(fname)
00088 if not f:
00089 return
00090
00091 if not lines:
00092 t = yat.Template(temp_txt)
00093 taged_txt = t.generate(self.data)
00094 else:
00095 taged_txt = lines
00096
00097
00098 gen_txt = self.replace_tags(taged_txt.split("\n"), tags)
00099 f.write(gen_txt)
00100 f.close()
00101 print " File \"" + fname + "\"" " was generated."
00102 return