Mercurial > hg > CbC > old > CbC_scripts
view meta_connector/meta_connector_name.py @ 5:7af72e3e4b62
add name base meta connector
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 10 Aug 2015 21:45:18 +0900 |
parents | |
children | d9f74f325135 |
line wrap: on
line source
#!/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="""\ Parse meta connect syntax. Default output is stdout. sample) goto meta_code1|code1(arg1, arg2, ...); \t| \tV goto meta_code1(context, arg1, arg2, ..., Code1); """) 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 parse_meta_syntax(lines,file): comment_out = False target_cs = False caller_name = '' isMeta = False for i,l in enumerate(lines): regexed_l = re.search(r"[a-zA-Z0-9_]+ *\(",l) # get caller code segment name if re.search(r"^ *__code",l) is not None: caller_name = regexed_l.group(0).rstrip('(') if re.search(r"^ *meta_*",caller_name) is not None: isMeta = True else: isMeta = False file.write(l) elif not isMeta and regexed_l is not None and re.search(r"^ *goto",l): callee_name = regexed_l.group(0).rstrip('(') file.write("/*-- connected by script */\n") file.write('// '+l) file.write("goto {0:s}(context, {1:s}, {2:s});\n".format('meta_'+caller_name,\ l.split('(')[1].rsplit(')')[0],\ callee_name.capitalize())) else: file.write(l) 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 = parse_meta_syntax(lines, output) main()