gen_base.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- python -*-
3 # -*- condig shift_jis -*-
4 #
5 # @file gen_base.py
6 # @brief rtc-template source code generator base class
7 # @date $Date: 2007/01/11 07:43:16 $
8 # @author Noriaki Ando <n-ando@aist.go.jp>
9 #
10 # Copyright (C) 2005
11 # Task-intelligence Research Group,
12 # Intelligent Systems Research Institute,
13 # National Institute of
14 # Advanced Industrial Science and Technology (AIST), Japan
15 # All rights reserved.
16 #
17 # $Id: gen_base.py 775 2008-07-28 16:14:45Z n-ando $
18 #
19 
20 import os
21 import re
22 import time
23 import yat
24 import StringIO
25 class gen_base:
26 
27  def check_overwrite(self, fname, wmode="w"):
28  """
29  Check file exist or not.
30  """
31  msg = " already exists. Overwrite or merge? (y/n/m)"
32  if (os.access(fname, os.F_OK)):
33  ans = raw_input("\"" + fname + "\"" + msg)
34  if (ans == "y" or ans == "Y"):
35  return file(fname, wmode), None
36  elif (ans == "m" or ans == "M"):
37  f = file(fname, "r")
38  lines = []
39  for l in f.readlines():
40  lines.append(l.rstrip())
41  f.close()
42  oldfname = fname + ".old." + time.strftime("%y%m%d%H%M%S")
43  os.rename(fname, oldfname)
44  return file(fname, wmode), lines
45  else:
46  return None, None
47  else:
48  return file(fname, wmode), None
49  return
50 
51  def replace_tags(self, lines, data):
52  in_tag = False
53  tag_name = ""
54  ret_lines = ""
55 
56  for l in lines:
57  m = re.search("<rtc-template block=\"(.*?)\">", l)
58  if m:
59  in_tag = True
60  tag_name = m.group(1)
61  ret_lines += l + "\n"
62  continue
63 
64  m = re.search("</rtc-template>", l)
65  if m:
66  in_tag = False
67  if data.has_key(tag_name):
68  ret_lines += data[tag_name] + "\n"
69  ret_lines += l + "\n"
70  tag_name = ""
71  continue
72 
73  if in_tag != True:
74  ret_lines += l + "\n"
75 
76  return ret_lines
77 
78 
79  def gen_tags(self, tags):
80  for key in tags.keys():
81  t = yat.Template(tags[key])
82  text=t.generate(self.data)
83  tags[key] = text
84  return
85 
86  def gen(self, fname, temp_txt, data, tags):
87  f, lines = self.check_overwrite(fname)
88  if not f: # overwrite: No
89  return
90 
91  if not lines: # overwrite: Yes
92  t = yat.Template(temp_txt)
93  taged_txt = t.generate(self.data)
94  else: # overwrite: Merge mode
95  taged_txt = lines
96 
97  # replace tags
98  gen_txt = self.replace_tags(taged_txt.split("\n"), tags)
99  f.write(gen_txt)
100  f.close()
101  print " File \"" + fname + "\"" " was generated."
102  return
def gen(self, fname, temp_txt, data, tags)
Definition: gen_base.py:86
def check_overwrite(self, fname, wmode="w")
Definition: gen_base.py:27
def gen_tags(self, tags)
Definition: gen_base.py:79
def replace_tags(self, lines, data)
Definition: gen_base.py:51


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:52