From 74ae0007d3a7bfea562e8b4a29880184815f9155 Mon Sep 17 00:00:00 2001 From: Channel Cat Date: Tue, 25 Oct 2016 02:45:28 -0700 Subject: [PATCH] Reverse static arguments --- CHANGELOG.md | 5 ++++- docs/static_files.md | 4 ++-- sanic/__init__.py | 2 +- sanic/blueprints.py | 8 ++++---- sanic/sanic.py | 4 ++-- sanic/static.py | 2 +- tests/test_blueprints.py | 2 +- tests/test_static.py | 4 ++-- 8 files changed, 17 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6123d44..84e7be78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ Version 0.1 ----------- - - 0.1.6 (not released) + - 0.1.7 + - Reversed static url and directory arguments to meet spec + - 0.1.6 - Static files + - Lazy Cookie Loading - 0.1.5 - Cookies - Blueprint listeners and ordering diff --git a/docs/static_files.md b/docs/static_files.md index 284d747d..fca8d251 100644 --- a/docs/static_files.md +++ b/docs/static_files.md @@ -8,11 +8,11 @@ Both directories and files can be served by registering with static app = Sanic(__name__) # Serves files from the static folder to the URL /static -app.static('./static', '/static') +app.static('/static', './static') # Serves the file /home/ubuntu/test.png when the URL /the_best.png # is requested -app.static('/home/ubuntu/test.png', '/the_best.png') +app.static('/the_best.png', '/home/ubuntu/test.png') app.run(host="0.0.0.0", port=8000) ``` diff --git a/sanic/__init__.py b/sanic/__init__.py index 618368d4..d8a9e56e 100644 --- a/sanic/__init__.py +++ b/sanic/__init__.py @@ -1,6 +1,6 @@ from .sanic import Sanic from .blueprints import Blueprint -__version__ = '0.1.6' +__version__ = '0.1.7' __all__ = ['Sanic', 'Blueprint'] diff --git a/sanic/blueprints.py b/sanic/blueprints.py index d619b574..c9c54b62 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -33,14 +33,14 @@ class BlueprintSetup: """ self.app.exception(*args, **kwargs)(handler) - def add_static(self, file_or_directory, uri, *args, **kwargs): + def add_static(self, uri, file_or_directory, *args, **kwargs): """ Registers static files to sanic """ if self.url_prefix: uri = self.url_prefix + uri - self.app.static(file_or_directory, uri, *args, **kwargs) + self.app.static(uri, file_or_directory, *args, **kwargs) def add_middleware(self, middleware, *args, **kwargs): """ @@ -122,8 +122,8 @@ class Blueprint: return handler return decorator - def static(self, file_or_directory, uri, *args, **kwargs): + def static(self, uri, file_or_directory, *args, **kwargs): """ """ self.record( - lambda s: s.add_static(file_or_directory, uri, *args, **kwargs)) + lambda s: s.add_static(uri, file_or_directory, *args, **kwargs)) diff --git a/sanic/sanic.py b/sanic/sanic.py index 73f20dfb..edb3a973 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -95,13 +95,13 @@ class Sanic: return register_middleware # Static Files - def static(self, file_or_directory, uri, pattern='.+', + def static(self, uri, file_or_directory, pattern='.+', use_modified_since=True): """ Registers a root to serve files from. The input can either be a file or a directory. See """ - static_register(self, file_or_directory, uri, pattern, + static_register(self, uri, file_or_directory, pattern, use_modified_since) def blueprint(self, blueprint, **options): diff --git a/sanic/static.py b/sanic/static.py index 7c3a529f..72361a9a 100644 --- a/sanic/static.py +++ b/sanic/static.py @@ -7,7 +7,7 @@ from .exceptions import FileNotFound, InvalidUsage from .response import file, HTTPResponse -def register(app, file_or_directory, uri, pattern, use_modified_since): +def register(app, uri, file_or_directory, pattern, use_modified_since): # TODO: Though sanic is not a file server, I feel like we should atleast # make a good effort here. Modified-since is nice, but we could # also look into etags, expires, and caching diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 39303ff4..f7b9b8ef 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -156,7 +156,7 @@ def test_bp_static(): app = Sanic('test_static') blueprint = Blueprint('test_static') - blueprint.static(current_file, '/testing.file') + blueprint.static('/testing.file', current_file) app.blueprint(blueprint) diff --git a/tests/test_static.py b/tests/test_static.py index 314a0927..6dafac2b 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -10,7 +10,7 @@ def test_static_file(): current_file_contents = file.read() app = Sanic('test_static') - app.static(current_file, '/testing.file') + app.static('/testing.file', current_file) request, response = sanic_endpoint_test(app, uri='/testing.file') assert response.status == 200 @@ -23,7 +23,7 @@ def test_static_directory(): current_file_contents = file.read() app = Sanic('test_static') - app.static(current_directory, '/dir') + app.static('/dir', current_directory) request, response = sanic_endpoint_test(app, uri='/dir/test_static.py') assert response.status == 200