From 57bcf092dca572e50caab1ac98810cc3c44ba2c4 Mon Sep 17 00:00:00 2001 From: Samesh Lakhotia Date: Mon, 9 Mar 2020 19:32:28 +0530 Subject: [PATCH 1/4] FEAT: Add prototype of dynamic routes in hydrus the functions have been defined in app.py and not been imported from any other file is to avoid the circular dependency issues in flask at the prototype stage. I will fix this issue once the prototype is accepted related to #404 --- hydrus/app.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/hydrus/app.py b/hydrus/app.py index 92e328f3..945361d4 100644 --- a/hydrus/app.py +++ b/hydrus/app.py @@ -1,9 +1,14 @@ """Main route for the application""" import logging +import sys +from os.path import dirname, abspath +# insert the ./app.py file path in the PYTHONPATH variable for imports to work +sys.path.insert(0, dirname(dirname(abspath(__file__)))) from sqlalchemy import create_engine - +from flask import request +from collections import defaultdict from gevent.pywsgi import WSGIServer from sqlalchemy.orm import sessionmaker @@ -50,6 +55,42 @@ # Create a Hydrus app app = app_factory(API_NAME) socketio = create_socket(app, session) + +# global dict to store mapping of each function to be run before +# specified route. +# stores in the form {'path1': {'method1': function_before_path1_for_method1}} +before_request_funcs = defaultdict(dict) + + +@app.before_request +def before_request_callback(): + path = request.path + method = request.method + global before_request_funcs + func = before_request_funcs.get(path, {}).get(method, None) + if func: + func() + + +# decorator to define logic for custom before request methods +def custom_before_request(path, method): + def wrapper(f): + global before_request_funcs + before_request_funcs[path][method] = f + return f + return wrapper + + +@custom_before_request('/api/MessageCollection', 'PUT') +def do_this_before_put_on_drone_collections(): + print("Do something before PUT request on MessageCollection") + + +@custom_before_request('/api/MessageCollection', 'GET') +def do_this_before_get_on_drone_collections(): + print("Do something before GET request on MessageCollection") + + # # Nested context managers # @@ -72,3 +113,5 @@ http_server.serve_forever() except KeyboardInterrupt: pass + + From 5cbe4ad4c89fd7e5569b22551c950329fee56ce2 Mon Sep 17 00:00:00 2001 From: Samesh Lakhotia Date: Mon, 9 Mar 2020 20:32:14 +0530 Subject: [PATCH 2/4] Fix PEP8 compliance Ignored the import error E402 for flake8 to set the PYTHONPATH before all imports are finished. --- hydrus/app.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/hydrus/app.py b/hydrus/app.py index 945361d4..7e366e81 100644 --- a/hydrus/app.py +++ b/hydrus/app.py @@ -6,22 +6,24 @@ # insert the ./app.py file path in the PYTHONPATH variable for imports to work sys.path.insert(0, dirname(dirname(abspath(__file__)))) -from sqlalchemy import create_engine -from flask import request -from collections import defaultdict -from gevent.pywsgi import WSGIServer -from sqlalchemy.orm import sessionmaker - -from hydrus.app_factory import app_factory +# ignoring import error by flake 8 temporarily +# for setting PYTHONPATH before imports +from sqlalchemy import create_engine # noqa: E402 +from flask import request # noqa: E402 +from collections import defaultdict # noqa: E402 +from gevent.pywsgi import WSGIServer # noqa: E402 +from sqlalchemy.orm import sessionmaker # noqa: E402 + +from hydrus.app_factory import app_factory # noqa: E402 from hydrus.conf import ( + HYDRUS_SERVER_URL, API_NAME, DB_URL, APIDOC_OBJ, PORT, DEBUG) # noqa: E402 +from hydrus.data import doc_parse # noqa: E402 +from hydrus.data.db_models import Base # noqa: E402 +from hydrus.data.exceptions import UserExists # noqa: E402 +from hydrus.data.user import add_user # noqa: E402 +from hydra_python_core import doc_maker # noqa: E402 HYDRUS_SERVER_URL, API_NAME, DB_URL, APIDOC_OBJ, PORT, DEBUG) -from hydrus.data import doc_parse -from hydrus.data.db_models import Base, create_database_tables -from hydrus.data.exceptions import UserExists -from hydrus.data.user import add_user -from hydra_python_core import doc_maker -from hydrus.utils import ( - set_session, set_doc, set_hydrus_server_url, +from hydrus.utils import (set_session, set_doc, set_hydrus_server_url,set_token, set_api_name, set_authentication) # noqa: E402 set_token, set_api_name, set_authentication) from hydrus.socketio_factory import create_socket @@ -113,5 +115,3 @@ def do_this_before_get_on_drone_collections(): http_server.serve_forever() except KeyboardInterrupt: pass - - From 735712df8d9a89bd796222a1a734409dd87fa8ee Mon Sep 17 00:00:00 2001 From: Purvansh Singh Date: Mon, 29 Mar 2021 18:55:10 +0530 Subject: [PATCH 3/4] PEP fix --- hydrus/app.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/hydrus/app.py b/hydrus/app.py index 7e366e81..0e5d08d6 100644 --- a/hydrus/app.py +++ b/hydrus/app.py @@ -3,6 +3,7 @@ import logging import sys from os.path import dirname, abspath + # insert the ./app.py file path in the PYTHONPATH variable for imports to work sys.path.insert(0, dirname(dirname(abspath(__file__)))) @@ -16,20 +17,20 @@ from hydrus.app_factory import app_factory # noqa: E402 from hydrus.conf import ( - HYDRUS_SERVER_URL, API_NAME, DB_URL, APIDOC_OBJ, PORT, DEBUG) # noqa: E402 + HYDRUS_SERVER_URL, API_NAME, + DB_URL, APIDOC_OBJ, PORT) # noqa: E402 from hydrus.data import doc_parse # noqa: E402 from hydrus.data.db_models import Base # noqa: E402 from hydrus.data.exceptions import UserExists # noqa: E402 from hydrus.data.user import add_user # noqa: E402 from hydra_python_core import doc_maker # noqa: E402 - HYDRUS_SERVER_URL, API_NAME, DB_URL, APIDOC_OBJ, PORT, DEBUG) -from hydrus.utils import (set_session, set_doc, set_hydrus_server_url,set_token, set_api_name, set_authentication) # noqa: E402 - set_token, set_api_name, set_authentication) +from hydrus.utils import ( + set_session, set_doc, set_hydrus_server_url, + set_token, set_api_name, set_authentication) # noqa: E402 from hydrus.socketio_factory import create_socket logger = logging.getLogger(__file__) -# TODO: loading the engine and creating the tables should be handled better engine = create_engine(DB_URL) session = sessionmaker(bind=engine)() @@ -40,7 +41,6 @@ classes = doc_parse.get_classes(apidoc) try: Base.metadata.drop_all(engine) - create_database_tables(classes) Base.metadata.create_all(engine) except Exception: pass @@ -80,6 +80,7 @@ def wrapper(f): global before_request_funcs before_request_funcs[path][method] = f return f + return wrapper @@ -101,17 +102,17 @@ def do_this_before_get_on_drone_collections(): # Set HYDRUS_SERVER_URL # Set the Database session with set_authentication(app, AUTH), set_token(app, TOKEN), \ - set_api_name(app, API_NAME), set_doc(app, apidoc), \ - set_hydrus_server_url(app, HYDRUS_SERVER_URL), set_session(app, session): - if __name__ == "__main__": - # this is run only if development server is run - # Set the name of the API - socketio.run(app=app, debug=True, port=PORT) - else: - # Start the Hydrus app - http_server = WSGIServer(('', PORT), app) - logger.info(f'Running server at port {PORT}') - try: - http_server.serve_forever() - except KeyboardInterrupt: - pass + set_api_name(app, API_NAME), set_doc(app, apidoc), \ + set_hydrus_server_url(app, HYDRUS_SERVER_URL), set_session(app, session): + if __name__ == "__main__": + # this is run only if development server is run + # Set the name of the API + socketio.run(app=app, debug=True, port=PORT) + else: + # Start the Hydrus app + http_server = WSGIServer(('', PORT), app) + logger.info(f'Running server at port {PORT}') + try: + http_server.serve_forever() + except KeyboardInterrupt: + pass From bbf8b6d287d134968166be62248ba22d49a054a9 Mon Sep 17 00:00:00 2001 From: Purvansh Singh Date: Mon, 29 Mar 2021 19:13:36 +0530 Subject: [PATCH 4/4] PEP fix 2 --- hydrus/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hydrus/app.py b/hydrus/app.py index 0e5d08d6..dc64b2de 100644 --- a/hydrus/app.py +++ b/hydrus/app.py @@ -2,6 +2,7 @@ import logging import sys +from hydrus.socketio_factory import create_socket from os.path import dirname, abspath # insert the ./app.py file path in the PYTHONPATH variable for imports to work @@ -27,7 +28,6 @@ from hydrus.utils import ( set_session, set_doc, set_hydrus_server_url, set_token, set_api_name, set_authentication) # noqa: E402 -from hydrus.socketio_factory import create_socket logger = logging.getLogger(__file__)