Skip to content

Commit

Permalink
Reformatted code per updates in black style.
Browse files Browse the repository at this point in the history
  • Loading branch information
akornatskyy committed Aug 29, 2020
1 parent 677b315 commit 45f8157
Show file tree
Hide file tree
Showing 37 changed files with 597 additions and 856 deletions.
6 changes: 2 additions & 4 deletions demos/ajax/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ def test_stream(self):
assert "John" in self.client.json.message

def test_method_not_allowed(self):
""" Ensure method not allowed status code.
"""
"""Ensure method not allowed status code."""
assert 405 == self.client.get("/")

def test_not_found(self):
""" Ensure not found status code.
"""
"""Ensure not found status code."""
assert 404 == self.client.get("/x")


Expand Down
12 changes: 4 additions & 8 deletions demos/guestbook/test_guestbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@


class MainFunctionalTestCase(unittest.TestCase):
""" Functional tests for ``guestbook`` application.
"""
"""Functional tests for ``guestbook`` application."""

def setUp(self):
from guestbook import main
Expand All @@ -20,19 +19,16 @@ def tearDown(self):
self.client = None

def test_welcome(self):
""" Ensure welcome page is rendered.
"""
"""Ensure welcome page is rendered."""
assert 200 == self.client.get("/")
assert "author" in self.client.content

def test_favicon(self):
""" Resource not found.
"""
"""Resource not found."""
assert 404 == self.client.get("/favicon.ico")

def test_add(self):
""" Add page redirects to welcome.
"""
"""Add page redirects to welcome."""
from guestbook import greetings

assert 200 == self.client.get("/")
Expand Down
6 changes: 2 additions & 4 deletions demos/hello/benchmark_helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@


class BenchmarkTestCase(HelloWorldTestCase):
"""
"""
""""""

def runTest(self): # noqa: N802
""" Perform bachmark and print results.
"""
"""Perform bachmark and print results."""
p = Benchmark((self.test_welcome, self.test_not_found), 20000)
p.report(
"hello", baselines={"test_welcome": 1.0, "test_not_found": 1.3}
Expand Down
6 changes: 2 additions & 4 deletions demos/hello/test_helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ def tearDown(self):
self.client = None

def test_welcome(self):
""" Ensure welcome page is rendered.
"""
"""Ensure welcome page is rendered."""
assert 200 == self.client.get("/")
assert "Hello" in self.client.content

def test_not_found(self):
""" Ensure not found status code.
"""
"""Ensure not found status code."""
assert 404 == self.client.get("/x")
3 changes: 1 addition & 2 deletions demos/streaming/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def __init__(
]

def __call__(self, start_response):
""" WSGI call processing.
"""
"""WSGI call processing."""
start_response("200 OK", self.headers)
return (chunk.encode(self.encoding) for chunk in self.iterable)

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
README = open(os.path.join(os.path.dirname(__file__), "README.md")).read()
VERSION = (
re.search(
r'__version__ = "(.+)"', open("src/wheezy/http/__init__.py").read(),
r'__version__ = "(.+)"',
open("src/wheezy/http/__init__.py").read(),
)
.group(1)
.strip()
Expand Down
20 changes: 20 additions & 0 deletions src/wheezy/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,25 @@
"HTTPCookie",
"accept_method",
"HTTPRequest",
"HTTPResponse",
"ajax_redirect",
"bad_request",
"error400",
"error401",
"error403",
"error404",
"error405",
"error500",
"forbidden",
"http_error",
"internal_error",
"json_response",
"method_not_allowed",
"not_found",
"permanent_redirect",
"redirect",
"see_other",
"temporary_redirect",
"unauthorized",
)
__version__ = "0.1"
55 changes: 27 additions & 28 deletions src/wheezy/http/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@


def wrap_middleware(following, func):
""" Helper function to wrap middleware, adapts middleware
contract to::
"""Helper function to wrap middleware, adapts middleware
contract to::
def handler(request):
return response
def handler(request):
return response
``following`` - next middleware in the chain.
``func`` - middleware callable.
``following`` - next middleware in the chain.
``func`` - middleware callable.
"""
return lambda request: func(request, following)


class WSGIApplication(object):
""" The application object is simply a WSGI callable object.
"""The application object is simply a WSGI callable object.
``middleware`` is any callable of the following
contract::
``middleware`` is any callable of the following
contract::
def middleware(request, following):
if following:
response = following(request)
else:
response
return response
def middleware(request, following):
if following:
response = following(request)
else:
response
return response
``middleware_factory`` is a factory that initialize
middleware::
``middleware_factory`` is a factory that initialize
middleware::
def middleware_factory(options):
return middleware
def middleware_factory(options):
return middleware
``middleware_factory`` can return None, this can be useful
for some sort of initialization that needs to be run during
application bootstrap.
``middleware_factory`` can return None, this can be useful
for some sort of initialization that needs to be run during
application bootstrap.
"""

def __init__(self, middleware, options):
""" Initializes WSGI application.
"""Initializes WSGI application.
``middleware`` - a list of middleware to be used by this
application.
``middleware`` - a list of middleware to be used by this
application.
``options`` - a dict of configuration options.
``options`` - a dict of configuration options.
"""
middleware = [
m for m in (m(options) for m in middleware) if m is not None
Expand All @@ -61,8 +61,7 @@ def __init__(self, middleware, options):
self.encoding = options["ENCODING"]

def __call__(self, environ, start_response):
""" WSGI application entry point.
"""
"""WSGI application entry point."""
request = HTTPRequest(environ, self.encoding, self.options)
response = self.middleware(request)
if response is None:
Expand Down
26 changes: 13 additions & 13 deletions src/wheezy/http/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@


def secure(wrapped=None, enabled=True):
""" Checks if user is accessing protected resource via SSL and if
not, issue permanent redirect to HTTPS location.
"""Checks if user is accessing protected resource via SSL and if
not, issue permanent redirect to HTTPS location.
``enabled`` - whenever to do any checks (defaults to ``True``).
``enabled`` - whenever to do any checks (defaults to ``True``).
Example::
Example::
@secure
def my_view(request):
...
return response
@secure
def my_view(request):
...
return response
Using ``enabled``::
Using ``enabled``::
@secure(enabled=False)
def my_view(request):
...
return response
@secure(enabled=False)
def my_view(request):
...
return response
"""

def decorate(method):
Expand Down
40 changes: 14 additions & 26 deletions src/wheezy/http/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


def response_cache(profile=None):
""" Decorator that applies cache profile strategy to the
wrapping handler.
"""Decorator that applies cache profile strategy to the
wrapping handler.
"""
if not profile:
profile = none_cache_profile
Expand Down Expand Up @@ -56,8 +56,7 @@ def no_cache(request, *args, **kwargs):


def wsgi_cache(profile):
""" Decorator that wraps wsgi app and set cache profile.
"""
"""Decorator that wraps wsgi app and set cache profile."""

def decorate(wsgi_app):
if not profile.enabled:
Expand All @@ -73,8 +72,7 @@ def wsgi_wrapper(environ, start_response):


def make_etag(hasher):
""" Build etag function based on `hasher` algorithm.
"""
"""Build etag function based on `hasher` algorithm."""

def etag(buf):
h = hasher()
Expand All @@ -89,8 +87,7 @@ def etag(buf):


def make_etag_crc32(hasher):
""" Build etag function based on `hasher` algorithm and crc32.
"""
"""Build etag function based on `hasher` algorithm and crc32."""

def etag(buf):
h = hasher()
Expand All @@ -105,55 +102,47 @@ def etag(buf):


class SurfaceResponse(object):
""" WSGI wrapper that returns ``response`` headers and buffer.
"""
"""WSGI wrapper that returns ``response`` headers and buffer."""

__slots__ = ("inner",)

def __init__(self, response):
""" Initializes response.
"""
"""Initializes response."""
self.inner = response

def __call__(self, start_response):
""" WSGI call processing.
"""
"""WSGI call processing."""
start_response("200 OK", self.inner.headers)
return self.inner.buffer


class NotModifiedResponse(object):
""" Not modified cachable response.
"""
"""Not modified cachable response."""

status_code = 304
__slots__ = ("headers",)

def __init__(self, response):
""" Initializes not modified cachable response.
"""
"""Initializes not modified cachable response."""
self.headers = [
h for h in response.headers if h[0] != "Content-Length"
]

def __call__(self, start_response):
""" WSGI call processing.
"""
"""WSGI call processing."""
start_response("304 Not Modified", self.headers)
return []


class CacheableResponse(object):
""" Cachable response.
"""
"""Cachable response."""

status_code = 200
last_modified = None
etag = None

def __init__(self, response):
""" Initializes cachable response.
"""
"""Initializes cachable response."""

def capture_headers(status, headers):
self.headers = [h for h in headers if h[0] != "Set-Cookie"]
Expand All @@ -165,7 +154,6 @@ def capture_headers(status, headers):
self.etag = cache_policy.http_etag

def __call__(self, start_response):
""" WSGI call processing.
"""
"""WSGI call processing."""
start_response("200 OK", self.headers)
return self.buffer

0 comments on commit 45f8157

Please sign in to comment.