| 1 | #! /usr/bin/env python3 |
| 2 | from argparse import ArgumentParser |
| 3 | from string import Template |
| 4 | |
| 5 | |
| 6 | def main(file_path, substitutions, in_place, participant_ids): |
| 7 | with open(file_path) as f: |
| 8 | pbtxt = Template(f.read()) |
| 9 | |
| 10 | sub_dict = {"max_queue_size": 0} |
| 11 | sub_dict["participant_ids"] = participant_ids |
| 12 | for sub in substitutions.split(","): |
| 13 | key, value = sub.split(":") |
| 14 | sub_dict[key] = value |
| 15 | |
| 16 | pbtxt = pbtxt.safe_substitute(sub_dict) |
| 17 | |
| 18 | if in_place: |
| 19 | with open(file_path, "w") as f: |
| 20 | f.write(pbtxt) |
| 21 | else: |
| 22 | print(pbtxt) |
| 23 | |
| 24 | |
| 25 | if __name__ == "__main__": |
| 26 | parser = ArgumentParser() |
| 27 | parser.add_argument("file_path", help="path of the .pbtxt to modify") |
| 28 | parser.add_argument( |
| 29 | "substitutions", |
| 30 | help="substitutions to perform, in the format variable_name_1:value_1,variable_name_2:value_2...", |
| 31 | ) |
| 32 | parser.add_argument("--in_place", "-i", action="store_true", help="do the operation in-place") |
| 33 | parser.add_argument("--participant_ids", help="Participant IDs for the model", default="") |
| 34 | args = parser.parse_args() |
| 35 | |
| 36 | main(**vars(args)) |
| 37 |