Beispiel #1
0
    def _get_value(self,
                   entry: ConfigEntry,
                   section: str = "") -> Union[str, bool]:
        """Get an entry value from a configuration mapping.

        Parameters
        ----------
        entry : ConfigEntry
            The entry to search
        section : str
            The section the entry belongs to

        Returns
        -------
        value : Union[str, bool]
            The entry value
        """
        try:
            if section:
                value = self.config[section][entry.name]
            else:
                value = self.config[entry.name]

            return entry.casted(value)

        except KeyError:
            if entry.default is not None:
                return entry.default

        msg = f"'{entry.name}' unset but no default specified"
        if section:
            msg += f" for section '{section}'"
        raise ConfigError(msg)
Beispiel #2
0
 def _get_fs(self):
     try:
         yield self.fs
     except PermissionError as e:
         raise ConfigError(
             f"{e} - check configured Google Cloud Storage credentials"
         )
Beispiel #3
0
    def casted(self, value):
        if self.cast is bool:
            try:
                value = strtobool(str(value))
            except ValueError as e:
                raise ConfigError(f"{self.name}: {e}")

        return self.cast(value)
Beispiel #4
0
    def _read_config(self, filename: str) -> Dict[str, Any]:
        """Read a configuration file from its path.

        Parameters
        ----------
        filename : str
            The path of the configuration file

        Returns
        -------
        configuration : Dict[str, str]
            The mapping of configuration variables found in the file
        """
        with open(filename) as f:
            try:
                return dict(toml.load(f))
            except toml.TomlDecodeError as e:
                raise ConfigError(f"failed to load config file '{filename}': {e}")
Beispiel #5
0
 def _get_fs(self):
     try:
         yield self.fs
     except PermissionError as e:
         raise ConfigError(f"{e} - check configured Azure Blob credentials")