Created
September 20, 2022 09:26
-
-
Save cyrillkuettel/925dbbfd35023bbdca3685801495e90f to your computer and use it in GitHub Desktop.
This file contains 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 PathAdapter(PosixPath): | |
""" | |
Adapter to translate a Sub Filesystem from Pyfilesystem to Pathlib. | |
Use like this: | |
from fs import open_fs | |
from pathlib import Path, PosixPath | |
your_fs = open_fs('/tmp/test/') | |
adapter = PathAdapter(your_fs, "some_folder") | |
path = adapter.as_pathlib_path() | |
assert str(path) == /tmp/test/some_folder | |
""" | |
def __new__(cls, fs, *pathsegments): | |
self = super().__new__(cls, *pathsegments) | |
self.fs = fs | |
return self | |
@property | |
def relative_fs_path(self) -> str: | |
return super().__str__() | |
def as_pathlib_path(self) -> Path: | |
return Path(self.fs.getsyspath(self.relative_fs_path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment