示例#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)
示例#2
0
 def _get_fs(self):
     try:
         yield self.fs
     except PermissionError as e:
         raise ConfigError(
             f"{e} - check configured Google Cloud Storage credentials"
         )
示例#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)
示例#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}")
示例#5
0
 def _get_fs(self):
     try:
         yield self.fs
     except PermissionError as e:
         raise ConfigError(f"{e} - check configured Azure Blob credentials")