示例#1
0
文件: fs.py 项目: moealmaw/xonsh
import sys
import io
import pathlib

try:
    from os import PathLike, fspath, fsencode, fsdecode
except ImportError:

    class PathLike(abc.ABC):
        """Abstract base class for implementing the file system path protocol."""
        @abc.abstractmethod
        def __fspath__(self):
            """Return the file system path representation of the object."""
            raise NotImplementedError

    PathLike.register(pathlib.Path)

    def fspath(path):
        """Return the string representation of the path.

        If str or bytes is passed in, it is returned unchanged. If __fspath__()
        returns something other than str or bytes then TypeError is raised. If
        this function is given something that is not str, bytes, or os.PathLike
        then TypeError is raised.
        """
        if isinstance(path, (str, bytes)):
            return path

        if isinstance(path, pathlib.Path):
            return str(path)
示例#2
0
文件: _fileio.py 项目: joshis1/Python
    def with_name(self, name: str) -> 'Path':
        return Path(self._path.with_name(name))

    def with_stem(self, stem: str) -> 'Path':
        return Path(self._path.with_name(stem + self._path.suffix))

    def with_suffix(self, suffix: str) -> 'Path':
        return Path(self._path.with_suffix(suffix))

    async def write_bytes(self, data: bytes) -> int:
        return await to_thread.run_sync(self._path.write_bytes, data)

    async def write_text(self,
                         data: str,
                         encoding: Optional[str] = None,
                         errors: Optional[str] = None,
                         newline: Optional[str] = None) -> int:
        # Path.write_text() does not support the "newline" parameter before Python 3.10
        def sync_write_text() -> int:
            with self._path.open('w',
                                 encoding=encoding,
                                 errors=errors,
                                 newline=newline) as fp:
                return fp.write(data)

        return await to_thread.run_sync(sync_write_text)


PathLike.register(Path)
示例#3
0
文件: fs.py 项目: donnemartin/gitsome
import io
import pathlib

try:
    from os import PathLike, fspath, fsencode, fsdecode
except ImportError:

    class PathLike(abc.ABC):
        """Abstract base class for implementing the file system path protocol."""

        @abc.abstractmethod
        def __fspath__(self):
            """Return the file system path representation of the object."""
            raise NotImplementedError

    PathLike.register(pathlib.Path)

    def fspath(path):
        """Return the string representation of the path.

        If str or bytes is passed in, it is returned unchanged. If __fspath__()
        returns something other than str or bytes then TypeError is raised. If
        this function is given something that is not str, bytes, or os.PathLike
        then TypeError is raised.
        """
        if isinstance(path, (str, bytes)):
            return path

        if isinstance(path, pathlib.Path):
            return str(path)