diff --git a/.gitignore b/.gitignore index a6234ef..c94b19d 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,11 @@ env/** texts/** .idea/** # }}} + +# Local development installs {{{ +*.egg-info/ +# }}} + +# Text editor cruft {{{ +*.sw[po] +# }}} diff --git a/blockspring/__init__.py b/blockspring/__init__.py index 301960c..424f87f 100644 --- a/blockspring/__init__.py +++ b/blockspring/__init__.py @@ -6,6 +6,8 @@ import base64 import re import tempfile +import inspect +import functools try: from urlparse import urlparse @@ -206,6 +208,21 @@ def processArgs(request): response = Response() block(request, response) +def decorate(func): + """ Decorate any function into one readily parsed by blockspring. + The arguments to the function will be looked for by name in request.params + TODO: respect default values in wrapped function. + """ + @functools.wraps(func) + def block(request, response): + arguments = inspect.getargspec(func).args #get a list of arguments accepted by wrapped + parameters = tuple(request.params[arg] for arg in arguments) + output = func(*parameters) #unpack them and call them into func + response.addOutput('output', output) + response.end() + + return block + class Request: def __init__(self): self.params = {} diff --git a/blockspring/test_decorators.py b/blockspring/test_decorators.py new file mode 100644 index 0000000..3d50465 --- /dev/null +++ b/blockspring/test_decorators.py @@ -0,0 +1,62 @@ +import pytest +import blockspring + +# ***** Fixtures ****** # + +def decorator_fixture(a,b=0): + """This will be the test function for our decorator""" + return a+b + +def dict_outputs(a, b): + """returns a dict""" + output = {} + return {'a_squared':a*a, 'b_squared':b*b} + +def list_outputs(a, b): + """returns a list""" + return [a*a, b*b] + +@pytest.fixture +def request(): + request = blockspring.parse({'a':1, 'b':3}) + return request + +@pytest.fixture +def response(): + response = blockspring.Response() + return response + +@pytest.fixture +def wrapped(): + wrapped = blockspring.decorate(decorator_fixture) + return wrapped + +@pytest.fixture +def dict_output_wrapped(): + wrapped = blockspring.decorate(dict_outputs) + return wrapped + +@pytest.fixture +def list_output_wrapped(): + wrapped = blockspring.decorate(list_outputs) + return wrapped + +########## Tests ######### + +def test_decorator_respects_name(wrapped): + assert wrapped.__name__ == 'decorator_fixture' + +def test_creates_blockspring_function(wrapped,request, response): + assert response.result['_blockspring_spec']==True + +def test_adds_output(wrapped, request, response): + test = wrapped(request, response) + assert response.result['output']==4 + +def test_wraps_functions_that_returns_dicts(dict_output_wrapped, request, response): + test = dict_output_wrapped(request, response) + assert response.result['output']['b_squared']==9 + +def test_wraps_functions_that_return_lists(list_output_wrapped, request, response): + test = list_output_wrapped(request, response) + assert response.result['output'][1]==9