metadatapath.py 1.1 KB

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