Skip to content

Commit b0fd88b

Browse files
committed
Adds doc comment and refactor if..else block
1 parent 4b9ce05 commit b0fd88b

2 files changed

Lines changed: 41 additions & 25 deletions

File tree

pyiceberg/catalog/glue.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -305,36 +305,44 @@ def add_glue_catalog_id(params: Dict[str, str], **kwargs: Any) -> None:
305305
class GlueCatalog(MetastoreCatalog):
306306
glue: GlueClient
307307

308-
def __init__(self, name: str, client: GlueClient | None = None, **properties: Any):
309-
super().__init__(name, **properties)
308+
def __init__(self, name: str, client: Optional[GlueClient] = None, **properties: Any):
309+
"""Glue Catalog.
310+
311+
You either need to provide a boto3 glue client, or one will be constructed from the properties.
310312
311-
retry_mode_prop_value = get_first_property_value(properties, GLUE_RETRY_MODE)
313+
Args:
314+
name: Name to identify the catalog.
315+
client: An optional boto3 glue client.
316+
properties: Properties for glue client construction and configuration.
317+
"""
318+
super().__init__(name, **properties)
312319

313320
if client:
314321
self.glue = client
315-
return
316-
317-
session = boto3.Session(
318-
profile_name=properties.get(GLUE_PROFILE_NAME),
319-
region_name=get_first_property_value(properties, GLUE_REGION, AWS_REGION),
320-
botocore_session=properties.get(BOTOCORE_SESSION),
321-
aws_access_key_id=get_first_property_value(properties, GLUE_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID),
322-
aws_secret_access_key=get_first_property_value(properties, GLUE_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY),
323-
aws_session_token=get_first_property_value(properties, GLUE_SESSION_TOKEN, AWS_SESSION_TOKEN),
324-
)
325-
self.glue: GlueClient = session.client(
326-
"glue",
327-
endpoint_url=properties.get(GLUE_CATALOG_ENDPOINT),
328-
config=Config(
329-
retries={
330-
"max_attempts": properties.get(GLUE_MAX_RETRIES, MAX_RETRIES),
331-
"mode": retry_mode_prop_value if retry_mode_prop_value in EXISTING_RETRY_MODES else STANDARD_RETRY_MODE,
332-
}
333-
),
334-
)
322+
else:
323+
retry_mode_prop_value = get_first_property_value(properties, GLUE_RETRY_MODE)
324+
325+
session = boto3.Session(
326+
profile_name=properties.get(GLUE_PROFILE_NAME),
327+
region_name=get_first_property_value(properties, GLUE_REGION, AWS_REGION),
328+
botocore_session=properties.get(BOTOCORE_SESSION),
329+
aws_access_key_id=get_first_property_value(properties, GLUE_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID),
330+
aws_secret_access_key=get_first_property_value(properties, GLUE_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY),
331+
aws_session_token=get_first_property_value(properties, GLUE_SESSION_TOKEN, AWS_SESSION_TOKEN),
332+
)
333+
self.glue: GlueClient = session.client(
334+
"glue",
335+
endpoint_url=properties.get(GLUE_CATALOG_ENDPOINT),
336+
config=Config(
337+
retries={
338+
"max_attempts": properties.get(GLUE_MAX_RETRIES, MAX_RETRIES),
339+
"mode": retry_mode_prop_value if retry_mode_prop_value in EXISTING_RETRY_MODES else STANDARD_RETRY_MODE,
340+
}
341+
),
342+
)
335343

336-
if glue_catalog_id := properties.get(GLUE_ID):
337-
_register_glue_catalog_id_with_glue_client(self.glue, glue_catalog_id)
344+
if glue_catalog_id := properties.get(GLUE_ID):
345+
_register_glue_catalog_id_with_glue_client(self.glue, glue_catalog_id)
338346

339347
def _convert_glue_to_iceberg(self, glue_table: TableTypeDef) -> Table:
340348
properties: Properties = glue_table["Parameters"]

tests/catalog/test_glue.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,3 +932,11 @@ def test_glue_endpoint_override(_bucket_initialize: None, moto_endpoint_url: str
932932
catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}", "glue.endpoint": test_endpoint}
933933
)
934934
assert test_catalog.glue.meta.endpoint_url == test_endpoint
935+
936+
937+
@mock_aws
938+
def test_glue_client_override() -> None:
939+
catalog_name = "glue"
940+
test_client = boto3.client("glue", region_name="us-west-2")
941+
test_catalog = GlueCatalog(catalog_name, test_client)
942+
assert test_catalog.glue is test_client

0 commit comments

Comments
 (0)