diff --git a/.travis.yml b/.travis.yml index 9db2c769..c4cc57ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ install: - python setup.py install - pip install flake8 - pip install pytest -before_script: flake8 sanic +before_script: flake8 --max-line-length=120 sanic script: py.test -v tests deploy: provider: pypi diff --git a/sanic/__init__.py b/sanic/__init__.py index 5b893df4..b7be9aaf 100644 --- a/sanic/__init__.py +++ b/sanic/__init__.py @@ -1,2 +1,4 @@ from .sanic import Sanic from .blueprints import Blueprint + +__all__ = ['Sanic', 'Blueprint'] diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 506c2e90..b3b40df9 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -53,7 +53,6 @@ class Blueprint: """ self.deferred_functions.append(func) - def make_setup_state(self, app, options): """ """ @@ -96,4 +95,3 @@ class Blueprint: self.record(lambda s: s.add_exception(handler, *args, **kwargs)) return handler return decorator - diff --git a/sanic/request.py b/sanic/request.py index ece1ae8f..85dcad46 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -80,7 +80,7 @@ class Request: @property def files(self): if self.parsed_files is None: - _ = self.form # compute form to get files + self.form # compute form to get files return self.parsed_files diff --git a/sanic/response.py b/sanic/response.py index f26254f5..3ca5a9ad 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -19,16 +19,16 @@ STATUS_CODES = { class HTTPResponse: __slots__ = ('body', 'status', 'content_type', 'headers') - def __init__(self, body=None, status=200, headers=[], content_type='text/plain', body_bytes=b''): + def __init__(self, body=None, status=200, headers=None, content_type='text/plain', body_bytes=b''): self.content_type = content_type - if not body is None: + if body is not None: self.body = body.encode('utf-8') else: self.body = body_bytes self.status = status - self.headers = headers + self.headers = headers or {} def output(self, version="1.1", keep_alive=False, keep_alive_timeout=None): # This is all returned in a kind-of funky way @@ -39,7 +39,10 @@ class HTTPResponse: headers = b'' if self.headers: - headers = b''.join(b'%b: %b\r\n' % (name.encode(), value.encode('utf-8')) for name, value in self.headers.items()) + headers = b''.join( + b'%b: %b\r\n' % (name.encode(), value.encode('utf-8')) + for name, value in self.headers.items() + ) return b'HTTP/%b %d %b\r\nContent-Type: %b\r\nContent-Length: %d\r\nConnection: %b\r\n%b%b\r\n%b' % ( version.encode(), self.status, @@ -52,6 +55,7 @@ class HTTPResponse: self.body ) + def json(body, status=200, headers=None): return HTTPResponse(ujson.dumps(body), headers=headers, status=status, content_type="application/json; charset=utf-8") diff --git a/sanic/router.py b/sanic/router.py index ddab5c61..cbae0292 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -94,7 +94,7 @@ class Router: break if route: - if route.methods and not request.method in route.methods: + if route.methods and request.method not in route.methods: raise InvalidUsage("Method {} not allowed for URL {}".format(request.method, request.url), status_code=405) return route.handler, args, kwargs @@ -120,7 +120,7 @@ class SimpleRouter: def get(self, request): route = self.routes.get(request.url) if route: - if route.methods and not request.method in route.methods: + if route.methods and request.method not in route.methods: raise InvalidUsage("Method {} not allowed for URL {}".format(request.method, request.url), status_code=405) return route.handler, [], {} diff --git a/sanic/sanic.py b/sanic/sanic.py index 50a5a803..154aac15 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -63,7 +63,6 @@ class Sanic: Decorates and registers middleware to be called before a request can either be called as @app.middleware or @app.middleware('request') """ - middleware = None attach_to = 'request' def register_middleware(middleware): @@ -80,11 +79,6 @@ class Sanic: attach_to = args[0] return register_middleware - if isinstance(middleware, FunctionType): - middleware = Middleware(process_request=middleware) - - return middleware - def register_blueprint(self, blueprint, **options): """ Registers a blueprint on the application.