metadatapath.py 955 B

1234567891011121314151617181920212223242526272829303132
  1. from pathlib import PurePath, PurePosixPath
  2. from dataladmetadatamodel.log import logger
  3. class MetadataPath(PurePosixPath):
  4. def __new__(cls, *args):
  5. original_path = PurePath(*args)
  6. if not original_path.is_absolute():
  7. created_path = super().__new__(
  8. cls,
  9. "/".join(original_path.parts))
  10. else:
  11. created_path = super().__new__(
  12. cls,
  13. "/".join(original_path.parts[1:]))
  14. logger.warning(
  15. f"Denied creation of absolute metadata path: {original_path}, "
  16. f"created {created_path} instead. This is considered an error "
  17. f"in the calling code.")
  18. return created_path
  19. def __str__(self):
  20. path_str = PurePosixPath.__str__(self)
  21. return (
  22. ""
  23. if path_str == "."
  24. else path_str)
  25. def __len__(self):
  26. return len(self.parts)