Created
February 12, 2020 02:49
-
-
Save cowlinator/23e984d27c17f45785a2b3cfda1f2072 to your computer and use it in GitHub Desktop.
pathlib_interface.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _Flavour(object): | |
"""A flavour implements a particular (platform-specific) set of path | |
semantics.""" | |
def __init__(self) | |
def parse_parts(self, parts) | |
def join_parsed_parts(self, drv, root, parts, drv2, root2, parts2) | |
""" | |
Join the two paths represented by the respective | |
(drive, root, parts) tuples. Return a new (drive, root, parts) tuple. | |
""" | |
class _WindowsFlavour(_Flavour): | |
sep | |
altsep | |
has_drv | |
pathmod | |
is_supported | |
def splitroot(self, part, sep=sep) | |
def casefold(self, s) | |
def casefold_parts(self, parts) | |
def resolve(self, path, strict=False) | |
def is_reserved(self, parts) | |
def make_uri(self, path) | |
# Under Windows, file URIs use the UTF-8 encoding. | |
def gethomedir(self, username) | |
class _PosixFlavour(_Flavour): | |
sep | |
altsep | |
has_drv | |
pathmod | |
is_supported | |
def splitroot(self, part, sep=sep) | |
def casefold(self, s) | |
def casefold_parts(self, parts) | |
def resolve(self, path, strict=False) | |
def is_reserved(self, parts) | |
def make_uri(self, path) | |
# We represent the path using the local filesystem encoding, | |
# for portability to other applications. | |
def gethomedir(self, username) | |
_windows_flavour = _WindowsFlavour() | |
_posix_flavour = _PosixFlavour() | |
class _Accessor: | |
"""An accessor implements a particular (system-specific or not) way of | |
accessing paths on the filesystem.""" | |
class _NormalAccessor(_Accessor): | |
def stat | |
def lstat | |
def open | |
def listdir | |
def scandir | |
def chmod | |
def lchmod(self, pathobj, mode) | |
def mkdir | |
def unlink | |
def rmdir | |
def rename | |
def replace | |
def symlink(a, b, target_is_directory) | |
def utime | |
# Helper for resolve() | |
def readlink(self, path) | |
_normal_accessor = _NormalAccessor() | |
# the below classes are near-fully implemented, and you will probably not need to override many functions therein: | |
class PurePath(object): | |
"""PurePath represents a filesystem path and offers operations which | |
don't imply any actual filesystem I/O. Depending on your system, | |
instantiating a PurePath will return either a PurePosixPath or a | |
PureWindowsPath object. You can also instantiate either of these classes | |
directly, regardless of your system. | |
""" | |
__slots__ | |
def __new__(cls, *args) | |
"""Construct a PurePath from one or several strings and or existing | |
PurePath objects. The strings and path objects are combined so as | |
to yield a canonicalized path, which is incorporated into the | |
new PurePath object. | |
""" | |
def __reduce__(self) | |
# Using the parts tuple helps share interned path parts | |
# when pickling related paths. | |
@classmethod | |
def _from_parsed_parts(cls, drv, root, parts, init=True) | |
def _init(self) | |
# Overridden in concrete Path | |
def _make_child(self, args) | |
def __str__(self) | |
"""Return the string representation of the path, suitable for | |
passing to system calls.""" | |
def __fspath__(self) | |
def as_posix(self) | |
"""Return the string representation of the path with forward (/) | |
slashes.""" | |
def __bytes__(self) | |
"""Return the bytes representation of the path. This is only | |
recommended to use under Unix.""" | |
def __repr__(self) | |
def as_uri(self) | |
"""Return the path as a 'file' URI.""" | |
@property | |
def _cparts(self) | |
# Cached casefolded parts, for hashing and comparison | |
def __eq__(self, other) | |
def __ne__(self, other) | |
def __hash__(self) | |
def __lt__(self, other) | |
def __le__(self, other) | |
def __gt__(self, other) | |
def __ge__(self, other) | |
drive | |
"""The drive prefix (letter or UNC path), if any.""" | |
root | |
"""The root of the path, if any.""" | |
@property | |
def anchor(self) | |
"""The concatenation of the drive and root, or ''.""" | |
@property | |
def name(self) | |
"""The final path component, if any.""" | |
@property | |
def suffix(self) | |
"""The final component's last suffix, if any.""" | |
@property | |
def suffixes(self) | |
"""A list of the final component's suffixes, if any.""" | |
@property | |
def stem(self) | |
"""The final path component, minus its last suffix.""" | |
def with_name(self, name) | |
"""Return a new path with the file name changed.""" | |
def with_suffix(self, suffix) | |
"""Return a new path with the file suffix changed (or added, if | |
none). | |
""" | |
def relative_to(self, *other) | |
"""Return the relative path to another path identified by the passed | |
arguments. If the operation is not possible (because this is not | |
a subpath of the other path), raise ValueError. | |
""" | |
@property | |
def parts(self) | |
"""An object providing sequence-like access to the | |
components in the filesystem path.""" | |
def joinpath(self, *args) | |
"""Combine this path with one or several arguments, and return a | |
new path representing either a subpath (if all arguments are relative | |
paths) or a totally different path (if one of the arguments is | |
anchored). | |
""" | |
def __truediv__(self, key) | |
def __rtruediv__(self, key) | |
__div__ | |
__rdiv__ | |
@property | |
def parent(self) | |
"""The logical parent of the path.""" | |
@property | |
def parents(self) | |
"""A sequence of this path's logical parents.""" | |
def is_absolute(self) | |
"""True if the path is absolute (has both a root and, if applicable, | |
a drive).""" | |
def is_reserved(self) | |
"""Return True if the path contains one of the special names reserved | |
by the system, if any.""" | |
def match(self, path_pattern) | |
""" | |
Return True if this path matches the given pattern. | |
""" | |
class PurePosixPath(PurePath): | |
_flavour = _posix_flavour | |
__slots__ = () | |
class PureWindowsPath(PurePath): | |
_flavour = _windows_flavour | |
__slots__ = () | |
class Path(PurePath): | |
__slots__ | |
def __new__(cls, *args, **kwargs) | |
def _init(self, | |
# Private non-constructor arguments | |
template=None, | |
) | |
def _make_child_relpath(self, part) | |
# This is an optimization used for dir walking. `part` must be | |
# a single part relative to this path. | |
def __enter__(self) | |
def __exit__(self, t, v, tb) | |
# Public API | |
@classmethod | |
def cwd(cls): | |
"""Return a new path pointing to the current working directory | |
(as returned by os.getcwd()). | |
""" | |
@classmethod | |
def home(cls) | |
"""Return a new path pointing to the user's home directory (as | |
returned by os.path.expanduser('~')). | |
""" | |
def samefile(self, other_path) | |
"""Return whether other_path is the same or not as this file | |
(as returned by os.path.samefile()). | |
""" | |
def iterdir(self) | |
"""Iterate over the files in this directory. Does not yield any | |
result for the special paths '.' and '..'. | |
""" | |
def glob(self, pattern) | |
"""Iterate over this subtree and yield all existing files (of any | |
kind, including directories) matching the given pattern. | |
""" | |
def rglob(self, pattern) | |
"""Recursive glob | |
""" | |
def absolute(self) | |
"""Return an absolute version of this path. This function works | |
even if the path doesn't point to anything. | |
No normalization is done, i.e. all '.' and '..' will be kept along. | |
Use resolve() to get the canonical path to a file. | |
""" | |
def resolve(self, strict=False) | |
""" | |
Make the path absolute, resolving all symlinks on the way and also | |
normalizing it (for example turning slashes into backslashes under | |
Windows). | |
""" | |
def stat(self) | |
*** Needs override ('Not implemented') *** | |
def owner(self) | |
""" | |
Return the login name of the file owner. | |
""" | |
*** Needs override ('Not implemented') *** | |
def group(self) | |
""" | |
Return the group name of the file gid. | |
""" | |
*** Needs override *** | |
def open(self, mode='r', buffering=-1, encoding=None, | |
errors=None, newline=None) | |
def read_bytes(self) | |
""" | |
Open the file in bytes mode, read it, and close the file. | |
""" | |
def read_text(self, encoding=None, errors=None) | |
def write_bytes(self, data) | |
def write_text(self, data, encoding=None, errors=None) | |
def touch(self, mode=0o666, exist_ok=True) | |
""" | |
Create this file with the given access mode, if it doesn't exist. | |
""" | |
def mkdir(self, mode=0o777, parents=False, exist_ok=False) | |
def chmod(self, mode) | |
""" | |
Change the permissions of the path, like os.chmod(). | |
""" | |
def lchmod(self, mode) | |
""" | |
Like chmod(), except if the path points to a symlink, the symlink's | |
permissions are changed, rather than its target's. | |
""" | |
def unlink(self) | |
""" | |
Remove this file or link. | |
If the path is a directory, use rmdir() instead. | |
""" | |
def rmdir(self) | |
def lstat(self) | |
""" | |
Like stat(), except if the path points to a symlink, the symlink's | |
status information is returned, rather than its target's. | |
""" | |
def rename(self, target) | |
""" | |
Rename this path to the given path. (atomic on unix) | |
""" | |
def replace(self, target) | |
""" | |
Rename this path to the given path, clobbering the existing | |
destination if it exists. | |
""" | |
def symlink_to(self, target, target_is_directory=False) | |
""" | |
Make this path a symlink pointing to the given path. | |
Note the order of arguments (self, target) is the reverse of | |
os.symlink's. | |
""" | |
# Convenience functions for querying the stat results | |
def exists(self) | |
def is_dir(self) | |
def is_file(self) | |
def is_symlink(self) | |
def is_block_device(self) | |
def is_char_device(self) | |
def is_fifo(self) | |
def is_socket(self) | |
def expanduser(self) | |
""" Return a new path with expanded ~ and ~user constructs | |
(as returned by os.path.expanduser) | |
""" | |
class PosixPath(Path, PurePosixPath): | |
__slots__ = () | |
class WindowsPath(Path, PureWindowsPath): | |
__slots__ = () | |
def owner(self) | |
def group(self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment