From 37f33b1a29b4672b71a93c74ca6dbd13ab539054 Mon Sep 17 00:00:00 2001 From: John Tordoff <> Date: Thu, 10 Aug 2023 08:00:50 -0400 Subject: [PATCH] take base_folder as part of __init__ --- tests/providers/s3/test_provider.py | 12 +---------- waterbutler/providers/s3/metadata.py | 14 +++++------- waterbutler/providers/s3/provider.py | 32 ++++++++++++---------------- 3 files changed, 20 insertions(+), 38 deletions(-) diff --git a/tests/providers/s3/test_provider.py b/tests/providers/s3/test_provider.py index 8abd1fa29..877023aab 100644 --- a/tests/providers/s3/test_provider.py +++ b/tests/providers/s3/test_provider.py @@ -321,15 +321,6 @@ async def test_folder(self, provider, mock_time): assert path.is_dir assert not path.is_root - @pytest.mark.asyncio - async def test_root(self, provider, mock_time): - provider.settings['id'] = 'that kerning:/' - path = await provider.validate_path('/') - assert path.name == '' - assert not path.is_file - assert path.is_dir - assert path.is_root - @pytest.mark.asyncio async def test_subfolder(self, provider, mock_time): path = await provider.validate_path('/') @@ -338,7 +329,6 @@ async def test_subfolder(self, provider, mock_time): assert path.is_dir assert not path.is_root - class TestCRUD: @pytest.mark.asyncio @@ -458,7 +448,7 @@ async def test_upload_to_subfolder_as_root(self, metadata, created = await provider.upload(file_stream, path) assert metadata.kind == 'file' - assert metadata.path == '/my-subfolder/foobah' + assert metadata.path == '/foobah' assert not created assert aiohttpretty.has_call(method='PUT', uri=url) assert aiohttpretty.has_call(method='HEAD', uri=metadata_url) diff --git a/waterbutler/providers/s3/metadata.py b/waterbutler/providers/s3/metadata.py index b1093e107..a5f6f210c 100644 --- a/waterbutler/providers/s3/metadata.py +++ b/waterbutler/providers/s3/metadata.py @@ -30,9 +30,7 @@ def __init__(self, path, headers): @property def path(self): - if self.raw.get('base_folder'): - return '/' + strip_char(self._path, self.raw['base_folder']) - return '/' + self._path + return '/' + strip_char(self._path, self.raw.get('base_folder', '')) @property def size(self): @@ -70,9 +68,7 @@ class S3FileMetadata(S3Metadata, metadata.BaseFileMetadata): @property def path(self): - if self.raw.get('base_folder'): - return '/' + strip_char(self.raw['Key'], self.raw['base_folder']) - return '/' + self.raw['Key'] + return '/' + strip_char(self.raw['Key'], self.raw.get('base_folder', '')) @property def size(self): @@ -113,7 +109,7 @@ def name(self): @property def path(self): - return '/' + self.raw['Key'] + return '/' + strip_char(self.raw['Key'], self.raw.get('base_folder', '')) class S3FolderMetadata(S3Metadata, metadata.BaseFolderMetadata): @@ -124,8 +120,8 @@ def name(self): @property def path(self): - if self.raw.get('base_folder'): - return '/' + strip_char(self.raw['Prefix'], self.raw['base_folder']) + if self.raw.get('base_folder', ''): + return '/' + strip_char(self.raw['Prefix'], self.raw.get('base_folder', '')) return '/' + self.raw['Prefix'] diff --git a/waterbutler/providers/s3/provider.py b/waterbutler/providers/s3/provider.py index 7b113763d..b5c169b22 100644 --- a/waterbutler/providers/s3/provider.py +++ b/waterbutler/providers/s3/provider.py @@ -62,6 +62,7 @@ def __init__(self, auth, credentials, settings, **kwargs): self.connection = S3Connection(credentials['access_key'], credentials['secret_key'], calling_format=OrdinaryCallingFormat()) self.bucket = self.connection.get_bucket(settings['bucket'], validate=False) + self.base_folder = self.settings.get('id', ':/').split(':/')[1] self.encrypt_uploads = self.settings.get('encrypt_uploads', False) self.region = None @@ -69,7 +70,7 @@ async def validate_v1_path(self, path, **kwargs): await self._check_region() # The user selected base folder, the root of the where that user's node is connected. - path = f"/{self.settings.get('id', ':/').split(':/')[1] + path.lstrip('/')}" + path = f"/{self.base_folder + path.lstrip('/')}" implicit_folder = path.endswith('/') @@ -99,8 +100,7 @@ async def validate_v1_path(self, path, **kwargs): async def validate_path(self, path, **kwargs): # The user selected base folder, the root of the where that user's node is connected. - path = f"/{self.settings.get('id', ':/').split(':/')[1] + path.lstrip('/')}" - return WaterButlerPath(path) + return WaterButlerPath(f"/{self.base_folder + path.lstrip('/')}") def can_duplicate_names(self): return True @@ -116,7 +116,6 @@ async def intra_copy(self, dest_provider, source_path, dest_path): `dest_provider` must have read access to `source.bucket`. """ await self._check_region() - exists = await dest_provider.exists(dest_path) dest_key = dest_provider.bucket.new_key(dest_path.path) @@ -640,9 +639,14 @@ async def metadata(self, path, revision=None, **kwargs): await self._check_region() if path.is_dir: - return (await self._metadata_folder(path)) + metadata = await self._metadata_folder(path) + for item in metadata: + item.raw['base_folder'] = self.base_folder + else: + metadata = await self._metadata_file(path, revision=revision) + metadata.raw['base_folder'] = self.base_folder - return (await self._metadata_file(path, revision=revision)) + return metadata async def create_folder(self, path, folder_precheck=True, **kwargs): """ @@ -664,7 +668,9 @@ async def create_folder(self, path, folder_precheck=True, **kwargs): throws=exceptions.CreateFolderError ) - return S3FolderMetadata({'Prefix': path.path}) + metadata = S3FolderMetadata({'Prefix': path.path}) + metadata.raw['base_folder'] = self.base_folder + return metadata async def _metadata_file(self, path, revision=None): await self._check_region() @@ -688,10 +694,7 @@ async def _metadata_file(self, path, revision=None): async def _metadata_folder(self, path): await self._check_region() - # The user selected base folder, the root of the where that user's node is connected. - prefix = self.settings['id'].split(':/')[1] if path == '/' and self.settings.get('id') else path.path - - params = {'prefix': prefix, 'delimiter': '/'} + params = {'prefix': path.path, 'delimiter': '/'} resp = await self.make_request( 'GET', @@ -726,13 +729,6 @@ async def _metadata_folder(self, path): if isinstance(prefixes, dict): prefixes = [prefixes] - # The user selected base folder, the root of the where that user's node is connected. - for item in prefixes: - item['base_folder'] = self.settings.get('id', ':/').split(':/')[1] - - for item in contents: - item['base_folder'] = self.settings.get('id', ':/').split(':/')[1] - items = [ S3FolderMetadata(item) for item in prefixes if item['Prefix'] != path.path