Skip to content

file: and data:

pathlib_next.uri.schemes.file

FileUri(*uris, **options)

Bases: UriPath

file: scheme: a UriPath wrapping a LocalPath (filepath), delegating all I/O to it.

Source code in src/pathlib_next/uri/__init__.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def __init__(self, *uris: UriLike, **options):
    if self._raw_uris or self._initiated:
        return
    _uris: list[str | Uri] = []
    for uri in uris:
        if not uri:
            uri = ""
        if isinstance(uri, Uri):
            _uris.append(uri)
        elif isinstance(uri, (_pathlib.Path, Path)):
            try:
                uri = uri.as_uri()
            except ValueError:
                # as_uri() raises ValueError for a relative path, which
                # joins like a relative PurePath (see _RelativeLocalPath).
                uri = _RelativeLocalPath(_uriencode(uri.as_posix(), safe="/"))
            _uris.append(uri)
        elif isinstance(uri, (_pathlib.PurePath, Pathname)):
            _uris.append(_path_reference(uri.as_posix()))
        elif hasattr(uri, "as_uri"):
            path = uri.as_uri
            if callable(path):
                path = path()
            _uris.append(path)
        elif isinstance(uri, str):
            _uris.append(uri)
        elif isinstance(uri, bytes):
            _uris.append(uri.decode())
        else:
            path = None
            try:
                path = os.fspath(uri)
            except (TypeError, NotImplementedError):
                pass
            if not isinstance(path, str):
                raise TypeError(
                    "argument should be a str or an os.PathLike "
                    "object where __fspath__ returns a str, "
                    f"not {type(path).__name__!r}"
                )
            # Only __fspath__ is guaranteed here -- posix-normalize the
            # string itself rather than assuming an as_posix() method.
            posix = _pathlib.PurePath(path).as_posix()
            _uris.append(_path_reference(posix))
    self._raw_uris = _uris

is_absolute()

Also True for a Windows drive path ("C:/x"), which _init() stores without the "/" a URI path gets in front of it. "C:" alone is drive-relative, as for PureWindowsPath.

Source code in src/pathlib_next/uri/schemes/file.py
44
45
46
47
48
49
50
51
52
53
def is_absolute(self):
    """Also True for a Windows drive path ("C:/x"), which `_init()`
    stores without the "/" a URI path gets in front of it. "C:" alone is
    drive-relative, as for `PureWindowsPath`."""
    path = self.path
    if _os.name == "nt":
        drive, sep, _ = path.partition("/")
        if _is_drive(drive):
            return bool(sep)
    return super().is_absolute()

pathlib_next.uri.schemes.data

DataUri(*uris, **options)

Bases: UriPath

data: scheme (RFC 2397): a read-only "file" whose entire content is embedded in the URI itself (data:[<mediatype>][;base64],<data>) -- no filesystem, no directories, no backend/connection. Percent-encoded reserved characters (?/#) in the payload are only decoded correctly if the URI was built with them escaped, per RFC 2397/3986.

Source code in src/pathlib_next/uri/__init__.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def __init__(self, *uris: UriLike, **options):
    if self._raw_uris or self._initiated:
        return
    _uris: list[str | Uri] = []
    for uri in uris:
        if not uri:
            uri = ""
        if isinstance(uri, Uri):
            _uris.append(uri)
        elif isinstance(uri, (_pathlib.Path, Path)):
            try:
                uri = uri.as_uri()
            except ValueError:
                # as_uri() raises ValueError for a relative path, which
                # joins like a relative PurePath (see _RelativeLocalPath).
                uri = _RelativeLocalPath(_uriencode(uri.as_posix(), safe="/"))
            _uris.append(uri)
        elif isinstance(uri, (_pathlib.PurePath, Pathname)):
            _uris.append(_path_reference(uri.as_posix()))
        elif hasattr(uri, "as_uri"):
            path = uri.as_uri
            if callable(path):
                path = path()
            _uris.append(path)
        elif isinstance(uri, str):
            _uris.append(uri)
        elif isinstance(uri, bytes):
            _uris.append(uri.decode())
        else:
            path = None
            try:
                path = os.fspath(uri)
            except (TypeError, NotImplementedError):
                pass
            if not isinstance(path, str):
                raise TypeError(
                    "argument should be a str or an os.PathLike "
                    "object where __fspath__ returns a str, "
                    f"not {type(path).__name__!r}"
                )
            # Only __fspath__ is guaranteed here -- posix-normalize the
            # string itself rather than assuming an as_posix() method.
            posix = _pathlib.PurePath(path).as_posix()
            _uris.append(_path_reference(posix))
    self._raw_uris = _uris

mediatype property

The declared media type. Without a type/subtype, RFC 2397 implies "text/plain": a bare header gives "text/plain;charset=US-ASCII", and parameters alone (";charset=utf-8") are given that type.

stat(*, follow_symlinks=True)

Source code in src/pathlib_next/uri/schemes/data.py
62
63
def stat(self, *, follow_symlinks=True):
    return FileStat(st_size=len(self._content()), is_dir=False)