Mercurial > hg > CbC > old > CbC_scripts
comparison context_creator/context_creator.py @ 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 | |
children | af04833f7667 |
comparison
equal
deleted
inserted
replaced
12:dac62c00581b | 13:9bea91fdd283 |
---|---|
1 #!/usr/bin/env python3 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 import sys | |
5 import argparse | |
6 import re | |
7 | |
8 # parse arguments and return arguments list. | |
9 def get_args(): | |
10 parser = argparse.ArgumentParser( | |
11 formatter_class=argparse.RawDescriptionHelpFormatter, | |
12 description="""\ | |
13 Create Context for CbC code. Default output is stdout. | |
14 """) | |
15 parser.add_argument('input_file',\ | |
16 nargs=None,\ | |
17 type=str,\ | |
18 help='input file path.') | |
19 parser.add_argument('-o', dest='output',\ | |
20 nargs=1,\ | |
21 type=str,\ | |
22 metavar='<file>',\ | |
23 help='write output to <file>') | |
24 | |
25 return parser.parse_args() | |
26 | |
27 # parse input file and create meta connection list | |
28 def create_context(lines, file): | |
29 code_list = [] | |
30 for l in lines: | |
31 # get code segment name except for stub and meta | |
32 if re.search(r"^ *__code",l) is not None: | |
33 regexed_l = re.search(r"[a-zA-Z0-9_]+ *\(",l) | |
34 cs_name = regexed_l.group(0).rstrip('(') | |
35 if re.search(r"^ *meta_*|stub$",cs_name) is None: | |
36 code_list.append(cs_name.capitalize()) | |
37 | |
38 | |
39 write_context(code_list, file) | |
40 | |
41 | |
42 def write_context(code_list, file): | |
43 file.write('/*-- created by script --*/\n') | |
44 file.write('enum Code {\n') | |
45 for c in code_list: | |
46 file.write(' {0:s},\n'.format(c)) | |
47 file.write(' Exit,\n') | |
48 file.write('};\n') | |
49 | |
50 | |
51 def main(): | |
52 args = get_args() | |
53 output = sys.stdout | |
54 try: | |
55 f = open(args.input_file,'r') | |
56 except IOError: | |
57 print("cannot open file %s" % input_file) | |
58 if args.output is not None: | |
59 try: | |
60 output = open(args.output[0],'w') | |
61 except IOError: | |
62 print("cannot open file %s" % args.output) | |
63 | |
64 lines = f.readlines() | |
65 connect_list = create_context(lines, output) | |
66 | |
67 main() |