gen_base.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- python -*-
00003 # -*- condig shift_jis -*-
00004 #
00005 #  @file gen_base.py
00006 #  @brief rtc-template source code generator base class
00007 #  @date $Date: 2007/01/11 07:43:16 $
00008 #  @author Noriaki Ando <n-ando@aist.go.jp>
00009 # 
00010 #  Copyright (C) 2005
00011 #      Task-intelligence Research Group,
00012 #      Intelligent Systems Research Institute,
00013 #      National Institute of
00014 #          Advanced Industrial Science and Technology (AIST), Japan
00015 #      All rights reserved.
00016 # 
00017 #  $Id: gen_base.py 775 2008-07-28 16:14:45Z n-ando $
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:      # overwrite: No
00089                         return
00090 
00091                 if not lines:  # overwrite: Yes
00092                         t = yat.Template(temp_txt)
00093                         taged_txt = t.generate(self.data)
00094                 else:          # overwrite: Merge mode
00095                         taged_txt = lines
00096 
00097                 # replace tags
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


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:04