memory.py 663 B

123456789101112131415161718192021222324252627
  1. from __future__ import annotations
  2. from upath._compat import FSSpecAccessorShim as _FSSpecAccessorShim
  3. from upath.core import UPath
  4. __all__ = ["MemoryPath"]
  5. # accessors are deprecated
  6. _MemoryAccessor = _FSSpecAccessorShim
  7. class MemoryPath(UPath):
  8. def iterdir(self):
  9. if not self.is_dir():
  10. raise NotADirectoryError(str(self))
  11. yield from super().iterdir()
  12. @property
  13. def path(self):
  14. path = super().path
  15. return "/" if path == "." else path
  16. def __str__(self):
  17. s = super().__str__()
  18. if s.startswith("memory:///"):
  19. s = s.replace("memory:///", "memory://", 1)
  20. return s