changeset 13:9bea91fdd283

context_creator: create Code ID
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Mon, 05 Oct 2015 16:50:37 +0900
parents dac62c00581b
children af04833f7667
files context_creator/context_creator.py
diffstat 1 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/context_creator/context_creator.py	Mon Oct 05 16:50:37 2015 +0900
@@ -0,0 +1,67 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+import sys
+import argparse
+import re
+
+# parse arguments and return arguments list.
+def get_args():
+    parser = argparse.ArgumentParser(
+    formatter_class=argparse.RawDescriptionHelpFormatter,
+    description="""\
+Create Context for CbC code. Default output is stdout.
+    """)
+    parser.add_argument('input_file',\
+                        nargs=None,\
+                        type=str,\
+                        help='input file path.')
+    parser.add_argument('-o', dest='output',\
+                        nargs=1,\
+                        type=str,\
+                        metavar='<file>',\
+                        help='write output to <file>')
+                        
+    return parser.parse_args()
+
+# parse input file and create meta connection list
+def create_context(lines, file):
+    code_list = []
+    for l in lines:
+        # get code segment name except for stub and meta
+        if re.search(r"^ *__code",l) is not None:
+            regexed_l = re.search(r"[a-zA-Z0-9_]+ *\(",l)
+            cs_name = regexed_l.group(0).rstrip('(')
+            if re.search(r"^ *meta_*|stub$",cs_name) is None:
+                code_list.append(cs_name.capitalize())
+        
+
+    write_context(code_list, file)
+    
+
+def write_context(code_list, file):
+    file.write('/*-- created by script --*/\n')
+    file.write('enum Code {\n')
+    for c in code_list:
+        file.write('    {0:s},\n'.format(c))
+    file.write('    Exit,\n')
+    file.write('};\n')
+
+
+def main():
+    args = get_args()
+    output = sys.stdout
+    try:
+        f = open(args.input_file,'r')
+    except IOError:
+        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 = create_context(lines, output)
+    
+main()