Skip to content

Commit 420ddee

Browse files
authored
remove bson dependency and use pymongo (#7022)
1 parent bc6f7f7 commit 420ddee

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

localstack/services/dynamodbstreams/dynamodbstreams_api.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import contextlib
12
import logging
23
import threading
34
import time
45
from typing import Dict
56

6-
import bson
7+
from bson.json_util import dumps
78

89
from localstack.aws.accounts import get_aws_account_id
910
from localstack.aws.api.dynamodbstreams import StreamStatus, StreamViewType
@@ -67,29 +68,25 @@ def forward_events(records: Dict) -> None:
6768
kinesis = aws_stack.connect_to_service("kinesis")
6869
for record in records:
6970
table_arn = record.pop("eventSourceARN", "")
70-
stream = get_stream_for_table(table_arn)
71-
if stream:
71+
if stream := get_stream_for_table(table_arn):
7272
table_name = table_name_from_stream_arn(stream["StreamArn"])
7373
stream_name = get_kinesis_stream_name(table_name)
7474
kinesis.put_record(
7575
StreamName=stream_name,
76-
Data=bson.dumps(record),
76+
Data=dumps(record),
7777
PartitionKey="TODO",
7878
)
7979

8080

8181
def delete_streams(table_arn: str) -> None:
8282
store = get_dynamodbstreams_store()
8383
table_name = table_name_from_table_arn(table_arn)
84-
stream = store.ddb_streams.pop(table_name, None)
85-
if stream:
84+
if store.ddb_streams.pop(table_name, None):
8685
stream_name = get_kinesis_stream_name(table_name)
87-
try:
86+
with contextlib.suppress(Exception):
8887
aws_stack.connect_to_service("kinesis").delete_stream(StreamName=stream_name)
8988
# sleep a bit, as stream deletion can take some time ...
9089
time.sleep(1)
91-
except Exception:
92-
pass # ignore "stream not found" errors
9390

9491

9592
def get_kinesis_stream_name(table_name: str) -> str:

localstack/services/dynamodbstreams/provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import copy
22
import logging
33

4-
import bson
4+
from bson.json_util import loads
55

66
from localstack.aws.api import RequestContext, handler
77
from localstack.aws.api.dynamodbstreams import (
@@ -103,7 +103,7 @@ def get_records(self, context: RequestContext, payload: GetRecordsInput) -> GetR
103103
"NextShardIterator": kinesis_records.get("NextShardIterator"),
104104
}
105105
for record in kinesis_records["Records"]:
106-
record_data = bson.loads(record["Data"])
106+
record_data = loads(record["Data"])
107107
record_data["dynamodb"]["SequenceNumber"] = record["SequenceNumber"]
108108
result["Records"].append(record_data)
109109
return GetRecordsOutput(**result)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ runtime =
9292
xmltodict>=0.11.0
9393
awscrt>=0.13.14
9494
vosk==0.3.43
95-
bson==0.5.10
95+
pymongo>=4.2.0
9696

9797
# @deprecated - use extra 'runtime' instead.
9898
full =

0 commit comments

Comments
 (0)