Skip to content

Commit

Permalink
Adds the wrapper around Flask create_app factory function (#29)
Browse files Browse the repository at this point in the history
Adds noqa for unused typing models

Bump the version to support the wrapper
  • Loading branch information
verdan authored and Hans Adriaans committed Jun 30, 2022
1 parent d924936 commit 21f45d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
27 changes: 26 additions & 1 deletion search/search_service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import ast
import importlib
import os
import logging
import sys

from flask import Flask, Blueprint
from flask_restful import Api
from typing import Dict, Any # noqa: F401

from search_service.api.search import SearchAPI, SearchFieldAPI
from search_service.api.healthcheck import healthcheck

# For customized flask use below arguments to override.
FLASK_APP_MODULE_NAME = os.getenv('FLASK_APP_MODULE_NAME')
FLASK_APP_CLASS_NAME = os.getenv('FLASK_APP_CLASS_NAME')
FLASK_APP_KWARGS_DICT_STR = os.getenv('FLASK_APP_KWARGS_DICT')


def create_app(*, config_module_class: str) -> Flask:
"""
Expand All @@ -23,8 +32,24 @@ def create_app(*, config_module_class: str) -> Flask:
:param config_module_class: name of the config
:return: Flask
"""
if FLASK_APP_MODULE_NAME and FLASK_APP_CLASS_NAME:
print(f'Using requested Flask module {FLASK_APP_MODULE_NAME} '
f'and class {FLASK_APP_CLASS_NAME}', file=sys.stderr)
class_obj = getattr(
importlib.import_module(FLASK_APP_MODULE_NAME),
FLASK_APP_CLASS_NAME
)

flask_kwargs_dict = {} # type: Dict[str, Any]
if FLASK_APP_KWARGS_DICT_STR:
print(f'Using kwargs {FLASK_APP_KWARGS_DICT_STR} to instantiate Flask',
file=sys.stderr)
flask_kwargs_dict = ast.literal_eval(FLASK_APP_KWARGS_DICT_STR)

app = class_obj(__name__, **flask_kwargs_dict)

app = Flask(__name__)
else:
app = Flask(__name__)

logging.info('Creating app with config name {}'
.format(config_module_class))
Expand Down
2 changes: 1 addition & 1 deletion search/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

__version__ = '1.0.2'
__version__ = '1.0.3'


setup(
Expand Down

0 comments on commit 21f45d8

Please sign in to comment.