- Timestamp:
- Oct 18, 2019, 10:35:56 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-259_genericise_backups/tools/backups/utils/config_parse_helper.py
r40945 r40949 41 41 def __init__(self, section_name, options: [ConfigSchemaLine]): 42 42 self.section_name = section_name 43 self.options = {} 43 44 self._process_options(options) 44 45 45 46 def _process_options(self, options): 46 self.options = {}47 47 if options is None: 48 48 return 49 49 if type(options) is not list: 50 raise ValidationError( "ValidationError: ConfigSchemaoptions must be '[ConfigSchemaLine]' or 'None'")50 raise ValidationError(f"ValidationError: {self.__class__.__name__} options must be '[ConfigSchemaLine]' or 'None'") 51 51 if len(options) == 0: 52 52 return … … 62 62 def get_schema_line(self, key: str) -> ConfigSchemaLine: 63 63 if not self.key_in_options(key): 64 raise KeyError(f"KeyError: '{key}' is not present in schema ")64 raise KeyError(f"KeyError: '{key}' is not present in schema section") 65 65 return self.options[key] 66 66 67 67 def __str__(self): 68 68 options_strs = [ o.__str__() for o in self.options] 69 return (f'{self.__class__.__name__} with {options_strs}') 69 return (f'{self.__class__.__name__} named "{self.section_name}" with {options_strs}') 70 71 72 class ConfigSchema(): 73 74 def __init__(self, sections: [ConfigSchemaSection]): 75 self.sections = {} 76 self._process_sections(sections) 77 78 def _process_sections(self, sections): 79 if sections is None: 80 return 81 if type(sections) is not list: 82 raise ValidationError(f"ValidationError: {self.__class__.__name__} sections must be '[ConfigSchemaSection]' or 'None'") 83 if len(sections) == 0: 84 return 85 for sec in sections: 86 self.add_section(sec) 87 88 def add_section(self, section: ConfigSchemaSection): 89 self.sections[section.section_name] = section 90 91 def section_exists(self, section_name: str) -> bool: 92 return section_name in self.sections.keys() 93 94 def get_schema_section(self, section_name: str) -> ConfigSchemaSection: 95 if not self.section_exists(section_name): 96 raise KeyError(f"KeyError: '{section_name}' is not present in schema") 97 return self.sections[section_name] 98 99 def __str__(self): 100 section_strs = [ o.__str__() for o in self.sections] 101 return (f'{self.__class__.__name__} with {section_strs}') 70 102 71 103 … … 76 108 77 109 78 def parse_config(config: ConfigParser(), schema: ConfigSchemaSection) -> dict:110 def _parse_config_section(config: ConfigParser(), schema: ConfigSchemaSection) -> dict: 79 111 """For a given config it will extract all values defined by the schema 80 112 and return them as a dictionary of the keys and the assoicated value as the … … 87 119 csl = schema.get_schema_line(csl_key) 88 120 121 # Parses each line 89 122 if config.has_option(section, csl_key): 90 123 if csl.expected_type is bool: … … 115 148 116 149 return parsed_items 150 151 152 # def parse_config(config: ConfigParser(), schema: ConfigSchemaSection) -> dict: 153 # return _parse_config_section(config, schema) 154 155 def _parse_config(config: ConfigParser(), schema: ConfigSchema) -> dict: 156 parsed_items = {} 157 print(config) 158 for c in config: 159 print(c) 160 161 for css_name in schema.sections: 162 print(css_name) 163 css = schema.get_schema_section(css_name) 164 print(css) 165 166 # if config.has_section(css_name): 167 parsed_section = _parse_config_section(config, css) 168 parsed_items[css_name] = parsed_section 169 # else: 170 # raise ConfigError(config, f"ConfigError: Expected section '{css_name}' from schema not present in config") 171 172 return parsed_items 173 174 175 class ParsedConfig(): 176 177 def __init__(self, config: ConfigParser, schema: ConfigSchema): 178 self._section_dict = _parse_config(config, schema) 179 180 def has_section(self, section_name): 181 return section_name in self._section_dict 182 183 def get_section(self, section_name): 184 if self.has_section(section_name): 185 return self._section_dict[section_name] 186 return None
Note:
See TracChangeset
for help on using the changeset viewer.
