| 845 | |
| 846 | |
| 847 | def add_config(parser, json_config, short_flag=None, long_flag=None, **kwargs): |
| 848 | if not long_flag: |
| 849 | raise Exception('add_config calls requires long_flag parameter!') |
| 850 | |
| 851 | full_attribute_path = long_flag.split('--')[1] |
| 852 | attribute_name = full_attribute_path.split('.')[-1] |
| 853 | |
| 854 | if '.' in full_attribute_path: # embedded config! |
| 855 | embedded_in = full_attribute_path.split('.')[0: -1] |
| 856 | for level in embedded_in: |
| 857 | json_config = json_config.get(level, {}) |
| 858 | |
| 859 | if 'default' in kwargs: |
| 860 | kwargs['default'] = json_config.get(attribute_name, kwargs['default']) |
| 861 | if short_flag: |
| 862 | args = (short_flag, long_flag) |
| 863 | else: |
| 864 | args = (long_flag,) |
| 865 | parser.add_argument(*args, **kwargs) |
| 866 | |
| 867 | |
| 868 | def fix_nested_config(config): |