Skip to content

Commit

Permalink
Rename wants tool to provides
Browse files Browse the repository at this point in the history
Due to contraints imposed by cherrypy, the supported formats can't be
provided as decorator args.
  • Loading branch information
lovett committed Jan 5, 2020
1 parent d3cb989 commit 93d104c
Show file tree
Hide file tree
Showing 30 changed files with 48 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ contextmanager-decorators=contextlib.contextmanager
# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members=cherrypy.tools.capture, cherrypy.tools.wants, cherrypy.tools.etag
generated-members=cherrypy.tools.capture, cherrypy.tools.provides, cherrypy.tools.etag

# Tells whether missing members accessed in mixin class should be ignored. A
# mixin class is detected if its name ends with "mixin" (case insensitive).
Expand Down
2 changes: 1 addition & 1 deletion apps/alturl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*args) -> str:
"""Dispatch to a site-specific handler."""

Expand Down
2 changes: 1 addition & 1 deletion apps/applog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
"""Display a list of recent log entries"""

Expand Down
2 changes: 1 addition & 1 deletion apps/bookmarklets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
@cherrypy.tools.etag()
def GET(*_args, **_kwargs):
"""Present a static list of bookmarklets"""
Expand Down
2 changes: 1 addition & 1 deletion apps/bookmarks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def check_wayback_availability(url):
closest_snapshot = snapshots.get("closest", {})
return closest_snapshot

@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(self, *args, **kwargs):
"""Display a list of recently bookmarked URLs, or URLs matching a
search.
Expand Down
2 changes: 1 addition & 1 deletion apps/bounce/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def host_to_keyword(self, host):

return "live"

@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(self, *_args, **kwargs):
"""Display all the URLs in a group."""

Expand Down
2 changes: 1 addition & 1 deletion apps/calls/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
"""Display a list of recent calls"""

Expand Down
2 changes: 1 addition & 1 deletion apps/captures/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
"""Display a list of recent captures, or captures matching a URI path.
"""
Expand Down
2 changes: 1 addition & 1 deletion apps/grids/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Controller:
exposed = True
show_on_homepage = True

@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(self, *_args, **kwargs):
"""Display the list of available grids, or the current grid"""

Expand Down
2 changes: 1 addition & 1 deletion apps/headers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants()
@cherrypy.tools.provides(formats=("json", "text", "html"))
def GET(*_args, **_kwargs):
"""Display the headers of the current request"""

Expand Down
1 change: 1 addition & 0 deletions apps/headers/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_custom_header(self):
as_json=True
)

print(response.body)
_, value = next(
pair
for pair in response.body
Expand Down
2 changes: 1 addition & 1 deletion apps/headlines/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
"""Display a list of headlines."""

Expand Down
2 changes: 1 addition & 1 deletion apps/homepage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def catalog_apps(self, apps):

return catalog

@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
@cherrypy.tools.etag()
def GET(self, *_args, **_kwargs) -> str:
"""List all available applications.
Expand Down
4 changes: 2 additions & 2 deletions apps/htmlhead/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **_kwargs):
"""Present a form for specifying a URL to fetch."""

Expand All @@ -21,7 +21,7 @@ def GET(*_args, **_kwargs):
).pop()

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def POST(url=None, username=None, password=None):
"""Request an HTML page and display its the contents of its head
section.
Expand Down
2 changes: 1 addition & 1 deletion apps/ip/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants()
@cherrypy.tools.provides(formats=("json", "text", "html"))
def GET(*_args, **_kwargs):
"""Display the client's local IP, and the server's external IP"""

Expand Down
2 changes: 1 addition & 1 deletion apps/later/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
"""Display a form for for bookmarking a URL"""

Expand Down
2 changes: 1 addition & 1 deletion apps/phone/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
"""
Display information about the specified number, or a search form to
Expand Down
2 changes: 1 addition & 1 deletion apps/redirect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Controller:
show_on_homepage = False

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **_kwargs):
"""Perform a client-side redirect to the URL specified in the
querystring.
Expand Down
2 changes: 1 addition & 1 deletion apps/registry/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants()
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
"""Display a UI to search for entries and add new ones."""

Expand Down
2 changes: 1 addition & 1 deletion apps/reminder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **_kwargs):
"""Display scheduled reminders, and a form to create new ones."""

Expand Down
2 changes: 1 addition & 1 deletion apps/speak/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **_kwargs):
"""Present an interface for on-demand muting of the speech service."""

Expand Down
4 changes: 2 additions & 2 deletions apps/startpage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def edit_page(self, page_name, page_content=None):
}

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
@cherrypy.tools.etag()
def render_page(page_name, page_record):
"""Render INI page content to HTML."""
Expand Down Expand Up @@ -139,7 +139,7 @@ def render_worker():
content_type="application/javascript"
)

@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(self, *args, **kwargs):
"""Render a page or present the edit form."""

Expand Down
4 changes: 2 additions & 2 deletions apps/transform/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def list_of_transforms(self):
"""Shape the list of transforms into a list of keys"""
return sorted(self.transforms.keys())

@cherrypy.tools.wants()
@cherrypy.tools.provides(formats=("json", "text", "html"))
def GET(self, *_args, **_kwargs):
"""The default view presents the available transformation methods"""

Expand All @@ -67,7 +67,7 @@ def GET(self, *_args, **_kwargs):
transforms=self.list_of_transforms()
).pop()

@cherrypy.tools.wants()
@cherrypy.tools.provides(formats=("json", "text", "html"))
def POST(self, transform, value=''):
"""Perform a transformation and display the result"""

Expand Down
2 changes: 1 addition & 1 deletion apps/visitors/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Controller:
exposed = True
show_on_homepage = True

@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(self, *_args, **kwargs):
"""Display a search interface, and the results of the default query"""

Expand Down
4 changes: 2 additions & 2 deletions apps/wakeup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
"""Display the list of hosts eligible for wakeup."""

Expand Down Expand Up @@ -40,7 +40,7 @@ def GET(*_args, **kwargs):
).pop()

@staticmethod
@cherrypy.tools.wants()
@cherrypy.tools.provides(formats=("text", "html"))
def POST(*_args, **kwargs):
"""Send a WoL packet to the mac address of the specified host."""

Expand Down
2 changes: 1 addition & 1 deletion apps/weather/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Controller:
exposed = True
show_on_homepage = True

@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(self, *args, **_kwargs):
"""Display selected parts of the most recent Darksky API query"""

Expand Down
2 changes: 1 addition & 1 deletion apps/whois/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Controller:
show_on_homepage = True

@staticmethod
@cherrypy.tools.wants(only="html")
@cherrypy.tools.provides(formats=("html",))
def GET(*_, **kwargs):
"""Display a search form and lookup results."""

Expand Down
4 changes: 2 additions & 2 deletions medley.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import plugins.url
import tools.capture
import tools.etag
import tools.wants
import tools.provides

# pylint: disable=too-many-statements
@plugins.decorators.log_runtime
Expand Down Expand Up @@ -124,7 +124,7 @@ def setup() -> None:
# Tools
cherrypy.tools.capture = tools.capture.Tool()
cherrypy.tools.etag = tools.etag.Tool()
cherrypy.tools.wants = tools.wants.Tool()
cherrypy.tools.provides = tools.provides.Tool()

# Mount the apps
for app in os.listdir(app_root):
Expand Down
4 changes: 2 additions & 2 deletions testing/cptestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class is a way of simulating an incoming request against a server that
import json
import cherrypy
import tools.capture
import tools.wants
import tools.provides
import tools.etag

cherrypy.config.update({'environment': "test_suite"})
cherrypy.tools.capture = tools.capture.Tool()
cherrypy.tools.wants = tools.wants.Tool()
cherrypy.tools.provides = tools.provides.Tool()
cherrypy.tools.etag = tools.etag.Tool()

# Don't start the HTTP server
Expand Down
24 changes: 13 additions & 11 deletions tools/wants.py → tools/provides.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
"""Identify the desired content type of a request."""
"""A Cherrypy tool to make it easier to work with the Accept header."""

import pathlib
import typing
import cherrypy


class Tool(cherrypy.Tool):
"""A Cherrypy tool to make it easier to work with the Accept header."""
"""Reconcile the desired content type of a request with what the
application supports.
"""

def __init__(self) -> None:
cherrypy.Tool.__init__(
self,
"before_request_body",
self.wants,
self.provides,
priority=10
)

@staticmethod
def wants(*_args, **kwargs) -> None:
"""Reshape the accept header as a custom request property.
def provides(formats: typing.Tuple[str]) -> None:
"""Populate a custom request property with a keyword describing the
client's desired content format.
Preference weights (;q=) are not considered.
Preference weights in the Accept header (;q=) are not considered.
If a JSON or TXT file extension is specified in the request
path, it takes precedence.
Callers can indicate what content types are supported by
setting the keyword argument "only" to a space-delimited list
of keywords.
passing known keywords via *args.
"""

only_wants = kwargs.get("only")

accept = cherrypy.request.headers.get("Accept", "*/*")

request_path = pathlib.Path(cherrypy.request.path_info)
Expand All @@ -52,5 +54,5 @@ def wants(*_args, **kwargs) -> None:
cherrypy.request.wants = "json"
response_headers["Content-Type"] = "application/json"

if only_wants and cherrypy.request.wants not in only_wants:
if cherrypy.request.wants not in formats:
raise cherrypy.HTTPError(406)

0 comments on commit 93d104c

Please sign in to comment.