- Timestamp:
- Oct 8, 2019, 11:42:36 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-259_genericise_backups/tools/backups/utils/config_parse_helper.py
r40915 r40919 3 3 from backups.utils.errors import ConfigError, ValidationError 4 4 5 # The as configs should be parsed should be pretty uniform. There's a limited6 # amount that can be done with the actual loading.7 8 # Each backup file should be expecting particular lines from a config. So, this9 # means a schema can be made for what each backup expects.10 11 5 12 6 class 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 8 separate it out into list. 14 9 """ 15 10 commma_separated = 1 … … 18 13 19 14 class 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, 16 the type that the value should be processed as, and any special processing required 17 """ 22 18 23 19 def __init__(self, key: str, required: bool, expected_type, special_processing: SpecialSchemaProcessing=None): … … 30 26 self.special_processing = special_processing 31 27 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 32 36 33 37 class ConfigSchema(): 34 """Defines the schema to be used""" 38 """Defines a schema to be used. Consists of a single section name and 39 multiple config lines""" 35 40 36 41 def __init__(self, section_name, options: [ConfigSchemaLine]): … … 52 57 self.options[line.key] = line 53 58 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 54 71 55 72 def _split_string_to_list(items: str, separator=',') -> [str]: … … 60 77 61 78 def 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 80 and return them as a dictionary of the keys and the assoicated value as the 81 type defined by the schema 82 """ 63 83 parsed_items = {} 64 84 65 85 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) 67 89 68 if config.has_option(section, csl.key): 69 90 if config.has_option(section, csl_key): 70 91 if csl.expected_type is bool: 71 92 parsed_items[csl.key] = config.getboolean(section, csl.key) 72 elif csl.expected is int:93 elif csl.expected_type is int: 73 94 parsed_items[csl.key] = config.getint(section, csl.key) 74 95 elif csl.expected_type is float: … … 79 100 if csl.special_processing == SpecialSchemaProcessing.commma_separated: 80 101 items = config[section][csl.key] 81 parsed_items[csl.key] = _split_string_to_list(items, '')82 # parse comma_separated stuff102 splitup = _split_string_to_list(items, ',') 103 parsed_items[csl.key] = splitup 83 104 elif csl.special_processing == SpecialSchemaProcessing.space_separated: 84 105 items = config[section][csl.key] 85 parsed_items[csl.key] = _split_string_to_list(items, ',')86 # space separated processing106 splitup = _split_string_to_list(items, ' ') 107 parsed_items[csl.key] = splitup 87 108 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}") 89 110 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__}") 91 112 92 113 else: 93 114 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") 95 116 96 117 return parsed_items
Note:
See TracChangeset
for help on using the changeset viewer.
