Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cyrillkuettel/925dbbfd35023bbdca3685801495e90f to your computer and use it in GitHub Desktop.
Save cyrillkuettel/925dbbfd35023bbdca3685801495e90f to your computer and use it in GitHub Desktop.
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