IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 8, 2019, 11:42:36 AM (7 years ago)
Author:
fairlamb
Message:

Finished schema parsing tests

File:
1 edited

Legend:

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

    r40915 r40919  
    33from backups.utils.errors import ConfigError, ValidationError
    44
    5 # The as configs should be parsed should be pretty uniform. There's a limited
    6 # amount that can be done with the actual loading.
    7 
    8 # Each backup file should be expecting particular lines from a config. So, this
    9 # means a schema can be made for what each backup expects.
    10 
    115
    126class SpecialSchemaProcessing(Enum):
    13     """These enums define how a config line should be processed if it is a list
     7    """These enums define how processing should be applied to a string to
     8separate it out into list.
    149    """
    1510    commma_separated = 1
     
    1813
    1914class ConfigSchemaLine():
    20     """Details the name of key, whether the option is required,
    21     and type expected"""
     15    """Defines a config line by what the key is, if it is required or optional,
     16the type that the value should be processed as, and any special processing required
     17"""
    2218
    2319    def __init__(self, key: str, required: bool, expected_type, special_processing: SpecialSchemaProcessing=None):
     
    3026        self.special_processing = special_processing
    3127
     28    def __str__(self):
     29        return (f'<{self.__class__.__name__} - '
     30                f'Key:{self.key}, '
     31                f'Required:{self.required}, '
     32                f'Type:{self.expected_type}, '
     33                f'Processing: {self.special_processing}'
     34                f'>')
     35
    3236
    3337class ConfigSchema():
    34     """Defines the schema to be used"""
     38    """Defines a schema to be used. Consists of a single section name and
     39multiple config lines"""
    3540
    3641    def __init__(self, section_name, options: [ConfigSchemaLine]):
     
    5257        self.options[line.key] = line
    5358
     59    def key_in_options(self, key: str) -> bool:
     60        return key in self.options.keys()
     61
     62    def get_schema_line(self, key: str) -> ConfigSchemaLine:
     63        if not self.key_in_options(key):
     64            raise KeyError(f"KeyError: '{key}' is not present in schema")
     65        return self.options[key]
     66
     67    def __str__(self):
     68        options_strs = [ o.__str__() for o in self.options]
     69        return (f'{self.__class__.__name__} with {options_strs}')
     70
    5471
    5572def _split_string_to_list(items: str, separator=',') -> [str]:
     
    6077
    6178def parse_config(config: ConfigParser(), schema: ConfigSchema) -> dict:
    62     # attempt to parse everything as intended.
     79    """For a given config it will extract all values defined by the schema
     80and return them as a dictionary of the keys and the assoicated value as the
     81type defined by the schema
     82"""
    6383    parsed_items = {}
    6484
    6585    section = schema.section_name
    66     for csl in schema.options:
     86    for csl_key in schema.options:
     87        csl = schema.get_schema_line(csl_key)
     88        print(csl)
    6789
    68         if config.has_option(section, csl.key):
    69 
     90        if config.has_option(section, csl_key):
    7091            if csl.expected_type is bool:
    7192                parsed_items[csl.key] = config.getboolean(section, csl.key)
    72             elif csl.expected is int:
     93            elif csl.expected_type is int:
    7394                parsed_items[csl.key] = config.getint(section, csl.key)
    7495            elif csl.expected_type is float:
     
    79100                if csl.special_processing == SpecialSchemaProcessing.commma_separated:
    80101                    items = config[section][csl.key]
    81                     parsed_items[csl.key] = _split_string_to_list(items, ' ')
    82                     # parse comma_separated stuff
     102                    splitup = _split_string_to_list(items, ',')
     103                    parsed_items[csl.key] = splitup
    83104                elif csl.special_processing == SpecialSchemaProcessing.space_separated:
    84105                    items = config[section][csl.key]
    85                     parsed_items[csl.key] = _split_string_to_list(items, ',')
    86                     # space separated processing
     106                    splitup = _split_string_to_list(items, ' ')
     107                    parsed_items[csl.key] = splitup
    87108                else:
    88                     raise ConfigError(f"ConfigError: Don't know how to process special schema {csl.special_processing}")
     109                    raise ConfigError(config, f"ConfigError: Don't know how to process special schema {csl.special_processing}")
    89110            else:
    90                 raise ConfigError(f"ConfigError: Type '{csl.expected_type}' cannot be processed - need to modify {__name__}")
     111                raise ConfigError(config, f"ConfigError: Type '{csl.expected_type}' cannot be processed - need to modify {__name__}")
    91112
    92113        else:
    93114            if csl.required:
    94                 raise ConfigError(f"ConfigError: Key '{csl.key}' is required but was not found in config")
     115                raise ConfigError(config, f"ConfigError: Key '{csl.key}' is required but was not found in config")
    95116
    96117    return parsed_items
Note: See TracChangeset for help on using the changeset viewer.