gen_base.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- python -*-
3 #
4 # @file gen_base.py
5 # @brief rtc-template source code generator base class
6 # @date $Date: 2007/01/11 07:43:16 $
7 # @author Noriaki Ando <n-ando@aist.go.jp>
8 #
9 # Copyright (C) 2005
10 # Task-intelligence Research Group,
11 # Intelligent Systems Research Institute,
12 # National Institute of
13 # Advanced Industrial Science and Technology (AIST), Japan
14 # All rights reserved.
15 #
16 # $Id: gen_base.py,v 1.4 2007/01/11 07:43:16 n-ando Exp $
17 #
18 
19 #
20 # $Log: gen_base.py,v $
21 # Revision 1.4 2007/01/11 07:43:16 n-ando
22 # A trivial fix.
23 #
24 # Revision 1.3 2005/09/08 09:24:06 n-ando
25 # - A bug fix for merge function.
26 #
27 # Revision 1.2 2005/09/06 14:37:29 n-ando
28 # rtc-template's command options and data structure for ezt (Easy Template)
29 # are changed for RTComponent's service features.
30 # Now rtc-template can generate services' skeletons, stubs and
31 # implementation files.
32 # The implementation code generation uses omniidl's IDL parser.
33 #
34 #
35 
36 import os
37 import re
38 import time
39 import ezt
40 import StringIO
41 
42 class gen_base:
43 
44  def check_overwrite(self, fname):
45  """
46  Check file exist or not.
47  """
48  msg = " already exists. Overwrite or merge? (y/n/m)"
49  if (os.access(fname, os.F_OK)):
50  ans = raw_input("\"" + fname + "\"" + msg)
51  if (ans == "y" or ans == "Y"):
52  return file(fname, "w"), None
53  elif (ans == "m" or ans == "M"):
54  f = file(fname, "r")
55  lines = []
56  for l in f.readlines():
57  lines.append(l.rstrip())
58  f.close()
59  oldfname = fname + ".old." + time.strftime("%y%m%d%H%M%S")
60  os.rename(fname, oldfname)
61  return file(fname, "w"), lines
62  else:
63  return None, None
64  else:
65  return file(fname, "w"), None
66  return
67 
68  def replace_tags(self, lines, data):
69  in_tag = False
70  tag_name = ""
71  ret_lines = ""
72 
73  for l in lines:
74  m = re.search("<rtc-template block=\"(.*?)\">", l)
75  if m:
76  in_tag = True
77  tag_name = m.group(1)
78  ret_lines += l + "\n"
79  continue
80 
81  m = re.search("</rtc-template>", l)
82  if m:
83  in_tag = False
84  if data.has_key(tag_name):
85  ret_lines += data[tag_name] + "\n"
86  ret_lines += l + "\n"
87  tag_name = ""
88  continue
89 
90  if in_tag != True:
91  ret_lines += l + "\n"
92 
93  return ret_lines
94 
95 
96  def gen_tags(self, tags):
97  for key in tags.keys():
98  s = StringIO.StringIO()
99  t = ezt.Template(compress_whitespace = 0)
100  t.parse(tags[key])
101  t.generate(s, self.data)
102  tags[key] = s.getvalue()
103  return
104 
105  def gen(self, fname, temp_txt, data, tags):
106  f, lines = self.check_overwrite(fname)
107  if not f: # overwrite: No
108  return
109 
110  if not lines: # overwrite: Yes
111  s = StringIO.StringIO()
112  t = ezt.Template(compress_whitespace = 0)
113  t.parse(temp_txt)
114  t.generate(s, data)
115  taged_txt = s.getvalue().splitlines()
116  else: # overwrite: Merge mode
117  taged_txt = lines
118 
119  # replace tags
120  gen_txt = self.replace_tags(taged_txt, tags)
121  f.write(gen_txt)
122  f.close()
123  print " File \"" + fname + "\"" " was generated."
124  return
125 
126 
127 
128 if __name__ == "__main__":
129  hoge = """
130  protected:
131  // <rtc-template block="inport_declar">
132  // </rtc-template>
133 
134  // <rtc-template block="outport_declar">
135  // </rtc-template>
136 """
137  data = {"inport_declar": " hoge;\n dara;\n munya;",
138  "outport_declar": " 1;\n 2;\n 3;"}
139  g = gen_base()
140  print g.replace_tags(hoge.splitlines(), data)
def gen(self, fname, temp_txt, data, tags)
Definition: gen_base.py:105


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:06