From 9eca60df310bb8ce27c13573a300eea8413167bd Mon Sep 17 00:00:00 2001 From: Samesh Lakhotia Date: Mon, 9 Mar 2020 19:32:28 +0530 Subject: [PATCH 1/2] 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 | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/hydrus/app.py b/hydrus/app.py index 7a7216e8..9854af74 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 @@ -42,8 +47,8 @@ doc_parse.insert_classes(classes, session) doc_parse.insert_properties(properties, session) -AUTH = True -TOKEN = True +AUTH = False +TOKEN = False if AUTH: try: @@ -54,6 +59,41 @@ # Create a Hydrus app app = app_factory(API_NAME) +# 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") + + with set_authentication(app, AUTH): # Use authentication for all requests with set_token(app, TOKEN): @@ -76,3 +116,4 @@ http_server.serve_forever() except KeyboardInterrupt: pass + From f28910eb78ec28899a102724e8a118a7f7a8344b Mon Sep 17 00:00:00 2001 From: Samesh Lakhotia Date: Mon, 9 Mar 2020 20:32:14 +0530 Subject: [PATCH 2/2] Fix PEP8 compliance Ignored the import error E402 for flake8 to set the PYTHONPATH before all imports are finished. --- hydrus/app.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/hydrus/app.py b/hydrus/app.py index 9854af74..c855dae2 100644 --- a/hydrus/app.py +++ b/hydrus/app.py @@ -6,23 +6,25 @@ # 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) -from hydrus.data import doc_parse -from hydrus.data.db_models import Base -from hydrus.data.exceptions import UserExists -from hydrus.data.user import add_user -from hydra_python_core import doc_maker + 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 from hydrus.utils import ( set_session, set_doc, set_hydrus_server_url, - set_token, set_api_name, set_authentication) + set_token, set_api_name, set_authentication) # noqa: E402 logger = logging.getLogger(__file__) @@ -116,4 +118,3 @@ def do_this_before_get_on_drone_collections(): http_server.serve_forever() except KeyboardInterrupt: pass -