| 4 | |
| 5 | |
| 6 | class ArgumentParserX(argparse.ArgumentParser): |
| 7 | def __init__(self, vis, **kwargs): |
| 8 | super().__init__(**kwargs) |
| 9 | self.add_argument("config", type=str) |
| 10 | if not vis: |
| 11 | self.add_argument("-run_ros", '--run_ros', action='store_true', default=False) |
| 12 | |
| 13 | def parse_args(self, args=None, namespace=None): |
| 14 | _args = self.parse_known_args(args, namespace)[0] |
| 15 | file_args = argparse.Namespace() |
| 16 | file_args = self.parse_config_yaml(_args.config, file_args) |
| 17 | file_args = self.convert_to_namespace(file_args) |
| 18 | for ckey, cvalue in file_args.__dict__.items(): |
| 19 | try: |
| 20 | self.add_argument('--' + ckey, type=type(cvalue), |
| 21 | default=cvalue, required=False) |
| 22 | except argparse.ArgumentError: |
| 23 | continue |
| 24 | _args = super().parse_args(args, namespace) |
| 25 | return _args |
| 26 | |
| 27 | def parse_config_yaml(self, yaml_path, args=None): |
| 28 | |
| 29 | with open(yaml_path, 'r') as f: |
| 30 | configs = yaml.load(f, Loader=yaml.FullLoader) |
| 31 | |
| 32 | if configs is not None: |
| 33 | base_config = configs.get('base_config') |
| 34 | if base_config is not None: |
| 35 | base_config = self.parse_config_yaml(configs["base_config"]) |
| 36 | if base_config is not None: |
| 37 | configs = self.update_recursive(base_config, configs) |
| 38 | else: |
| 39 | raise FileNotFoundError("base_config specified but not found!") |
| 40 | |
| 41 | return configs |
| 42 | |
| 43 | def convert_to_namespace(self, dict_in, args=None): |
| 44 | if args is None: |
| 45 | args = argparse.Namespace() |
| 46 | for ckey, cvalue in dict_in.items(): |
| 47 | if ckey not in args.__dict__.keys(): |
| 48 | args.__dict__[ckey] = cvalue |
| 49 | |
| 50 | return args |
| 51 | |
| 52 | def update_recursive(self, dict1, dict2): |
| 53 | for k, v in dict2.items(): |
| 54 | if k not in dict1: |
| 55 | dict1[k] = dict() |
| 56 | if isinstance(v, dict): |
| 57 | self.update_recursive(dict1[k], v) |
| 58 | else: |
| 59 | dict1[k] = v |
| 60 | return dict1 |
| 61 | |
| 62 | |
| 63 | def get_parser(vis=False): |
no outgoing calls
no test coverage detected