Skip to content
Permalink
Browse files

Disable CherryPy encode tool and normalize controller signatures

Since calling encode() is necessary for JSON responses, relying on the
encode tool for HTML responses isn't as obvious as handling it
explicitly. The impact to controllers is minimal since they are
already returning the result of jinja:render. But this allows for a
single return type rather than a union of string or bytes.
  • Loading branch information
lovett committed Jan 8, 2020
1 parent 7065c9d commit 55ca8ed4456e28cde791d690e563c5fcd3245d01
@@ -13,7 +13,7 @@ class Controller:

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

bookmarks = cherrypy.engine.publish(
@@ -11,7 +11,7 @@ class Controller:

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

offset = int(kwargs.get('offset', 0))
@@ -12,7 +12,7 @@ class Controller:
@staticmethod
@cherrypy.tools.json_in()
@cherrypy.tools.capture()
def POST():
def POST() -> None:
"""Transform a deployment notification from Azure to a notification."""

details = cherrypy.request.json
@@ -12,7 +12,7 @@ class Controller:
@staticmethod
@cherrypy.tools.provides(formats=("html",))
@cherrypy.tools.etag()
def GET(*_args, **_kwargs):
def GET(*_args, **_kwargs) -> bytes:
"""Present a static list of bookmarklets"""

later_url = cherrypy.engine.publish(
@@ -26,7 +26,7 @@ def check_wayback_availability(url):
return json.dumps(closest_snapshot).encode()

@cherrypy.tools.provides(formats=("json", "html"))
def GET(self, *args, **kwargs):
def GET(self, *args, **kwargs) -> bytes:
"""Display a list of recently bookmarked URLs, or URLs matching a
search.
@@ -101,12 +101,17 @@ def GET(self, *args, **kwargs):
page=page,
start_index=start_index,
end_index=end_index
)
).pop()

@staticmethod
def POST(url, title=None, tags=None, comments=None, added=None):
def POST(url, **kwargs) -> None:
"""Add a new bookmark, or update an existing one."""

title = kwargs.get("title")
tags = kwargs.get("tags")
comments = kwargs.get("comments")
added = kwargs.get("added")

result = cherrypy.engine.publish(
"scheduler:add",
2,
@@ -124,7 +129,7 @@ def POST(url, title=None, tags=None, comments=None, added=None):
cherrypy.response.status = 204

@staticmethod
def DELETE(url):
def DELETE(url) -> None:
"""Discard a previously bookmarked URL."""

deleted_rows = cherrypy.engine.publish(
@@ -138,7 +143,7 @@ def DELETE(url):
cherrypy.response.status = 204

@staticmethod
def taglist():
def taglist() -> bytes:
"""Render a list of all known bookmark tags using a dedicated
template.
@@ -74,7 +74,7 @@ def host_to_keyword(self, host):
return "live"

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

host = ''
@@ -145,7 +145,7 @@ def GET(self, *_args, **kwargs):
invalid=invalid
).pop()

def POST(self, url, name, group):
def POST(self, url, name, group) -> None:
"""Add a new URL to a group."""

host = self.url_to_host(url)
@@ -11,7 +11,7 @@ class Controller:

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

offset = int(kwargs.get('offset', 0))
@@ -11,7 +11,7 @@ class Controller:

@staticmethod
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
def GET(*_args, **kwargs) -> bytes:
"""Display a list of recent captures, or captures matching a URI path.
"""

@@ -11,7 +11,7 @@ class Controller:
show_on_homepage = False

@staticmethod
def GET(*_args, **_kwargs):
def GET(*_args, **_kwargs) -> None:
"""Request the country code list and populate the registry"""

country_codes = cherrypy.engine.publish(
@@ -10,7 +10,7 @@ class Controller:
show_on_homepage = False

@staticmethod
def POST(action=None):
def POST(action=None) -> None:
"""Schedule a database download."""

if action != "update":
@@ -13,7 +13,7 @@ class Controller:
show_on_homepage = True

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

name = kwargs.get('name', '')
@@ -81,7 +81,7 @@ def GET(self, *_args, **kwargs):
).pop()

@staticmethod
def redirect_to_first_grid(grids):
def redirect_to_first_grid(grids) -> None:
"""Identify the first item in the grid collection and redirect to
it.
@@ -12,7 +12,7 @@ class Controller:

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

headers = sorted(
@@ -24,7 +24,7 @@ def GET(*_args, **_kwargs):
return "\n".join([
f"{header[0]}: {header[1]}"
for header in headers
])
]).encode()

if cherrypy.request.wants == "json":
return json.dumps(headers).encode()
@@ -12,7 +12,7 @@ class Controller:

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

limit = kwargs.get('limit')
@@ -1,6 +1,7 @@
"""Present all the available applications."""

import sys
import typing
import cherrypy
from plugins import decorators

@@ -12,7 +13,7 @@ class Controller:
show_on_homepage = True

@decorators.log_runtime
def catalog_apps(self, apps):
def catalog_apps(self, apps) -> typing.Tuple[str, ...]:
"""Extract app summaries from module docstrings."""
catalog = []
for mount_path, controller in apps.items():
@@ -57,7 +58,7 @@ def catalog_apps(self, apps):

@cherrypy.tools.provides(formats=("html",))
@cherrypy.tools.etag()
def GET(self, *_args, **_kwargs) -> str:
def GET(self, *_args, **_kwargs) -> bytes:
"""List all available applications.
Apps can be excluded from this list by setting
@@ -12,7 +12,7 @@ class Controller:

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

return cherrypy.engine.publish(
@@ -22,11 +22,15 @@ def GET(*_args, **_kwargs):

@staticmethod
@cherrypy.tools.provides(formats=("html",))
def POST(url=None, username=None, password=None):
def POST(*_args, **kwargs) -> None:
"""Request an HTML page and display its the contents of its head
section.
"""

url = kwargs.get("url")
username = kwargs.get("username")
password = kwargs.get("password")

status_code = None
request_failed = False

@@ -12,7 +12,7 @@ class Controller:

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

client_ip = cherrypy.request.headers.get("Remote-Addr")
@@ -46,7 +46,7 @@ def GET(*_args, **_kwargs):
}).encode()

if cherrypy.request.wants == "text":
return f"client_ip={client_ip}\nexternal_ip={external_ip}"
return f"client_ip={client_ip}\nexternal_ip={external_ip}".encode()

return cherrypy.engine.publish(
"jinja:render",
@@ -34,7 +34,7 @@ class Controller:

@cherrypy.tools.json_in()
@cherrypy.tools.capture()
def POST(self):
def POST(self) -> None:
"""Receive a JSON payload from Jenkins"""

payload = self.normalize_payload(cherrypy.request.json)
@@ -12,7 +12,7 @@ class Controller:

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

error = None
@@ -13,7 +13,7 @@ class Controller:
exposed = True
show_on_homepage = False

def POST(self, *args, **kwargs):
def POST(self, *args, **kwargs) -> None:
"""
Dispatch to a subhandler.
"""
@@ -10,11 +10,13 @@ class Controller:
exposed = True

@staticmethod
def POST(group=None):
def POST(*_args, **kwargs) -> None:
"""
Schedule maintenance operations.
"""

group = kwargs.get("group")

if group == "db":
cherrypy.engine.publish(
"scheduler:add",
@@ -12,7 +12,7 @@ class Controller:
@staticmethod
@cherrypy.tools.capture()
@cherrypy.tools.json_in()
def POST():
def POST(*_args, **_kwargs) -> None:
"""Decide whether a notification is speakable."""

notification = cherrypy.request.json
@@ -11,7 +11,7 @@ class Controller:

@staticmethod
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **kwargs):
def GET(*_args, **kwargs) -> bytes:
"""
Display information about the specified number, or a search form to
look up a number
@@ -11,7 +11,7 @@ class Controller:

@staticmethod
@cherrypy.tools.provides(formats=("html",))
def GET(*_args, **_kwargs):
def GET(*_args, **_kwargs) -> bytes:
"""Perform a client-side redirect to the URL specified in the
querystring.
@@ -12,7 +12,7 @@ class Controller:

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

entries = ()
@@ -67,7 +67,7 @@ def GET(*_args, **kwargs):
).pop()

@staticmethod
def PUT(key, value, replace=False):
def PUT(key, value, replace=False) -> None:
"""Store a new entry in the database or replace an existing entry"""

key = key.strip()
@@ -83,7 +83,7 @@ def PUT(key, value, replace=False):
cherrypy.response.status = 204

@staticmethod
def DELETE(uid=None, key=None):
def DELETE(uid=None, key=None) -> None:
"""Remove an existing entry by its key or ID"""

if uid:
@@ -15,7 +15,7 @@ class Controller:

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

registry_rows = cherrypy.engine.publish(
@@ -63,7 +63,7 @@ def GET(*_args, **_kwargs):
).pop()

@staticmethod
def POST(*args, **kwargs):
def POST(*args, **kwargs) -> None:
"""Queue a new reminder for delivery."""

message = kwargs.get("message")
@@ -195,7 +195,7 @@ def POST(*args, **kwargs):
raise cherrypy.HTTPRedirect(redirect_url)

@staticmethod
def DELETE(uid):
def DELETE(uid) -> None:
"""Remove a previously-scheduled reminder."""

uid = float(uid)
@@ -10,6 +10,6 @@ class Controller:
exposed = True

@staticmethod
def GET(*_args, **_kwargs):
def GET(*_args, **_kwargs) -> None:
"""Redirect requests for non-static assets"""
raise cherrypy.HTTPRedirect("/")

0 comments on commit 55ca8ed

Please sign in to comment.
You can’t perform that action at this time.