|
|
@@ -71,17 +71,6 @@ def exception_to_httperror(*expected): |
|
|
|
# care of that. |
|
|
|
return decorator.decorator(wrapper) |
|
|
|
|
|
|
|
# Custom Cherrypy tools |
|
|
|
def allow_methods(methods): |
|
|
|
method = cherrypy.request.method.upper() |
|
|
|
if method not in methods: |
|
|
|
if method in cherrypy.request.methods_with_bodies: |
|
|
|
cherrypy.request.body.read() |
|
|
|
allowed = ', '.join(methods) |
|
|
|
cherrypy.response.headers['Allow'] = allowed |
|
|
|
raise cherrypy.HTTPError(405, method + " not allowed; use " + allowed) |
|
|
|
cherrypy.tools.allow_methods = cherrypy.Tool('before_handler', allow_methods) |
|
|
|
|
|
|
|
# CherryPy apps |
|
|
|
class Root(NilmApp): |
|
|
|
"""Root application for NILM database""" |
|
|
@@ -142,7 +131,7 @@ class Stream(NilmApp): |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_out() |
|
|
|
@exception_to_httperror(NilmDBError, ValueError) |
|
|
|
@cherrypy.tools.allow_methods(methods = ["POST"]) |
|
|
|
@cherrypy.tools.allow(methods = ["POST"]) |
|
|
|
def create(self, path, layout): |
|
|
|
"""Create a new stream in the database. Provide path |
|
|
|
and one of the nilmdb.layout.layouts keys. |
|
|
@@ -153,7 +142,7 @@ class Stream(NilmApp): |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_out() |
|
|
|
@exception_to_httperror(NilmDBError) |
|
|
|
@cherrypy.tools.allow_methods(methods = ["POST"]) |
|
|
|
@cherrypy.tools.allow(methods = ["POST"]) |
|
|
|
def destroy(self, path): |
|
|
|
"""Delete a stream and its associated data.""" |
|
|
|
return self.db.stream_destroy(path) |
|
|
@@ -186,7 +175,7 @@ class Stream(NilmApp): |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_out() |
|
|
|
@exception_to_httperror(NilmDBError, LookupError, TypeError) |
|
|
|
@cherrypy.tools.allow_methods(methods = ["POST"]) |
|
|
|
@cherrypy.tools.allow(methods = ["POST"]) |
|
|
|
def set_metadata(self, path, data): |
|
|
|
"""Set metadata for the named stream, replacing any |
|
|
|
existing metadata. Data should be a json-encoded |
|
|
@@ -198,7 +187,7 @@ class Stream(NilmApp): |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_out() |
|
|
|
@exception_to_httperror(NilmDBError, LookupError, TypeError) |
|
|
|
@cherrypy.tools.allow_methods(methods = ["POST"]) |
|
|
|
@cherrypy.tools.allow(methods = ["POST"]) |
|
|
|
def update_metadata(self, path, data): |
|
|
|
"""Update metadata for the named stream. Data |
|
|
|
should be a json-encoded dictionary""" |
|
|
@@ -208,7 +197,7 @@ class Stream(NilmApp): |
|
|
|
# /stream/insert?path=/newton/prep |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_out() |
|
|
|
@cherrypy.tools.allow_methods(methods = ["PUT"]) |
|
|
|
@cherrypy.tools.allow(methods = ["PUT"]) |
|
|
|
def insert(self, path, start, end): |
|
|
|
""" |
|
|
|
Insert new data into the database. Provide textual data |
|
|
@@ -264,7 +253,7 @@ class Stream(NilmApp): |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_out() |
|
|
|
@exception_to_httperror(NilmDBError) |
|
|
|
@cherrypy.tools.allow_methods(methods = ["POST"]) |
|
|
|
@cherrypy.tools.allow(methods = ["POST"]) |
|
|
|
def remove(self, path, start = None, end = None): |
|
|
|
""" |
|
|
|
Remove data from the backend database. Removes all data in |
|
|
@@ -427,8 +416,8 @@ class Server(object): |
|
|
|
|
|
|
|
# Only allow GET and HEAD by default. Individual handlers |
|
|
|
# can override. |
|
|
|
app_config.update({ 'tools.allow_methods.on': True, |
|
|
|
'tools.allow_methods.methods': ['GET', 'HEAD'] }) |
|
|
|
app_config.update({ 'tools.allow.on': True, |
|
|
|
'tools.allow.methods': ['GET', 'HEAD'] }) |
|
|
|
|
|
|
|
# Send tracebacks in error responses. They're hidden by the |
|
|
|
# error_page function for client errors (code 400-499). |
|
|
|