Beispiel #1
0
    def _path_method_wrapper(
            self, path: Path, method: str, *args: Any,
            **kwargs: Any) -> Union[Tuple[Path, Any], List[Tuple[Path, Any]]]:

        # TODO: Move to parameters after dropping Python 2.7
        return_first = kwargs.pop("return_first", True)
        writable_only = kwargs.pop("writable_only", False)

        candidates = self.make_candidates(path, writable_only=writable_only)

        if not candidates:
            raise RuntimeError(
                'Unable to find a suitable destination for "{}" in {}'.format(
                    str(path), paths_csv(self._candidates)))

        results = []

        for candidate in candidates:
            try:
                result = candidate, getattr(candidate, method)(*args, **kwargs)
                if return_first:
                    return result
                else:
                    results.append(result)
            except (IOError, OSError):
                # TODO: Replace with PermissionError
                pass

        if results:
            return results

        raise OSError("Unable to access any of {}".format(
            paths_csv(candidates)))
Beispiel #2
0
    def _path_method_wrapper(
        self,
        path: Union[str, Path],
        method: str,
        *args: Any,
        return_first: bool = True,
        writable_only: bool = False,
        **kwargs: Any,
    ) -> Union[Tuple[Path, Any], List[Tuple[Path, Any]]]:
        if isinstance(path, str):
            path = Path(path)

        candidates = self.make_candidates(path, writable_only=writable_only)

        if not candidates:
            raise RuntimeError(
                'Unable to find a suitable destination for "{}" in {}'.format(
                    str(path), paths_csv(self._candidates)))

        results = []

        for candidate in candidates:
            try:
                result = candidate, getattr(candidate, method)(*args, **kwargs)
                if return_first:
                    return result
                else:
                    results.append(result)
            except OSError:
                # TODO: Replace with PermissionError
                pass

        if results:
            return results

        raise OSError("Unable to access any of {}".format(
            paths_csv(candidates)))