def get_folder(self, section: str, key: str) -> str: # if the config is missing the given section if not self._config.has_section(section): # add the given section to the config self.add_section(section) try: # get the value from the config value = self.get_key_value(section, key) except ConfigurationMissingEntryError: # create the key if it is missing self.set_key_value(section, key, '') value = None try: # if the key's value is missing if not value: raise ConfigurationEmptyEntryError( key, Exception(f'Value for {key} is empty.')) # get a Path instance from the given folder name path = pathlib.Path(value) # if the path doesn't exist if not path.exists(): # create the directory path.mkdir(parents=True, exist_ok=True) return value except ValueError as valueError: raise ConfigurationTypeError(key, int, valueError)
def owner(self): key = 'owner' try: return self._uxStore.owner except ValueError as valueError: invalidEntry = '\n'.join([ str(valueError), f'Please insert a valid {key} id in the config file.' ]) raise ConfigurationEmptyEntryError(key, ValueError(invalidEntry))
def get_token(self, tag): # if the config is missing the token section if not self._config.has_section(self.section): # add the token section to the config self.add_section(self.section) try: # if the provided tag is None if tag is None: raise ConfigurationGetError(tag, Exception(f'Tag to retreive token entry for was type {type(tag)}.')) # get the token from the config file token = self.get_key_value(self.section, tag) if token == '': raise ConfigurationEmptyEntryError(tag, Exception(f'Token entry for tag {tag} was empty.')) return token except ConfigurationMissingEntryError: raise
def mode(self): default_section = 'DEFAULT' entry_name = 'mode' # if the config is missing the TOKEN section if not self._config.has_section(default_section): # add the TOKEN section to the config self.add_section(default_section) try: # get the mode value from the config file mode = self.get_key_value(default_section, entry_name) if mode == '': raise ConfigurationEmptyEntryError(mode, Exception(f'Entry for token mode was empty.')) return mode except ConfigurationMissingEntryError: ##print('Tried to get entry for token mode but the entry either did not exist or was empty.') raise
def token(self): try: return self._tokenStore.get_token(self.mode) except ConfigurationMissingEntryError: self._tokenStore.add_token(self.mode, '') missingToken = ' '.join([ f'No config entry found with tag \'{self.mode}\'.', 'An entry has been created for you to insert your token.' ]) raise ConfigurationMissingEntryError(self.mode, Exception(missingToken)) except ConfigurationEmptyEntryError: emptyToken = ' '.join([ f'Config entry with tag \'{self.mode}\' contained an empty string.', 'Please insert a token for sign-in.' ]) raise ConfigurationEmptyEntryError(self.mode, Exception(emptyToken))
def logging_channel(self): entry_name = 'logging_channel' try: return self._uxStore.logging_channel except ConfigurationEmptyEntryError: missing_entry = ' '.join([ f'No {entry_name} selector was found in the config, a selector has been generated for you.', f'Please supply a valid value for the {entry_name} entry in the config file.' ]) raise ConfigurationMissingEntryError(entry_name, Exception(missing_entry)) except ConfigurationEmptyEntryError: empty_entry = ' '.join([ f'The {entry_name} selector in the config contained an empty string.', f'Please supply a valid value for the {entry_name} entry in the config file.' ]) raise ConfigurationEmptyEntryError(entry_name, Exception(empty_entry))
def get_string(self, section: str, key: str): # if the config is missing the given section if not self._config.has_section(section): # add the given section to the config self.add_section(section) try: # get the value from the config value = self.get_key_value(section, key) except ConfigurationMissingEntryError: # create the key if it is missing self.set_key_value(section, key, '') value = None try: # if the key's value is missing if not value: raise ConfigurationEmptyEntryError( key, Exception(f'Entry for {key} is empty')) return value except ValueError as valueError: raise ConfigurationTypeError(key, int, valueError)
def get_integer(self, section: str, key: str) -> int: # if the config is missing the given section if not self._config.has_section(section): # add the given section to the config self.add_section(section) try: # get the value from the config file value = self.get_key_value(section, key) except ConfigurationMissingEntryError: # create the key if it is missing self.set_key_value(section, key, '') value = None try: # if the key's value is empty if not value: raise ConfigurationEmptyEntryError( key, Exception(f'Value for {key} is empty.')) # parse the value to an integer or raise ValueError return int(value) # if cast to int fails except ValueError as valueError: raise ConfigurationTypeError(key, int, valueError)