awsimple 4.1.0__py3-none-any.whl → 5.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of awsimple might be problematic. Click here for more details.

awsimple/__version__.py CHANGED
@@ -1,7 +1,7 @@
1
1
  __application_name__ = "awsimple"
2
2
  __title__ = __application_name__
3
3
  __author__ = "abel"
4
- __version__ = "4.1.0"
4
+ __version__ = "5.0.0"
5
5
  __author_email__ = "j@abel.co"
6
6
  __url__ = "https://github.com/jamesabel/awsimple"
7
7
  __download_url__ = "https://github.com/jamesabel/awsimple"
awsimple/pubsub.py CHANGED
@@ -3,6 +3,7 @@ pub/sub abstraction on top of AWS SNS and SQS using boto3.
3
3
  """
4
4
 
5
5
  import time
6
+ from functools import lru_cache
6
7
  from typing import Any, Dict, List, Callable, Union
7
8
  from datetime import timedelta
8
9
  from multiprocessing import Process, Queue, Event
@@ -13,6 +14,7 @@ import json
13
14
 
14
15
  from typeguard import typechecked
15
16
  from botocore.exceptions import ClientError
17
+ import strif
16
18
 
17
19
  from .sns import SNSAccess
18
20
  from .sqs import SQSPollAccess, get_all_sqs_queues
@@ -119,6 +121,20 @@ class _SubscriptionThread(Thread):
119
121
  self.new_event.set() # notify parent process that a new message is available
120
122
 
121
123
 
124
+ @lru_cache
125
+ def make_name_aws_safe(*args: str) -> str:
126
+ """
127
+ Make a name safe for an SQS queue to subscribe to an SNS topic. AWS has a bunch of undocumented restrictions on names, so we just hash the name to a base36 string.
128
+
129
+ :params: input name(s)
130
+ :return: AWS safe name
131
+ """
132
+
133
+ base36 = strif.hash_string("".join(args)).base36
134
+ assert len(base36) == 31
135
+ return base36
136
+
137
+
122
138
  class PubSub(Process):
123
139
 
124
140
  @typechecked()
@@ -136,12 +152,14 @@ class PubSub(Process):
136
152
  Pub and Sub.
137
153
  Create in a separate process to offload from main thread. Also facilitates use of moto mock in tests.
138
154
 
139
- :param channel: Channel name (SNS topic name).
155
+ :param channel: Channel name (used for SNS topic name). This must not be a prefix of other channel names to avoid collisions (don't name one channel "a" and another "ab").
140
156
  :param node_name: Node name (SQS queue name suffix). Defaults to a combination of computer name and username, but can be passed in for customization and/or testing.
141
157
  :param sub_callback: Optional thread and process safe callback function to be called when a new message is received. The function should accept a single argument, which will be the message as a dictionary.
142
158
  """
143
- self.channel = channel.lower() # when subscribing SQS queues to SNS topics, the names must all be lowercase (bizarre AWS "gotcha")
144
- self.node_name = node_name.lower() # e.g., computer name or user and computer name
159
+ self.aws_resource_prefix = "ps" # for pubsub
160
+ self.channel = self.aws_resource_prefix + make_name_aws_safe(channel) # prefix with ps (pubsub) to avoid collisions with other uses of SNS topics and SQS queues
161
+ self.node_name = node_name
162
+ self.sqs_queue_name = self.aws_resource_prefix + make_name_aws_safe(self.channel, self.node_name)
145
163
  self.sub_callback = sub_callback
146
164
 
147
165
  self.profile_name = profile_name
@@ -158,9 +176,6 @@ class PubSub(Process):
158
176
 
159
177
  def run(self):
160
178
 
161
- sqs_prefix = f"{self.channel}-"
162
- sqs_queue_name = f"{sqs_prefix}{self.node_name}"
163
-
164
179
  sns = SNSAccess(
165
180
  self.channel,
166
181
  auto_create=True,
@@ -170,11 +185,16 @@ class PubSub(Process):
170
185
  region_name=self.region_name,
171
186
  )
172
187
  sqs_metadata = _DynamoDBMetadataTable(
173
- sqs_name, sqs_queue_name, profile_name=self.profile_name, aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key, region_name=self.region_name
188
+ sqs_name,
189
+ self.sqs_queue_name,
190
+ profile_name=self.profile_name,
191
+ aws_access_key_id=self.aws_access_key_id,
192
+ aws_secret_access_key=self.aws_secret_access_key,
193
+ region_name=self.region_name,
174
194
  )
175
195
 
176
196
  sqs = SQSPollAccess(
177
- sqs_queue_name, profile_name=self.profile_name, aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key, region_name=self.region_name
197
+ self.sqs_queue_name, profile_name=self.profile_name, aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key, region_name=self.region_name
178
198
  )
179
199
  if not sqs.exists():
180
200
  sqs.create_queue()
@@ -188,7 +208,7 @@ class PubSub(Process):
188
208
  _connect_sns_to_sqs(sqs, sns)
189
209
 
190
210
  sqs_metadata.update_table_mtime() # update SQS use time (the existing infrastructure calls it a "table", but we're using it for the SQS queue)
191
- remove_old_queues(sqs_prefix) # clean up old queues
211
+ remove_old_queues(self.channel) # clean up old queues
192
212
 
193
213
  sqs_thread = _SubscriptionThread(sqs, self._new_event)
194
214
  sqs_thread.start()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: awsimple
3
- Version: 4.1.0
3
+ Version: 5.0.0
4
4
  Summary: Simple AWS API for S3, DynamoDB, SNS, and SQS
5
5
  Home-page: https://github.com/jamesabel/awsimple
6
6
  Download-URL: https://github.com/jamesabel/awsimple
@@ -22,6 +22,7 @@ Requires-Dist: tobool
22
22
  Requires-Dist: urllib3
23
23
  Requires-Dist: python-dateutil
24
24
  Requires-Dist: yasf
25
+ Requires-Dist: strif
25
26
  Dynamic: author
26
27
  Dynamic: author-email
27
28
  Dynamic: description
@@ -1,5 +1,5 @@
1
1
  awsimple/__init__.py,sha256=RT9hATlcKnnvJYnSh7tCWEeeT2LyleL_vT1Zdz0nSkM,1051
2
- awsimple/__version__.py,sha256=_8-4yMvCFU2QJpF5V0ofTEvVAuRaDuwWi01mBIjSNkU,323
2
+ awsimple/__version__.py,sha256=xCHPAAnvsCqjRKpsIAQOEFC8V53loQAyz0sN0R7SAo0,323
3
3
  awsimple/aws.py,sha256=NbRu1v_J5j2-pcefNZ5Xggii3mM9nHpeHMz9L9K9r-U,7653
4
4
  awsimple/cache.py,sha256=_LvPx76215t8KhAJOin6Pe2b4lWpB6kbKpdzgR4FeA4,7206
5
5
  awsimple/dynamodb.py,sha256=7MNxAutOCMTS4JSX-DLOwzaImJ2TzIc7kfQzQPAy5y8,41193
@@ -8,14 +8,14 @@ awsimple/exceptions.py,sha256=Ew-S8YkHVWrZFI_Yik5n0cJ7Ss4Kig5JsEPQ-9z18SU,922
8
8
  awsimple/logs.py,sha256=s9FhdDFWjfxGCVDx-FNTPgJ-YN1AOAgz4HNxTVfRARE,4108
9
9
  awsimple/mock.py,sha256=eScbnxFF9xAosOAsL-NZgp_P-fezB6StQMkb85Y3TNo,574
10
10
  awsimple/platform.py,sha256=TObvLIVgRGh-Mh4ZCxFfxAmrn8KNMHktr6XkDZX8JYE,376
11
- awsimple/pubsub.py,sha256=exWZWWI9NrE36fESZprhxU5Qj9BSgK5diYKQZHBOqBs,9546
11
+ awsimple/pubsub.py,sha256=Fxo3i4zo3GfBVhmGMGI4CQsRQ18x4Pp_bjpxSL-9xJY,10240
12
12
  awsimple/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  awsimple/s3.py,sha256=OhWF1uv4oLmBF5jAhKIi918iUQxx4CX8bTEvQ5wLYr8,24050
14
14
  awsimple/sns.py,sha256=T_FyN8eSmBPo213HOfB3UBlMrvtBK768IaRo_ks-7do,3526
15
15
  awsimple/sqs.py,sha256=9ZY7161CpmYpcxlCFIfW8bvMn9SGl4cgGR79I4MFLDk,17281
16
- awsimple-4.1.0.dist-info/licenses/LICENSE,sha256=d956YAgtDaxgxQmccyUk__EfhORZyBjvmJ8pq6zYTxk,1093
17
- awsimple-4.1.0.dist-info/licenses/LICENSE.txt,sha256=d956YAgtDaxgxQmccyUk__EfhORZyBjvmJ8pq6zYTxk,1093
18
- awsimple-4.1.0.dist-info/METADATA,sha256=AYF-IUhIYqU2zGs9K_L6J3XCq4RVzOaf2RP5lu1YRek,6638
19
- awsimple-4.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
20
- awsimple-4.1.0.dist-info/top_level.txt,sha256=mwqCoH_8vAaK6iYioiRbasXmVCQM7AK3grNWOKp2VHE,9
21
- awsimple-4.1.0.dist-info/RECORD,,
16
+ awsimple-5.0.0.dist-info/licenses/LICENSE,sha256=d956YAgtDaxgxQmccyUk__EfhORZyBjvmJ8pq6zYTxk,1093
17
+ awsimple-5.0.0.dist-info/licenses/LICENSE.txt,sha256=d956YAgtDaxgxQmccyUk__EfhORZyBjvmJ8pq6zYTxk,1093
18
+ awsimple-5.0.0.dist-info/METADATA,sha256=98gj_2M_A4xwO4-y5c8LLsRML1pfnoKeqE2-LCyEvMU,6660
19
+ awsimple-5.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
20
+ awsimple-5.0.0.dist-info/top_level.txt,sha256=mwqCoH_8vAaK6iYioiRbasXmVCQM7AK3grNWOKp2VHE,9
21
+ awsimple-5.0.0.dist-info/RECORD,,