Skip to content

Commit 95c5986

Browse files
committed
More pylint tidying
1 parent bf76c98 commit 95c5986

File tree

4 files changed

+51
-43
lines changed

4 files changed

+51
-43
lines changed

src/bulk_importer/main.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from cryptography.x509.oid import NameOID
1919
from cryptography.hazmat.backends import default_backend
2020
from cryptography.hazmat.primitives import hashes
21+
from aws_lambda_powertools.utilities.typing import LambdaContext
22+
from aws_lambda_powertools.utilities.data_classes import SQSEvent
2123

2224
logger = logging.getLogger()
2325
logger.setLevel("INFO")
@@ -58,7 +60,8 @@ def get_thing(thing_name):
5860
response = iot_client.describe_thing(thingName=thing_name)
5961
return response.get("thingArn")
6062
except ClientError as error:
61-
assert error.response['Error']['Code'] == 'ResourceNotFoundException'
63+
error_code = error.response['Error']['Code']
64+
assert error_code == 'ResourceNotFoundException'
6265
return None
6366

6467
def get_policy(policy_name):
@@ -67,12 +70,13 @@ def get_policy(policy_name):
6770
try:
6871
response = iot_client.get_policy(policyName=policy_name)
6972
return response.get('policyArn')
70-
except botocore.exceptions.ClientError as error:
71-
if error.response['Error']['Code'] == 'ResourceNotFoundException':
72-
print("ERROR: You need to configure the policy [" + policy_name +
73-
"] in your target region first.")
74-
if error.response['Error']['Code'] == 'UnauthorizedException':
75-
print("ERROR: There is a deployment problem with the attached Role."
73+
except ClientError as error:
74+
error_code = error.response['Error']['Code']
75+
if error_code == 'ResourceNotFoundException':
76+
logger.error("ERROR: You need to configure the policy {policy_name} "
77+
"in your target region first.")
78+
if error_code == 'UnauthorizedException':
79+
logger.error("There is a deployment problem with the attached Role."
7680
"Unable to reach IoT Core object.")
7781
return None
7882

@@ -83,13 +87,14 @@ def get_thing_group(thing_group_name):
8387
try:
8488
response = iot_client.describe_thing_group(thingGroupName=thing_group_name)
8589
return response.get('thingGroupArn')
86-
except botocore.exceptions.ClientError as error:
87-
if error.response['Error']['Code'] == 'ResourceNotFoundException':
88-
print("ERROR: You need to configure the Thing Group [" + thing_group_name +
89-
"] in your target region first.")
90-
if error.response['Error']['Code'] == 'UnauthorizedException':
91-
print("ERROR: There is a deployment problem with the attached Role. Unable"
92-
"to reach IoT Core object.")
90+
except ClientError as error:
91+
error_code = error.response['Error']['Code']
92+
if 'ResourceNotFoundException' == error_code:
93+
logger.error("You need to configure the Thing Group {thing_group_name} "
94+
"in your target region first.")
95+
if 'UnauthorizedException' == error_code:
96+
logger.error("There is a deployment problem with the attached Role. Unable"
97+
"to reach IoT Core object.")
9398
return None
9499

95100
def get_thing_type(type_name):
@@ -99,12 +104,13 @@ def get_thing_type(type_name):
99104
response = iot_client.describeThingType(thingTypeName=type_name)
100105
return response.get('thingTypeArn')
101106
except ClientError as error:
102-
if error.response['Error']['Code'] == 'ResourceNotFoundException':
103-
print("ERROR: You need to configure the Thing Type [" + type_name +
104-
"] in your target region first.")
105-
if error.response['Error']['Code'] == 'UnauthorizedException':
106-
print("ERROR: There is a deployment problem with the attached Role."
107-
"Unable to reach IoT Core object.")
107+
error_code = error.response['Error']['Code']
108+
if 'ResourceNotFoundException' == error_code:
109+
logger.error("You need to configure the Thing Type {type_name} "
110+
"in your target region first.")
111+
if 'UnauthorizedException' == error_code:
112+
logger.error("There is a deployment problem with the attached Role."
113+
"Unable to reach IoT Core object.")
108114
return None
109115

110116
def process_policy(policy_name, certificate_id):
@@ -121,7 +127,8 @@ def process_thing(thing_name, certificate_id, thing_type_name):
121127
iot_client.describe_thing(thingName=thing_name)
122128
return None
123129
except ClientError as error:
124-
print("Thing not found (" + error.response['Error']['Code'] + "). Creating.")
130+
error_code = error.response['Error']['Code']
131+
logger.info("Thing not found {error_code}. Creating.")
125132

126133
# Create thing
127134
try:
@@ -132,8 +139,8 @@ def process_thing(thing_name, certificate_id, thing_type_name):
132139
thingTypeName=thing_type_name)
133140

134141
except ClientError as error:
135-
print("ERROR Thing creation failed.")
136-
print(error)
142+
error_code = error.response['Error']['Code']
143+
logger.error("ERROR Thing creation failed: {error_code}")
137144
return None
138145

139146
try:
@@ -182,11 +189,9 @@ def process_certificate(config, requeue_cb):
182189
fingerprint, error.response['Error']['Code'])
183190

184191
try:
185-
print("Importing certificate.")
186192
response = iot_client.register_certificate_without_ca(
187193
certificatePem=certificate_text.decode('ascii'),
188194
status='ACTIVE')
189-
print("Certificate imported.")
190195
return response.get("certificateId")
191196
except botocore.exceptions.ClientError as e:
192197
if e.response['Error']['Code'] == 'ThrottlingException':
@@ -248,7 +253,7 @@ def process_sqs(config):
248253
process_policy(policy_name, certificate_id)
249254
process_thing_group(thing_group_name, thing_name)
250255

251-
def lambda_handler(event, context):
256+
def lambda_handler(event: SQSEvent, context: LambdaContext):
252257
"""Lambda function main entry point"""
253258
if event.get('Records') is None:
254259
print("ERROR: Configuration incorrect: no event record on invoke")
@@ -264,9 +269,4 @@ def lambda_handler(event, context):
264269
config = json.loads(record["body"])
265270
process_sqs(config)
266271

267-
return {
268-
"statusCode": 204,
269-
"body": json.dumps({
270-
"message": "Processing succeeded."
271-
})
272-
}
272+
return event

src/provider_infineon/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
from aws_lambda_powertools.utilities.typing import LambdaContext
1919
from aws_lambda_powertools.utilities.data_classes import S3Event, event_source
2020

21-
from . import schemas
22-
2321
def s3_object_stream(bucket_name: str, object_name: str):
2422
"""Given a bucket and object, verify its existence and return the resource."""
2523
s3 = boto3resource('s3')

test/unit/src/test_provider_espressif.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
from moto import mock_aws, settings
2323
from aws_lambda_powertools.utilities.validation import validate
2424

25-
from src.provider_espressif.testable import LambdaS3Class, LambdaSQSClass # pylint: disable=wrong-import-position
2625
from src.provider_espressif.main import lambda_handler, s3_filebuf_bytes, invoke_export # pylint: disable=wrong-import-position
2726
from src.provider_espressif.main import s3_object_stream
27+
from .model_provider_espressif import LambdaS3Class, LambdaSQSClass # pylint: disable=wrong-import-position
2828

2929
@mock_aws(config={
3030
"core": {
@@ -63,18 +63,21 @@ def test_pos_s3_object_resource(self):
6363

6464
def test_neg_s3_object_resource(self):
6565
with pytest.raises(botocore.exceptions.ClientError) as e:
66+
# Although this returns a value, no need to define var for it
6667
r = s3_object_stream("unit_test_s3_buckets", "manifest")
67-
assert str(e.value) == "An error occurred (NoSuchBucket) when calling the HeadObject operation: The specified bucket does not exist"
68+
assert str(e.value) == "An error occurred (NoSuchBucket) when calling " \
69+
"the HeadObject operation: The specified bucket " \
70+
"does not exist"
6871

6972
def test_pos_s3_filebuf_bytes(self):
70-
# The bytes should equal to the object in the bucket
73+
""" The bytes should equal to the object in the bucket """
7174
v = s3_filebuf_bytes("unit_test_s3_bucket", "manifest.csv")
7275
assert v == self.test_s3_object_content.read()
7376

7477
def test_pos_invoke_export(self):
78+
""" The number of items in the queue should be 7 since there are
79+
seven certificates in the test file """
7580
invoke_export("unit_test_s3_bucket", "manifest.csv", "provider")
76-
# The number of items in the queue should be 7 since there are
77-
# seven certificates in the test file
7881
sqs_client = client("sqs", "us-east-1")
7982
sqs_queue_url_r = sqs_client.get_queue_url(QueueName=self.test_sqs_queue_name)
8083
sqs_queue_url = sqs_queue_url_r['QueueUrl']

test/unit/src/test_provider_infineon.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#from moto import mock_aws, settings
1919
#from unittest.mock import MagicMock, patch
2020

21-
from src.provider_infineon.testable import LambdaS3Class, LambdaSQSClass
2221
from src.provider_infineon.main import s3_object_stream, s3_filebuf_bytes
2322
from src.provider_infineon.main import lambda_handler, invoke_export
23+
from .model_provider_infineon import LambdaS3Class, LambdaSQSClass
2424

2525
@mock_aws(config={
2626
"core": {
@@ -39,7 +39,8 @@ def setUp(self):
3939
s3_client.create_bucket(Bucket = self.test_s3_bucket_name )
4040
with open('./test/artifacts/manifest-espressif.csv', 'rb') as data:
4141
s3_client.put_object(Bucket=self.test_s3_bucket_name, Key="manifest.csv", Body=data)
42-
self.test_s3_object_content = s3_client.get_object(Bucket=self.test_s3_bucket_name, Key="manifest.csv")['Body']
42+
self.test_s3_object_content = s3_client.get_object(Bucket=self.test_s3_bucket_name,
43+
Key="manifest.csv")['Body']
4344
mocked_s3_resource = resource("s3")
4445
mocked_s3_resource = { "resource" : resource('s3'),
4546
"bucket_name" : self.test_s3_bucket_name }
@@ -54,15 +55,21 @@ def setUp(self):
5455
self.mocked_sqs_class = LambdaSQSClass(mocked_sqs_resource)
5556

5657
def test_pos_s3_object_resource(self):
58+
"""Basic pos test case for object resource"""
5759
r = s3_object_stream("unit_test_s3_bucket", "manifest.csv")
5860
assert isinstance(r, io.BytesIO)
5961

6062
def test_neg_s3_object_resource(self):
63+
"""Basic neg test case for object resource"""
6164
with pytest.raises(botocore.exceptions.ClientError) as e:
62-
r = s3_object_stream("unit_test_s3_buckets", "manifest")
63-
assert str(e.value) == "An error occurred (NoSuchBucket) when calling the HeadObject operation: The specified bucket does not exist"
65+
# Although this returns a value, no need to define var for it
66+
s3_object_stream("unit_test_s3_buckets", "manifest")
67+
errstr = "An error occurred (NoSuchBucket) when calling the " \
68+
"HeadObject operation: The specified bucket does not exist"
69+
assert str(e.value) == errstr
6470

6571
def test_pos_s3_filebuf_bytes(self):
72+
"""Basic pos test case for byte buffer handling"""
6673
# The bytes should equal to the object in the bucket
6774
v = s3_filebuf_bytes("unit_test_s3_bucket", "manifest.csv")
6875
assert v == self.test_s3_object_content.read()

0 commit comments

Comments
 (0)