Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Adds support for passing in a name and a Flask application to the ini… #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions firefly/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use name=None and set it to "firefly" if not specified. That way you don't have to change any of the existing code.

"""Creates a firefly application.

If the optional parameter auth_token is specified, the
Expand All @@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally it should be,
self.flask_app = flask_app if flask_app else Flask(name) as this is more readable.

The current statement also evaluates to the same result, however intent is not clear, and one has to know that Python evaluates or lazily.


def set_auth_token(self, token):
self.auth_token = token
Expand Down
2 changes: 1 addition & 1 deletion firefly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ def main():

setup_logger()
logger.info("Starting Firefly...")
app = Firefly()
app = Firefly(__name__)
load_from_env()
14 changes: 7 additions & 7 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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": {
Expand All @@ -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}')
Expand All @@ -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}')
Expand All @@ -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}')
Expand All @@ -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='{}')
Expand All @@ -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='{}')
Expand Down