IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 18, 2019, 10:35:56 AM (7 years ago)
Author:
fairlamb
Message:

changed the structure so that a full schema must be given, also allowed a full schema to exist

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ipp-259_genericise_backups/tools/backups/utils/config_parse_helper.py

    r40945 r40949  
    4141    def __init__(self, section_name, options: [ConfigSchemaLine]):
    4242        self.section_name = section_name
     43        self.options = {}
    4344        self._process_options(options)
    4445
    4546    def _process_options(self, options):
    46         self.options = {}
    4747        if options is None:
    4848            return
    4949        if type(options) is not list:
    50             raise ValidationError("ValidationError: ConfigSchema options must be '[ConfigSchemaLine]' or 'None'")
     50            raise ValidationError(f"ValidationError: {self.__class__.__name__} options must be '[ConfigSchemaLine]' or 'None'")
    5151        if len(options) == 0:
    5252            return
     
    6262    def get_schema_line(self, key: str) -> ConfigSchemaLine:
    6363        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")
    6565        return self.options[key]
    6666
    6767    def __str__(self):
    6868        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
     72class 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}')
    70102
    71103
     
    76108
    77109
    78 def parse_config(config: ConfigParser(), schema: ConfigSchemaSection) -> dict:
     110def _parse_config_section(config: ConfigParser(), schema: ConfigSchemaSection) -> dict:
    79111    """For a given config it will extract all values defined by the schema
    80112and return them as a dictionary of the keys and the assoicated value as the
     
    87119        csl = schema.get_schema_line(csl_key)
    88120
     121        # Parses each line
    89122        if config.has_option(section, csl_key):
    90123            if csl.expected_type is bool:
     
    115148
    116149    return parsed_items
     150
     151
     152# def parse_config(config: ConfigParser(), schema: ConfigSchemaSection) -> dict:
     153#     return _parse_config_section(config, schema)
     154
     155def _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
     175class 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.