root / scout / libscout / generate_behavior_lists.py @ 4bdd00ba
History | View | Annotate | Download (3.69 KB)
1 |
#!/usr/bin/python
|
---|---|
2 |
|
3 |
# Usage:
|
4 |
#
|
5 |
# python generate_behavior_lists.py
|
6 |
#
|
7 |
# Parses to use only the final folder and file name for each source file.
|
8 |
|
9 |
import sys |
10 |
import os |
11 |
|
12 |
AUTOGEN_HEADER = '''/**
|
13 |
******************************************************************************
|
14 |
* WARNING: THIS IS AN AUTOGENERATED FILE!
|
15 |
*
|
16 |
* Editing this file is USELESS. It will be overwritten during the build.
|
17 |
* To properly edit this file, edit its corresponding *.template.* file, which
|
18 |
* a script run by CMake uses to generate this file.
|
19 |
*
|
20 |
* This file was generated via rules in src/generate_behavior_lists.py.
|
21 |
*
|
22 |
* Please see that file for a description of syntax and procedure for
|
23 |
* creating auto-generated files.
|
24 |
*
|
25 |
* THIS WARNING IS LONG TO MAKE SURE IT GETS YOUR ATTENTION!
|
26 |
* THANK YOU.
|
27 |
*****************************************************************************
|
28 |
*/
|
29 |
|
30 |
'''
|
31 |
|
32 |
# These are the codes this script will replace in template files.
|
33 |
FULL_PATH_CODE = 'AUTOGEN_BEHAVIOR_FULL_PATH'
|
34 |
LAST_PATH_CODE = 'AUTOGEN_BEHAVIOR_LAST_PATH'
|
35 |
BASE_NAME_CODE = 'AUTOGEN_BEHAVIOR_BASE_NAME'
|
36 |
|
37 |
# Different ways to refer to files in behaviors/ and test_behaviors/.
|
38 |
full_paths = [] |
39 |
last_paths = [] |
40 |
base_names = [] |
41 |
|
42 |
# Mapping of generated files to their templates
|
43 |
R_FILES = {'src/BehaviorList.cpp' : 'src/BehaviorList.template.cpp', |
44 |
'src/BehaviorList.h' : 'src/BehaviorList.template.h'} |
45 |
|
46 |
# Parse the src/behaviors and src/test_behaviors folders
|
47 |
cur_dir = os.getcwd() |
48 |
for path, dirname, fnames in os.walk('src'): |
49 |
if path == 'src/behaviors' or path == 'src/test_behaviors': |
50 |
path_parts = path.split('/')
|
51 |
|
52 |
for f in sorted(fnames): |
53 |
# The pause_scout behavior needs to go first!
|
54 |
if f.endswith('.h') and 'pause_scout' in f: |
55 |
full_paths.insert(0, os.path.join(cur_dir, path, f))
|
56 |
last_paths.insert(0, os.path.join(path_parts[-1], f)) |
57 |
base_names.insert(0, f.split('.')[0]) |
58 |
# Everything else goes in alphabetical order
|
59 |
elif f.endswith('.h'): |
60 |
full_paths.append(os.path.join(cur_dir, path, f)) |
61 |
last_paths.append(os.path.join(path_parts[-1], f))
|
62 |
base_names.append(f.split('.')[0]) |
63 |
|
64 |
# Now, a dictionary of codes to replace -> things to replace them with.
|
65 |
replacements = {FULL_PATH_CODE : full_paths, |
66 |
LAST_PATH_CODE : last_paths, |
67 |
BASE_NAME_CODE : base_names} |
68 |
|
69 |
# also simultaneously write a txt file for the GUI to read from
|
70 |
with open("../scoutsim/behaviorList.txt", "w") as listFile: |
71 |
# Write all the replacements in R_FILES
|
72 |
for outfile_name in sorted(R_FILES.keys()): |
73 |
with open(outfile_name, 'w') as outfile: |
74 |
with open(R_FILES[outfile_name], 'r') as infile: |
75 |
|
76 |
# Write an obvious header so no one will edit our nicely
|
77 |
# generated file
|
78 |
outfile.write(AUTOGEN_HEADER) |
79 |
|
80 |
for line in infile.readlines(): |
81 |
# If the line needs replacing, don't write the orgininal
|
82 |
# to the output file; rather, write many versions of the
|
83 |
# line replaced with each replacement value.
|
84 |
replaced = False
|
85 |
for r_key in replacements: |
86 |
if r_key in line: |
87 |
replaced = True
|
88 |
for b in replacements[r_key]: |
89 |
outfile.write(line.replace(r_key, b)) |
90 |
if (r_key == BASE_NAME_CODE):
|
91 |
listFile.write(b+"\n")
|
92 |
# If it doesn't need replacing, just write the original
|
93 |
if not replaced: |
94 |
outfile.write(line) |
95 |
|
96 |
|