changeset 2:81da5b2c3382

add -o option(output to file)
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Wed, 29 Jul 2015 18:12:38 +0900
parents 4a172166e6c3
children 62d7512f2bf1
files meta_connector/meta_connector.py
diffstat 1 files changed, 17 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/meta_connector/meta_connector.py	Wed Jul 29 00:13:12 2015 +0900
+++ b/meta_connector/meta_connector.py	Wed Jul 29 18:12:38 2015 +0900
@@ -7,7 +7,7 @@
 
 # parse arguments and return arguments list.
 def get_args():
-    parser = argparse.ArgumentParser()
+    parser = argparse.ArgumentParser(description='Parse meta connect syntax. Default output is stdout.')
     parser.add_argument('input_file',\
                         nargs=None,\
                         type=str,\
@@ -45,7 +45,7 @@
     for l in lines:
         if l.find("meta_connect") >= 0:
             comment_out = True
-            print('/* -- meta connector --')
+            file.write('/* -- meta connector --\n')
         
         for meta_index, meta in enumerate(list):
             if re.search(r" *__code +{0:s} *\(".format(meta[1].strip()),l) is not None:
@@ -58,31 +58,38 @@
                 pass
             # connect to meta code segment
             elif re.search(r" *goto +\w+\(",l) is not None:
-                print("/*-- connected by script */")
-                print("// "+l, end='')
+                file.write("/*-- connected by script */\n")
+                file.write("// "+l)
                 ll = re.split(r"\(|\)", l)
-                print("goto {0:s}(context, {1:s}, {2:s});".format(list[meta_index][0],\
+                file.write("goto {0:s}(context, {1:s}, {2:s});\n".format(list[meta_index][0],\
                                                                   ll[1],\
                                                                   re.split(" +",ll[0].strip())[1].capitalize()))
-                print("/* connected by script --*/")
+                file.write("/* connected by script --*/\n")
                 continue
             if l.find('}') >= 0:
                 target_cs = False
 
-        print(l, end='')
+        file.write(l)
 
         if comment_out and l.find(')') >= 0:
             comment_out = False
-            print('-- meta connector --*/')
+            file.write('-- meta connector --*/\n')
 
 def main():
     args = get_args()
+    output = sys.stdout
     try:
         f = open(args.input_file,'r')
     except IOError:
-        print("cannot read file %s" % input_file)
+        print("cannot open file %s" % input_file)
+    if args.output is not None:
+        try:
+            output = open(args.output[0],'w')
+        except IOError:
+            print("cannot open file %s" % args.output)
+        
     lines = f.readlines()
     connect_list = parse_meta_syntax(lines)
-    connect_meta(lines, connect_list, args.output)
+    connect_meta(lines, connect_list, output)
     
 main()