Skip to content

Commit 09a84f7

Browse files
committed
Fixed lint errors
1 parent e475c5a commit 09a84f7

File tree

9 files changed

+437
-308
lines changed

9 files changed

+437
-308
lines changed

dot-env-example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ PORT=8080
22
CLOUDANT_HOST=localhost
33
CLOUDANT_PORT=5984
44
CLOUDANT_USERNAME=admin
5-
CLOUDANT_PASSWORD=pass
6-
5+
CLOUDANT_PASSWORD=pass

service/__init__.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,47 +36,52 @@
3636

3737
app.url_map.strict_slashes = False
3838

39-
app.config['SECRET_KEY'] = 'secret-for-dev'
40-
app.config['LOGGING_LEVEL'] = logging.INFO
41-
app.config['API_KEY'] = os.getenv('API_KEY')
42-
app.config['ERROR_404_HELP'] = False
39+
app.config["SECRET_KEY"] = "secret-for-dev"
40+
app.config["LOGGING_LEVEL"] = logging.INFO
41+
app.config["API_KEY"] = os.getenv("API_KEY")
42+
app.config["ERROR_404_HELP"] = False
4343

4444
# Document the type of authorization required
45-
authorizations = {
46-
'apikey': {
47-
'type': 'apiKey',
48-
'in': 'header',
49-
'name': 'X-Api-Key'
50-
}
51-
}
45+
authorizations = {"apikey": {"type": "apiKey", "in": "header", "name": "X-Api-Key"}}
5246

5347
######################################################################
5448
# Configure Swagger before initializing it
5549
######################################################################
56-
api = Api(app,
57-
version='1.0.0',
58-
title='Pet Demo REST API Service',
59-
description='This is a sample server Pet store server.',
60-
default='pets',
61-
default_label='Pet shop operations',
62-
doc='/apidocs', # default also could use doc='/apidocs/'
63-
authorizations=authorizations,
64-
prefix='/api'
65-
)
50+
api = Api(
51+
app,
52+
version="1.0.0",
53+
title="Pet Demo REST API Service",
54+
description="This is a sample server Pet store server.",
55+
default="pets",
56+
default_label="Pet shop operations",
57+
doc="/apidocs", # default also could use doc='/apidocs/'
58+
authorizations=authorizations,
59+
prefix="/api",
60+
)
61+
6662

6763
# Import the routes After the Flask app is created
68-
from service import routes, models
64+
# pylint: disable=wrong-import-position, wrong-import-order, cyclic-import
65+
from service import routes, models # noqa: F401, E402
66+
from service.common import error_handlers
6967

7068
# Set up logging for production
7169
log_handlers.init_logging(app, "gunicorn.error")
7270

73-
app.logger.info(70 * '*')
74-
app.logger.info(' P E T S E R V I C E R U N N I N G '.center(70, '*'))
75-
app.logger.info(70 * '*')
71+
app.logger.info(70 * "*")
72+
app.logger.info(" P E T S E R V I C E R U N N I N G ".center(70, "*"))
73+
app.logger.info(70 * "*")
7674

77-
app.logger.info('Service initialized!')
75+
app.logger.info("Service initialized!")
7876

7977
# If an API Key was not provided, autogenerate one
80-
if not app.config['API_KEY']:
81-
app.config['API_KEY'] = routes.generate_apikey()
82-
app.logger.info('Missing API Key! Autogenerated: {}'.format(app.config['API_KEY']))
78+
if not app.config["API_KEY"]:
79+
app.config["API_KEY"] = routes.generate_apikey()
80+
app.logger.info("Missing API Key! Autogenerated: %s", app.config["API_KEY"])
81+
82+
try:
83+
routes.init_db()
84+
except Exception as error: # pylint: disable=broad-except
85+
app.logger.critical("%s: Cannot continue", error)
86+
# gunicorn requires exit code 4 to stop spawning workers when they die
87+
sys.exit(4)

service/common/error_handlers.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,29 @@
2424
from service.models import DataValidationError, DatabaseConnectionError
2525
from . import status
2626

27+
2728
######################################################################
2829
# Special Error Handlers
2930
######################################################################
3031
@api.errorhandler(DataValidationError)
3132
def request_validation_error(error):
32-
""" Handles Value Errors from bad data """
33+
"""Handles Value Errors from bad data"""
3334
message = str(error)
3435
app.logger.error(message)
3536
return {
36-
'status_code': status.HTTP_400_BAD_REQUEST,
37-
'error': 'Bad Request',
38-
'message': message
37+
"status_code": status.HTTP_400_BAD_REQUEST,
38+
"error": "Bad Request",
39+
"message": message,
3940
}, status.HTTP_400_BAD_REQUEST
4041

42+
4143
@api.errorhandler(DatabaseConnectionError)
4244
def database_connection_error(error):
43-
""" Handles Database Errors from connection attempts """
45+
"""Handles Database Errors from connection attempts"""
4446
message = str(error)
4547
app.logger.critical(message)
4648
return {
47-
'status_code': status.HTTP_503_SERVICE_UNAVAILABLE,
48-
'error': 'Service Unavailable',
49-
'message': message
50-
}, status.HTTP_503_SERVICE_UNAVAILABLE
49+
"status_code": status.HTTP_503_SERVICE_UNAVAILABLE,
50+
"error": "Service Unavailable",
51+
"message": message,
52+
}, status.HTTP_503_SERVICE_UNAVAILABLE

service/common/log_handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def init_logging(app, logger_name: str):
3030
app.logger.handlers = gunicorn_logger.handlers
3131
app.logger.setLevel(gunicorn_logger.level)
3232
# Make all log formats consistent
33-
formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] [%(module)s] %(message)s", "%Y-%m-%d %H:%M:%S %z")
33+
formatter = logging.Formatter(
34+
"[%(asctime)s] [%(levelname)s] [%(module)s] %(message)s", "%Y-%m-%d %H:%M:%S %z"
35+
)
3436
for handler in app.logger.handlers:
3537
handler.setFormatter(formatter)
3638
app.logger.info("Logging handler established")

0 commit comments

Comments
 (0)