[docs]classMutateAPIMixin:""" A mixin class that implements the S3Path object mutation. """
[docs]defcopy(self:"S3Path")->"S3Path":""" Create a copy of S3Path object that logically equals to this one, but is actually different identity in memory. Also, the cache data are cleared. Example:: >>> p1 = S3Path("bucket", "folder", "file.txt") >>> p2 = p1.copy() >>> p1 == p2 True >>> p1 is p2 False .. versionadded:: 1.0.1 """returnself._from_parsed_parts(bucket=self._bucket,parts=list(self._parts),is_dir=self._is_dir,)
[docs]defchange(self:"S3Path",new_bucket:str=None,new_abspath:str=None,new_dirpath:str=None,new_dirname:str=None,new_basename:str=None,new_fname:str=None,new_ext:str=None,)->"S3Path":""" Create a new S3Path by replacing part of the attributes. If no argument is given, it behaves like :meth:`copy`. Example: >>> s3path = S3Path("bucket", "folder", "file.txt") >>> s3path.change(new_bucket="new_bucket") S3Path('s3://new_bucket/folder/file.txt') >>> s3path = S3Path("bucket", "folder", "file.txt") >>> s3path.change(new_basename="data.json") S3Path('s3://bucket/folder/data.json') >>> s3path = S3Path("bucket", "folder", "file.txt") >>> s3path.change(new_fname="log") S3Path('s3://bucket/folder/log.txt') :param new_bucket: The new bucket name :param new_abspath: :param new_dirpath: :param new_dirname: :param new_basename: :param new_fname: :param new_ext: .. versionadded:: 1.0.2 """ifnew_bucketisNone:new_bucket=self.bucketifnew_abspathisnotNone:exc.ensure_all_none(new_dirpath=new_dirpath,new_dirname=new_dirname,new_basename=new_basename,new_fname=new_fname,new_ext=new_ext,)p=self._from_parts([self.bucket,new_abspath])returnpif(new_dirpathisNone)and(new_dirnameisnotNone):dir_parts=self.parent.parent._parts+[new_dirname]elif(new_dirpathisnotNone)and(new_dirnameisNone):dir_parts=[new_dirpath,]elif(new_dirpathisNone)and(new_dirnameisNone):dir_parts=self.parent._partselse:raiseValueError("Cannot having both 'new_dirpath' and 'new_dirname'!")ifnew_basenameisNone:ifnew_fnameisNone:new_fname=self.fnameifnew_extisNone:new_ext=self.extnew_basename=new_fname+new_extelse:if(new_fnameisnotNone)or(new_extisnotNone):raiseValueError("Cannot having both ""'new_basename' / 'new_fname', ""or 'new_basename' / 'new_ext'!")ifnew_bucketisNone:p=self._from_parts(["dummy-bucket",]+dir_parts+[new_basename,])p._bucket=Noneelse:p=self._from_parts([new_bucket,]+dir_parts+[new_basename,])returnp
[docs]defto_dir(self:"S3Path")->"S3Path":""" Convert the S3Path to a directory. If the S3Path is a file, then append a "/" at the end. If the S3Path is already a directory, then do nothing. Example: >>> S3Path.from_s3_uri("s3://bucket/folder").to_dir() S3Path('s3://bucket/folder/') """ifself.is_dir():returnself.copy()elifself.is_file():returnself.joinpath("/")else:raiseValueError("only concrete file or folder S3Path can do .to_dir()")
[docs]defto_file(self:"S3Path")->"S3Path":""" Convert the S3Path to a file. If the S3Path is a directory, then strip out the last "/". If the S3Path is already a file, then do nothing. Example: >>> S3Path.from_s3_uri("s3://bucket/file/").to_dir() S3Path('s3://bucket/file/') """ifself.is_file():returnself.copy()elifself.is_dir():p=self.copy()p._is_dir=Falsereturnpelse:raiseValueError("only concrete file or folder S3Path can do .to_file()")