From 16d0ed67fc52be2c87cbb46e5804d692a2e296c9 Mon Sep 17 00:00:00 2001 From: ian-whitestone Date: Sun, 13 Jun 2021 16:27:28 -0400 Subject: [PATCH] Reformat files with black round 2 --- tests/test_handler.py | 30 +++++++--- tests/tests.py | 112 +++++++++++++++++++++++++++++--------- tests/tests_middleware.py | 4 +- tests/tests_placebo.py | 15 +++-- zappa/cli.py | 18 ++++-- zappa/core.py | 43 +++++++++++---- zappa/letsencrypt.py | 23 ++++++-- 7 files changed, 187 insertions(+), 58 deletions(-) diff --git a/tests/test_handler.py b/tests/test_handler.py index ad44aff34..1089ef798 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -82,7 +82,9 @@ def test_wsgi_script_name_on_aws_url(self): "resource": "/{proxy+}", "requestContext": {}, "queryStringParameters": {}, - "headers": {"Host": "1234567890.execute-api.us-east-1.amazonaws.com",}, + "headers": { + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + }, "pathParameters": {"proxy": "return/request/url"}, "httpMethod": "GET", "stageVariables": {}, @@ -108,7 +110,9 @@ def test_wsgi_script_name_on_domain_url(self): "resource": "/{proxy+}", "requestContext": {}, "queryStringParameters": {}, - "headers": {"Host": "example.com",}, + "headers": { + "Host": "example.com", + }, "pathParameters": {"proxy": "return/request/url"}, "httpMethod": "GET", "stageVariables": {}, @@ -131,7 +135,9 @@ def test_wsgi_script_name_with_multi_value_header(self): "resource": "/{proxy+}", "requestContext": {}, "queryStringParameters": {}, - "multiValueHeaders": {"Host": ["example.com"],}, + "multiValueHeaders": { + "Host": ["example.com"], + }, "pathParameters": {"proxy": "return/request/url"}, "httpMethod": "GET", "stageVariables": {}, @@ -152,7 +158,9 @@ def test_wsgi_script_name_with_multi_value_querystring(self): "resource": "/{proxy+}", "requestContext": {}, "multiValueQueryStringParameters": {"multi": ["value", "qs"]}, - "multiValueHeaders": {"Host": ["example.com"],}, + "multiValueHeaders": { + "Host": ["example.com"], + }, "pathParameters": {"proxy": "return/request/url"}, "httpMethod": "GET", "stageVariables": {}, @@ -200,7 +208,9 @@ def test_exception_handler_on_web_request(self): "resource": "/{proxy+}", "requestContext": {}, "queryStringParameters": {}, - "headers": {"Host": "1234567890.execute-api.us-east-1.amazonaws.com",}, + "headers": { + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + }, "pathParameters": {"proxy": "return/request/url"}, "httpMethod": "GET", "stageVariables": {}, @@ -254,7 +264,10 @@ def test_bot_triggered_event(self): "messageVersion": "1.0", "invocationSource": "DialogCodeHook", "userId": "user-id specified in the POST request to Amazon Lex.", - "sessionAttributes": {"key1": "value1", "key2": "value2",}, + "sessionAttributes": { + "key1": "value1", + "key2": "value2", + }, "bot": {"name": "bot-name", "alias": "bot-alias", "version": "bot-version"}, "outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request", "currentIntent": { @@ -282,7 +295,10 @@ def test_exception_in_bot_triggered_event(self): "messageVersion": "1.0", "invocationSource": "DialogCodeHook", "userId": "user-id specified in the POST request to Amazon Lex.", - "sessionAttributes": {"key1": "value1", "key2": "value2",}, + "sessionAttributes": { + "key1": "value1", + "key2": "value2", + }, "bot": {"name": "bot-name", "alias": "bot-alias", "version": "bot-version"}, "outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request", "currentIntent": { diff --git a/tests/tests.py b/tests/tests.py index 5b35e19e2..52c3a6eb7 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -270,7 +270,10 @@ def test_getting_installed_packages_mixed_case_location(self, *args): z.get_installed_packages( "/venv/Site-packages", "/venv/site-packages64" ), - {"superpackage": "0.1", "superpackage64": "0.1",}, + { + "superpackage": "0.1", + "superpackage64": "0.1", + }, ) def test_getting_installed_packages_mixed_case(self, *args): @@ -1787,7 +1790,9 @@ def get_domain_name(domain, *_args, **_kwargs): # Test creating domain without Route53 zappa_cli.zappa_settings["stage"].update( - {"route53_enabled": False,} + { + "route53_enabled": False, + } ) zappa_cli.zappa.reset_mock() zappa_cli.zappa.domain_names["test.example.com"] = "" @@ -1881,7 +1886,10 @@ def test_get_all_zones_two_pages(self, client): zones = zappa_core.get_all_zones() zappa_core.route53.list_hosted_zones.assert_has_calls( - [mock.call(MaxItems="100"), mock.call(MaxItems="100", Marker="101"),] + [ + mock.call(MaxItems="100"), + mock.call(MaxItems="100", Marker="101"), + ] ) self.assertListEqual(zones["HostedZones"], [{"Id": "zone1"}, {"Id": "zone2"}]) @@ -2376,7 +2384,9 @@ def test_zappa_core_deploy_lambda_alb(self): ) elbv2_stubber.add_response( "describe_load_balancers", - expected_params={"LoadBalancerArns": [loadbalancer_arn],}, + expected_params={ + "LoadBalancerArns": [loadbalancer_arn], + }, service_response={ "LoadBalancers": [ {"LoadBalancerArn": loadbalancer_arn, "State": {"Code": "active"}} @@ -2400,8 +2410,17 @@ def test_zappa_core_deploy_lambda_alb(self): elbv2_stubber.add_response( "create_target_group", - expected_params={"Name": kwargs["lambda_name"], "TargetType": "lambda",}, - service_response={"TargetGroups": [{"TargetGroupArn": targetgroup_arn,}]}, + expected_params={ + "Name": kwargs["lambda_name"], + "TargetType": "lambda", + }, + service_response={ + "TargetGroups": [ + { + "TargetGroupArn": targetgroup_arn, + } + ] + }, ) elbv2_stubber.add_response( "modify_target_group_attributes", @@ -2443,10 +2462,15 @@ def test_zappa_core_deploy_lambda_alb(self): "create_listener", expected_params={ "Certificates": [ - {"CertificateArn": kwargs["alb_vpc_config"]["CertificateArn"],} + { + "CertificateArn": kwargs["alb_vpc_config"]["CertificateArn"], + } ], "DefaultActions": [ - {"Type": "forward", "TargetGroupArn": targetgroup_arn,} + { + "Type": "forward", + "TargetGroupArn": targetgroup_arn, + } ], "LoadBalancerArn": loadbalancer_arn, "Protocol": "HTTPS", @@ -2491,48 +2515,77 @@ def test_zappa_core_undeploy_lambda_alb(self): ) elbv2_stubber.add_response( "describe_load_balancers", - expected_params={"Names": [kwargs["lambda_name"]],}, + expected_params={ + "Names": [kwargs["lambda_name"]], + }, service_response={ - "LoadBalancers": [{"LoadBalancerArn": loadbalancer_arn,}] + "LoadBalancers": [ + { + "LoadBalancerArn": loadbalancer_arn, + } + ] }, ) elbv2_stubber.add_response( "describe_listeners", - expected_params={"LoadBalancerArn": loadbalancer_arn,}, - service_response={"Listeners": [{"ListenerArn": listener_arn,}]}, + expected_params={ + "LoadBalancerArn": loadbalancer_arn, + }, + service_response={ + "Listeners": [ + { + "ListenerArn": listener_arn, + } + ] + }, ) elbv2_stubber.add_response( "delete_listener", - expected_params={"ListenerArn": listener_arn,}, + expected_params={ + "ListenerArn": listener_arn, + }, service_response={}, ) elbv2_stubber.add_response( "delete_load_balancer", - expected_params={"LoadBalancerArn": loadbalancer_arn,}, + expected_params={ + "LoadBalancerArn": loadbalancer_arn, + }, service_response={}, ) elbv2_stubber.add_client_error( - "describe_load_balancers", service_error_code="LoadBalancerNotFound", + "describe_load_balancers", + service_error_code="LoadBalancerNotFound", ) lambda_stubber.add_response( "get_function", - expected_params={"FunctionName": kwargs["lambda_name"],}, + expected_params={ + "FunctionName": kwargs["lambda_name"], + }, service_response={"Configuration": {"FunctionArn": function_arn}}, ) elbv2_stubber.add_response( "describe_target_groups", - expected_params={"Names": [kwargs["lambda_name"]],}, - service_response={"TargetGroups": [{"TargetGroupArn": targetgroup_arn}],}, + expected_params={ + "Names": [kwargs["lambda_name"]], + }, + service_response={ + "TargetGroups": [{"TargetGroupArn": targetgroup_arn}], + }, ) elbv2_stubber.add_response( - "deregister_targets", service_response={}, + "deregister_targets", + service_response={}, ) elbv2_stubber.add_client_error( - "describe_target_health", service_error_code="InvalidTarget", + "describe_target_health", + service_error_code="InvalidTarget", ) elbv2_stubber.add_response( "delete_target_group", - expected_params={"TargetGroupArn": targetgroup_arn,}, + expected_params={ + "TargetGroupArn": targetgroup_arn, + }, service_response={}, ) lambda_stubber.activate() @@ -2552,9 +2605,12 @@ def test_set_lambda_concurrency(self, client): "FunctionArn": "abc", "Version": 1, } - access_logging_patch = zappa_core.create_lambda_function(concurrency=5,) + access_logging_patch = zappa_core.create_lambda_function( + concurrency=5, + ) boto_mock.client().put_function_concurrency.assert_called_with( - FunctionName="abc", ReservedConcurrentExecutions=5, + FunctionName="abc", + ReservedConcurrentExecutions=5, ) @mock.patch("botocore.client") @@ -2571,10 +2627,13 @@ def test_update_lambda_concurrency(self, client): "Version": 1, } access_logging_patch = zappa_core.update_lambda_function( - bucket="test", function_name="abc", concurrency=5, + bucket="test", + function_name="abc", + concurrency=5, ) boto_mock.client().put_function_concurrency.assert_called_with( - FunctionName="abc", ReservedConcurrentExecutions=5, + FunctionName="abc", + ReservedConcurrentExecutions=5, ) boto_mock.client().delete_function_concurrency.assert_not_called() @@ -2592,7 +2651,8 @@ def test_delete_lambda_concurrency(self, client): "Version": 1, } access_logging_patch = zappa_core.update_lambda_function( - bucket="test", function_name="abc", + bucket="test", + function_name="abc", ) boto_mock.client().put_function_concurrency.assert_not_called() boto_mock.client().delete_function_concurrency.assert_called_with( diff --git a/tests/tests_middleware.py b/tests/tests_middleware.py index 40aff46e7..81d82128d 100644 --- a/tests/tests_middleware.py +++ b/tests/tests_middleware.py @@ -161,7 +161,9 @@ def test_wsgi_map_context_headers_handling(self): "body": {}, "headers": {"Content-Type": "application/json"}, "pathParameters": {"proxy": "v1/runs"}, - "requestContext": {"authorizer": {"principalId": "user1"},}, + "requestContext": { + "authorizer": {"principalId": "user1"}, + }, "query": {}, } diff --git a/tests/tests_placebo.py b/tests/tests_placebo.py index 8836e18e1..91783c055 100644 --- a/tests/tests_placebo.py +++ b/tests/tests_placebo.py @@ -49,7 +49,8 @@ def test_upload_remove_s3(self, session): # will throw ClientError with 404 if object doesn't exist s3.meta.client.head_object( - Bucket=bucket_name, Key=zip_path, + Bucket=bucket_name, + Key=zip_path, ) res = z.remove_from_s3(zip_path, bucket_name) self.assertTrue(res) @@ -77,7 +78,8 @@ def test_copy_on_s3(self, session): # will throw ClientError with 404 if object doesn't exist s3.meta.client.head_object( - Bucket=bucket_name, Key=zip_path, + Bucket=bucket_name, + Key=zip_path, ) zp = "copy_" + zip_path res = z.copy_on_s3(zip_path, zp, bucket_name) @@ -102,7 +104,9 @@ def test_create_lambda_function_s3(self, session): ) arn = z.update_lambda_function( - bucket=bucket_name, s3_key=zip_path, function_name="test_lmbda_function55", + bucket=bucket_name, + s3_key=zip_path, + function_name="test_lmbda_function55", ) @placebo_session @@ -223,7 +227,10 @@ def test_handler(self, session): event = { "body": {}, "headers": {}, - "params": {"parameter_1": "asdf1", "parameter_2": "asdf2",}, + "params": { + "parameter_1": "asdf1", + "parameter_2": "asdf2", + }, "method": "GET", "query": {}, } diff --git a/zappa/cli.py b/zappa/cli.py index 6ba6dd5fe..44f462753 100755 --- a/zappa/cli.py +++ b/zappa/cli.py @@ -678,7 +678,9 @@ def dispatch_command(self, command, stage): command = command_tail[0] # ex: zappa manage dev showmigrations admin self.invoke( - command, command="manage", no_color=self.vargs["no_color"], + command, + command="manage", + no_color=self.vargs["no_color"], ) elif command == "tail": # pragma: no cover @@ -1499,7 +1501,9 @@ def unschedule(self): print("Unscheduling..") self.zappa.unschedule_events( - lambda_name=self.lambda_name, lambda_arn=function_arn, events=events, + lambda_name=self.lambda_name, + lambda_arn=function_arn, + events=events, ) # Remove async task SNS @@ -1528,7 +1532,9 @@ def invoke(self, function_name, raw_python=False, command=None, no_color=False): import json as json response = self.zappa.invoke_lambda_function( - self.lambda_name, json.dumps(command), invocation_type="RequestResponse", + self.lambda_name, + json.dumps(command), + invocation_type="RequestResponse", ) if "LogResult" in response: @@ -2912,8 +2918,10 @@ def get_zappa_settings_string(self): # If slim handler, path to project zip if self.stage_config.get("slim_handler", False): - settings_s += "ARCHIVE_PATH='s3://{0!s}/{1!s}_{2!s}_current_project.tar.gz'\n".format( - self.s3_bucket_name, self.api_stage, self.project_name + settings_s += ( + "ARCHIVE_PATH='s3://{0!s}/{1!s}_{2!s}_current_project.tar.gz'\n".format( + self.s3_bucket_name, self.api_stage, self.project_name + ) ) # since includes are for slim handler add the setting here by joining arbitrary list from zappa_settings file diff --git a/zappa/core.py b/zappa/core.py index d4737727e..78fc5ff5b 100644 --- a/zappa/core.py +++ b/zappa/core.py @@ -1051,7 +1051,9 @@ def upload_to_s3(self, source_path, bucket_name, disable_progress=False): # it's actually "US Standard", or something. # More here: https://github.com/boto/boto3/issues/125 if self.aws_region == "us-east-1": - self.s3_client.create_bucket(Bucket=bucket_name,) + self.s3_client.create_bucket( + Bucket=bucket_name, + ) else: self.s3_client.create_bucket( Bucket=bucket_name, @@ -1241,7 +1243,8 @@ def create_lambda_function( if concurrency is not None: self.lambda_client.put_function_concurrency( - FunctionName=resource_arn, ReservedConcurrentExecutions=concurrency, + FunctionName=resource_arn, + ReservedConcurrentExecutions=concurrency, ) return resource_arn @@ -1284,7 +1287,8 @@ def update_lambda_function( # https://github.com/Miserlou/Zappa/issues/1823 try: response = self.lambda_client.get_alias( - FunctionName=function_name, Name=ALB_LAMBDA_ALIAS, + FunctionName=function_name, + Name=ALB_LAMBDA_ALIAS, ) alias_exists = True except botocore.exceptions.ClientError as e: # pragma: no cover @@ -1301,7 +1305,8 @@ def update_lambda_function( if concurrency is not None: self.lambda_client.put_function_concurrency( - FunctionName=function_name, ReservedConcurrentExecutions=concurrency, + FunctionName=function_name, + ReservedConcurrentExecutions=concurrency, ) else: self.lambda_client.delete_function_concurrency(FunctionName=function_name) @@ -1389,7 +1394,11 @@ def update_lambda_configuration( if lambda_aws_config["PackageType"] != "Image": kwargs.update( - {"Handler": handler, "Runtime": runtime, "Layers": layers,} + { + "Handler": handler, + "Runtime": runtime, + "Layers": layers, + } ) response = self.lambda_client.update_function_configuration(**kwargs) @@ -1486,7 +1495,7 @@ def is_lambda_function_ready(self, function_name): def wait_until_lambda_function_is_ready(self, function_name): """ - Continuously check if a lambda function is active. + Continuously check if a lambda function is active. For functions deployed with a docker image instead of a ZIP package, the function can take a few seconds longer to be created or update, so we must wait before running any status @@ -1530,7 +1539,9 @@ def delete_lambda_function(self, function_name): """ print("Deleting Lambda function..") - return self.lambda_client.delete_function(FunctionName=function_name,) + return self.lambda_client.delete_function( + FunctionName=function_name, + ) ## # Application load balancer @@ -1652,7 +1663,12 @@ def deploy_lambda_alb(self, lambda_arn, lambda_name, alb_vpc_config, timeout): kwargs = dict( # TODO: Listeners support custom ssl certificates (Certificates). For now we leave this default. Certificates=[{"CertificateArn": alb_vpc_config["CertificateArn"]}], - DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn,}], + DefaultActions=[ + { + "Type": "forward", + "TargetGroupArn": target_group_arn, + } + ], LoadBalancerArn=load_balancer_arn, Protocol="HTTPS", # TODO: Add option for custom ports @@ -2147,7 +2163,12 @@ def remove_api_compression(self, api_id): """ self.apigateway_client.update_rest_api( restApiId=api_id, - patchOperations=[{"op": "replace", "path": "/minimumCompressionSize",}], + patchOperations=[ + { + "op": "replace", + "path": "/minimumCompressionSize", + } + ], ) def get_api_keys(self, api_id, stage_name): @@ -3333,7 +3354,9 @@ def create_async_dynamodb_table(self, table_name, read_capacity, write_capacity) dynamodb_table = self.dynamodb_client.create_table( AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}], TableName=table_name, - KeySchema=[{"AttributeName": "id", "KeyType": "HASH"},], + KeySchema=[ + {"AttributeName": "id", "KeyType": "HASH"}, + ], ProvisionedThroughput={ "ReadCapacityUnits": read_capacity, "WriteCapacityUnits": write_capacity, diff --git a/zappa/letsencrypt.py b/zappa/letsencrypt.py index da22b2d57..9ecd63f02 100755 --- a/zappa/letsencrypt.py +++ b/zappa/letsencrypt.py @@ -41,7 +41,11 @@ def get_cert_and_update_domain( - zappa_instance, lambda_name, api_stage, domain=None, manual=False, + zappa_instance, + lambda_name, + api_stage, + domain=None, + manual=False, ): """ Main cert installer path. @@ -260,7 +264,10 @@ def get_cert(zappa_instance, log=LOGGER, CA=DEFAULT_CA): # get new challenge code, result = _send_signed_request( CA + "/acme/new-authz", - {"resource": "new-authz", "identifier": {"type": "dns", "value": domain},}, + { + "resource": "new-authz", + "identifier": {"type": "dns", "value": domain}, + }, ) if code != 201: raise ValueError( @@ -357,7 +364,11 @@ def sign_certificate(): devnull = open(os.devnull, "wb") csr_der = subprocess.check_output(cmd, stderr=devnull) code, result = _send_signed_request( - DEFAULT_CA + "/acme/new-cert", {"resource": "new-cert", "csr": _b64(csr_der),}, + DEFAULT_CA + "/acme/new-cert", + { + "resource": "new-cert", + "csr": _b64(csr_der), + }, ) if code != 201: raise ValueError("Error signing certificate: {0} {1}".format(code, result)) @@ -370,8 +381,10 @@ def encode_certificate(result): """ Encode cert bytes to PEM encoded cert file. """ - cert_body = """-----BEGIN CERTIFICATE-----\n{0}\n-----END CERTIFICATE-----\n""".format( - "\n".join(textwrap.wrap(base64.b64encode(result).decode("utf8"), 64)) + cert_body = ( + """-----BEGIN CERTIFICATE-----\n{0}\n-----END CERTIFICATE-----\n""".format( + "\n".join(textwrap.wrap(base64.b64encode(result).decode("utf8"), 64)) + ) ) signed_crt = open("{}/signed.crt".format(gettempdir()), "w") signed_crt.write(cert_body)