From db9f72c64cb36b00b2370457439f3292807b098e Mon Sep 17 00:00:00 2001 From: Nabarun Pal Date: Fri, 12 Oct 2018 12:57:18 +0530 Subject: [PATCH 1/3] Adds support for passing in a name and a Flask application to the init of Firefly --- firefly/app.py | 6 ++++-- firefly/main.py | 2 +- tests/test_app.py | 14 +++++++------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/firefly/app.py b/firefly/app.py index 128f5c8..9a34e1e 100644 --- a/firefly/app.py +++ b/firefly/app.py @@ -9,6 +9,7 @@ from .version import __version__ import threading from wsgiref.simple_server import make_server +from flask import Flask try: from inspect import signature, _empty @@ -25,7 +26,7 @@ ctx.request = None class Firefly(object): - def __init__(self, auth_token=None, allowed_origins=""): + def __init__(self, name, auth_token=None, allowed_origins="", flask_app=None): """Creates a firefly application. If the optional parameter auth_token is specified, the @@ -39,11 +40,12 @@ def __init__(self, auth_token=None, allowed_origins=""): :param auth_token: the auto_token for the application :param allowed_origins: allowed origins for cross-origin requests """ + self.name = name self.mapping = {} self.add_route('/', self.generate_index,internal=True) self.auth_token = auth_token self.allowed_origins = allowed_origins - + self.flask_app = flask_app or Flask(name) def set_auth_token(self, token): self.auth_token = token diff --git a/firefly/main.py b/firefly/main.py index 7cb6da7..da69e2e 100644 --- a/firefly/main.py +++ b/firefly/main.py @@ -130,5 +130,5 @@ def main(): setup_logger() logger.info("Starting Firefly...") -app = Firefly() +app = Firefly(__name__) load_from_env() diff --git a/tests/test_app.py b/tests/test_app.py index 8be637e..aa02f71 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -16,7 +16,7 @@ def dummy(): class TestFirefly: def test_generate_function_list(self): - firefly = Firefly() + firefly = Firefly(__name__) assert firefly.generate_function_list() == {} firefly.add_route("/square", square, "square") @@ -35,7 +35,7 @@ def test_generate_function_list(self): assert firefly.generate_function_list() == returned_dict def test_generate_function_list_for_func_name(self): - firefly = Firefly() + firefly = Firefly(__name__) firefly.add_route("/sq2", square, "sq") returned_dict = { "sq": { @@ -52,7 +52,7 @@ def test_generate_function_list_for_func_name(self): assert firefly.generate_function_list() == returned_dict def test_function_call(self): - app = Firefly() + app = Firefly(__name__) app.add_route("/", square) request = Request.blank("/", POST='{"a": 3}') @@ -61,7 +61,7 @@ def test_function_call(self): assert response.text == '9' def test_auth_failure(self): - app = Firefly(auth_token='abcd') + app = Firefly(__name__, auth_token='abcd') app.add_route("/", square) request = Request.blank("/", POST='{"a": 3}') @@ -77,7 +77,7 @@ def test_auth_failure(self): assert response.status == '403 Forbidden' def test_http_error_404(self): - app = Firefly() + app = Firefly(__name__) app.add_route("/", square) request = Request.blank("/sq", POST='{"a": 3}') @@ -89,7 +89,7 @@ def peek_ctx(): keys = sorted(ctx.__dict__.keys()) return list(keys) - app = Firefly() + app = Firefly(__name__) app.add_route("/", peek_ctx) request = Request.blank("/", POST='{}') @@ -103,7 +103,7 @@ def peek_ctx(): ctx.count = getattr(ctx, "count", 0) + 1 return ctx.count - app = Firefly() + app = Firefly(__name__) app.add_route("/", peek_ctx) request = Request.blank("/", POST='{}') From b4969b2c156506e153ab52f184d6b2c173fe45fb Mon Sep 17 00:00:00 2001 From: Nabarun Pal Date: Fri, 12 Oct 2018 14:16:26 +0530 Subject: [PATCH 2/3] Adds flask to requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index e3f9fe7..f4f2c40 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ WebOb>=1.7.2 requests>=2.18.1 PyYAML>=3.12 funcsigs>=1.0.2 ; python_version < '3' +Flask==1.0.2 From f37f484f4297172057aede5b040e5eb13fb0db1c Mon Sep 17 00:00:00 2001 From: Nabarun Pal Date: Fri, 12 Oct 2018 14:18:58 +0530 Subject: [PATCH 3/3] Makes pasing in the name to Firefly constructor optional --- firefly/app.py | 2 +- firefly/main.py | 2 +- tests/test_app.py | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/firefly/app.py b/firefly/app.py index 9a34e1e..89d2de5 100644 --- a/firefly/app.py +++ b/firefly/app.py @@ -26,7 +26,7 @@ ctx.request = None class Firefly(object): - def __init__(self, name, auth_token=None, allowed_origins="", flask_app=None): + def __init__(self, name="firefly", auth_token=None, allowed_origins="", flask_app=None): """Creates a firefly application. If the optional parameter auth_token is specified, the diff --git a/firefly/main.py b/firefly/main.py index da69e2e..7cb6da7 100644 --- a/firefly/main.py +++ b/firefly/main.py @@ -130,5 +130,5 @@ def main(): setup_logger() logger.info("Starting Firefly...") -app = Firefly(__name__) +app = Firefly() load_from_env() diff --git a/tests/test_app.py b/tests/test_app.py index aa02f71..8be637e 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -16,7 +16,7 @@ def dummy(): class TestFirefly: def test_generate_function_list(self): - firefly = Firefly(__name__) + firefly = Firefly() assert firefly.generate_function_list() == {} firefly.add_route("/square", square, "square") @@ -35,7 +35,7 @@ def test_generate_function_list(self): assert firefly.generate_function_list() == returned_dict def test_generate_function_list_for_func_name(self): - firefly = Firefly(__name__) + firefly = Firefly() firefly.add_route("/sq2", square, "sq") returned_dict = { "sq": { @@ -52,7 +52,7 @@ def test_generate_function_list_for_func_name(self): assert firefly.generate_function_list() == returned_dict def test_function_call(self): - app = Firefly(__name__) + app = Firefly() app.add_route("/", square) request = Request.blank("/", POST='{"a": 3}') @@ -61,7 +61,7 @@ def test_function_call(self): assert response.text == '9' def test_auth_failure(self): - app = Firefly(__name__, auth_token='abcd') + app = Firefly(auth_token='abcd') app.add_route("/", square) request = Request.blank("/", POST='{"a": 3}') @@ -77,7 +77,7 @@ def test_auth_failure(self): assert response.status == '403 Forbidden' def test_http_error_404(self): - app = Firefly(__name__) + app = Firefly() app.add_route("/", square) request = Request.blank("/sq", POST='{"a": 3}') @@ -89,7 +89,7 @@ def peek_ctx(): keys = sorted(ctx.__dict__.keys()) return list(keys) - app = Firefly(__name__) + app = Firefly() app.add_route("/", peek_ctx) request = Request.blank("/", POST='{}') @@ -103,7 +103,7 @@ def peek_ctx(): ctx.count = getattr(ctx, "count", 0) + 1 return ctx.count - app = Firefly(__name__) + app = Firefly() app.add_route("/", peek_ctx) request = Request.blank("/", POST='{}')