コード例 #1
0
    def from_yaml_file(cls, path: typing.Optional[pathlib.Path] = None) -> 'Settings':
        if path is None:
            path = get_config_file_path()
        logger.debug(f"Loading from {path}")

        try:
            with path.open('r') as stream:
                return cls.from_yaml_stream(stream)
        except FileNotFoundError as error:
            raise utils.UsageError(f'Could not find the file {error.filename}')
        except OSError as error:
            raise utils.UsageError(f'Failed to open config file: {error}')
コード例 #2
0
    def _get_editor_path(self, editor: typing.Optional[str]) -> str:
        if editor is not None:
            path = spawn.find_executable(editor)
            if path is None:
                raise utils.UsageError(f"Could not find the editor {editor}")
            return path

        for default_editor in self.DEFAULT_EDITORS:
            path = spawn.find_executable(default_editor)
            if path is not None:
                return path

        raise utils.UsageError('Could not find a valid editor')
コード例 #3
0
    def from_yaml_stream(cls, stream: typing.IO) -> 'Settings':
        validator = utils.SchemaValidator()

        try:
            data = yaml.safe_load(stream)
        except (yaml.YAMLError, OSError, UnicodeDecodeError) as error:
            raise utils.UsageError(f"Failed to load configuration:\n{error}")

        validator.validate(data, cls.SETTINGS_SCHEMA)

        root_dir = pathlib.Path(os.path.expanduser(data.pop('root-dir')))
        global_ignore = data.pop('global-ignore', [])

        connections = cls._get_connection_settings(
            validator=validator,
            raw_connections=data.pop('connections'),
            raw_subjects=data.pop('subjects'),
            global_ignore=global_ignore,
            root_dir=root_dir,
        )

        return Settings(
            root_dir=root_dir,
            connections=connections,
        )
コード例 #4
0
    def edit(self,
             config: typing.Optional[pathlib.Path] = None,
             editor: typing.Optional[str] = None) -> None:
        if editor is None and 'EDITOR' in os.environ:
            editor = os.environ['EDITOR']
        editor_path: str = self._get_editor_path(editor)

        if config is None:
            config = get_config_file_path()
            config.parent.mkdir(exist_ok=True)
            config.touch(exist_ok=True)
        elif not config.exists():
            raise utils.UsageError(f"Could not find the configuration file {config}")

        subprocess.call([editor_path, config])