Skip to content

Commit 9eac7e5

Browse files
committed
simplify the code
1 parent cadb934 commit 9eac7e5

1 file changed

Lines changed: 41 additions & 74 deletions

File tree

synapseclient/models/storage_location.py

Lines changed: 41 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,6 @@ class UploadType(str, Enum):
128128
},
129129
}
130130

131-
# Subset of _STORAGE_TYPE_SPECIFIC_FIELDS that are required (no default value).
132-
# Fields with defaults (e.g. base_key=None, sts_enabled=False) are omitted.
133-
_REQUIRED_STORAGE_TYPE_SPECIFIC_FIELDS: Dict[str, set] = {
134-
StorageLocationType.EXTERNAL_S3: {"bucket", "endpoint_url"},
135-
StorageLocationType.EXTERNAL_GOOGLE_CLOUD: {"bucket"},
136-
StorageLocationType.EXTERNAL_OBJECT_STORE: {"bucket", "endpoint_url"},
137-
StorageLocationType.EXTERNAL_SFTP: {"url"},
138-
StorageLocationType.EXTERNAL_HTTPS: {"url"},
139-
StorageLocationType.PROXY: {"proxy_url", "secret_key", "benefactor_id"},
140-
}
141-
142131

143132
@dataclass()
144133
@async_to_sync
@@ -161,31 +150,32 @@ class StorageLocation(EnumCoercionMixin, StorageLocationSynchronousProtocol):
161150
162151
Attributes:
163152
bucket: The name of the S3 or Google Cloud Storage bucket. Applicable to
164-
SYNAPSE_S3, EXTERNAL_S3, EXTERNAL_GOOGLE_CLOUD, and
165-
EXTERNAL_OBJECT_STORE types.
153+
`SYNAPSE_S3`, `EXTERNAL_S3`, `EXTERNAL_GOOGLE_CLOUD`, and
154+
`EXTERNAL_OBJECT_STORE` types.
166155
base_key: The optional base key (prefix/folder) within the bucket.
167-
Applicable to SYNAPSE_S3, EXTERNAL_S3, and EXTERNAL_GOOGLE_CLOUD types.
156+
Applicable to `SYNAPSE_S3`, `EXTERNAL_S3`, and `EXTERNAL_GOOGLE_CLOUD`
157+
types.
168158
sts_enabled: Whether STS (AWS Security Token Service) is enabled on this
169-
storage location. Applicable to SYNAPSE_S3 and EXTERNAL_S3 types.
159+
storage location. Applicable to `SYNAPSE_S3` and `EXTERNAL_S3` types.
170160
endpoint_url: The endpoint URL of the S3 service. Applicable to
171-
EXTERNAL_S3 (default: https://s3.amazonaws.com) and
172-
EXTERNAL_OBJECT_STORE types.
161+
`EXTERNAL_S3` (default: https://s3.amazonaws.com) and
162+
`EXTERNAL_OBJECT_STORE` types.
173163
174164
Attributes:
175165
url: The base URL for uploading to the external destination. Applicable to
176-
EXTERNAL_SFTP type.
166+
`EXTERNAL_SFTP` type.
177167
supports_subfolders: Whether the destination supports creating subfolders
178-
under the base url. Applicable to EXTERNAL_SFTP type. Default: False.
168+
under the base url. Applicable to `EXTERNAL_SFTP` type. Default: False.
179169
180170
Attributes:
181171
proxy_url: The HTTPS URL of the proxy used for upload and download.
182-
Applicable to PROXY type.
172+
Applicable to `PROXY` type.
183173
secret_key: The encryption key used to sign all pre-signed URLs used to
184-
communicate with the proxy. Applicable to PROXY type.
174+
communicate with the proxy. Applicable to `PROXY` type.
185175
benefactor_id: An Entity ID (such as a Project ID). When set, any user with
186176
the 'create' permission on the given benefactorId will be allowed to
187177
create ProxyFileHandle using its storage location ID. Applicable to
188-
PROXY type.
178+
`PROXY` type.
189179
190180
Attributes:
191181
upload_type: (Read Only) The upload type for this storage location.
@@ -261,43 +251,43 @@ class StorageLocation(EnumCoercionMixin, StorageLocationSynchronousProtocol):
261251

262252
# S3/GCS specific fields
263253
bucket: Optional[str] = None
264-
"""The name of the S3 or Google Cloud Storage bucket. Applicable to SYNAPSE_S3,
265-
EXTERNAL_S3, EXTERNAL_GOOGLE_CLOUD, and EXTERNAL_OBJECT_STORE types."""
254+
"""The name of the S3 or Google Cloud Storage bucket. Applicable to `SYNAPSE_S3`,
255+
`EXTERNAL_S3`, `EXTERNAL_GOOGLE_CLOUD`, and `EXTERNAL_OBJECT_STORE` types."""
266256

267257
base_key: Optional[str] = None
268258
"""The optional base key (prefix/folder) within the bucket. Applicable to
269-
SYNAPSE_S3, EXTERNAL_S3, and EXTERNAL_GOOGLE_CLOUD types."""
259+
`SYNAPSE_S3`, `EXTERNAL_S3`, and `EXTERNAL_GOOGLE_CLOUD` types."""
270260

271261
sts_enabled: Optional[bool] = False
272262
"""Whether STS (AWS Security Token Service) is enabled on this storage location.
273-
Applicable to SYNAPSE_S3 and EXTERNAL_S3 types. Default: False."""
263+
Applicable to `SYNAPSE_S3` and `EXTERNAL_S3` types. Default: False."""
274264

275265
endpoint_url: Optional[str] = "https://s3.amazonaws.com"
276-
"""The endpoint URL of the S3 service. Applicable to EXTERNAL_S3
277-
(default: https://s3.amazonaws.com) and EXTERNAL_OBJECT_STORE types."""
266+
"""The endpoint URL of the S3 service. Applicable to `EXTERNAL_S3`
267+
(default: https://s3.amazonaws.com) and `EXTERNAL_OBJECT_STORE` types."""
278268

279269
# SFTP specific fields
280270
url: Optional[str] = None
281271
"""The base URL for uploading to the external destination. Applicable to
282-
EXTERNAL_SFTP type."""
272+
`EXTERNAL_SFTP` type."""
283273

284274
supports_subfolders: Optional[bool] = False
285275
"""Whether the destination supports creating subfolders under the base url.
286-
Applicable to EXTERNAL_SFTP type. Default: False."""
276+
Applicable to `EXTERNAL_SFTP` type. Default: False."""
287277

288278
# Proxy specific fields
289279
proxy_url: Optional[str] = None
290280
"""The HTTPS URL of the proxy used for upload and download. Applicable to
291-
PROXY type."""
281+
`PROXY` type."""
292282

293283
secret_key: Optional[str] = None
294284
"""The encryption key used to sign all pre-signed URLs used to communicate
295-
with the proxy. Applicable to PROXY type."""
285+
with the proxy. Applicable to `PROXY` type."""
296286

297287
benefactor_id: Optional[str] = None
298288
"""An Entity ID (such as a Project ID). When set, any user with the 'create'
299289
permission on the given benefactorId will be allowed to create ProxyFileHandle
300-
using its storage location ID. Applicable to PROXY type."""
290+
using its storage location ID. Applicable to `PROXY` type."""
301291

302292
# Read-only fields
303293
upload_type: Optional[UploadType] = field(default=None, compare=False)
@@ -343,58 +333,35 @@ def fill_from_dict(self, synapse_response: Dict[str, Any]) -> "StorageLocation":
343333
The StorageLocation object.
344334
"""
345335
self.storage_location_id = synapse_response.get("storageLocationId", None)
346-
self.banner = (
347-
synapse_response.get("banner", None)
348-
if synapse_response.get("banner", None) is not None
349-
else None
350-
)
351-
self.description = (
352-
synapse_response.get("description", None)
353-
if synapse_response.get("description", None) is not None
354-
else None
355-
)
356-
self.etag = (
357-
synapse_response.get("etag", None)
358-
if synapse_response.get("etag", None) is not None
359-
else None
360-
)
361-
self.created_on = (
362-
synapse_response.get("createdOn", None)
363-
if synapse_response.get("createdOn", None) is not None
364-
else None
365-
)
366-
self.created_by = (
367-
synapse_response.get("createdBy", None)
368-
if synapse_response.get("createdBy", None) is not None
369-
else None
370-
)
336+
self.banner = synapse_response.get("banner", None)
337+
self.description = synapse_response.get("description", None)
338+
self.etag = synapse_response.get("etag", None)
339+
self.created_on = synapse_response.get("createdOn", None)
340+
self.created_by = synapse_response.get("createdBy", None)
371341

372-
self.upload_type = (
373-
synapse_response.get("uploadType", None)
374-
if synapse_response.get("uploadType", None) is not None
375-
else None
376-
)
342+
self.upload_type = synapse_response.get("uploadType", None)
377343

378344
# Parse storage type from concreteType + uploadType.
379345
# Both are needed to distinguish EXTERNAL_SFTP from EXTERNAL_HTTPS.
380-
self.concrete_type = (
381-
synapse_response.get("concreteType", "")
382-
if synapse_response.get("concreteType", "") is not None
383-
else None
384-
)
346+
self.concrete_type = synapse_response.get("concreteType", "")
385347
if self.concrete_type:
386348
type_suffix = (
387349
self.concrete_type.split(".")[-1] if "." in self.concrete_type else ""
388350
)
389351
key = (type_suffix, self.upload_type)
390352
if key in _CONCRETE_UPLOAD_TO_STORAGE_TYPE:
391353
self.storage_type = _CONCRETE_UPLOAD_TO_STORAGE_TYPE[key]
392-
# Type-specific fields — only populate attributes relevant to this storage type
393-
if self.storage_type:
394-
for field_name, api_key in _STORAGE_TYPE_SPECIFIC_FIELDS.get(
395-
self.storage_type, {}
396-
).items():
397-
setattr(self, field_name, synapse_response.get(api_key, None))
354+
# Type-specific fields — only populate attributes relevant to this storage type
355+
for field_name, api_key in _STORAGE_TYPE_SPECIFIC_FIELDS.get(
356+
self.storage_type, {}
357+
).items():
358+
setattr(self, field_name, synapse_response.get(api_key, None))
359+
else:
360+
Synapse.get_client().logger.warning(
361+
f"Unrecognized concreteType/uploadType pair "
362+
f"({self.concrete_type}, {self.upload_type.value}); "
363+
"storage_type will not be set and type-specific fields will be empty."
364+
)
398365
return self
399366

400367
def _to_synapse_request(self) -> Dict[str, Any]:

0 commit comments

Comments
 (0)