1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# Copyright (C) 2015 Ipsilon Contributors see COPYING for license
import cherrypy
import json
from functools import wraps
from ipsilon.util.endpoint import Endpoint
def jsonout(func):
"""
JSON output decorator. Does not handle binary data.
"""
@wraps(func)
def wrapper(*args, **kw):
value = func(*args, **kw)
cherrypy.response.headers["Content-Type"] = \
"application/json;charset=utf-8"
return json.dumps(value, sort_keys=True, indent=2)
return wrapper
def rest_error(status=500, message=''):
"""
Create a REST error response.
The assumption is that the jsonout wrapper will handle converting
the response to JSON.
"""
cherrypy.response.status = status
cherrypy.response.headers['Content-Type'] = 'application/json'
return {'status': status, 'message': message}
class RestPage(Endpoint):
def __init__(self, *args, **kwargs):
super(RestPage, self).__init__(*args, **kwargs)
self.auth_protect = True
class RestPlugins(RestPage):
def __init__(self, name, site, parent, facility, ordered=True):
super(RestPlugins, self).__init__(site)
self._master = parent
self.name = name
self.title = '%s plugins' % name
self.url = '%s/%s' % (parent.url, name)
self.facility = facility
self.template = None
self.order = None
parent.add_subtree(name, self)
for plugin in self._site[facility].available:
obj = self._site[facility].available[plugin]
if hasattr(obj, 'rest'):
cherrypy.log.error('Rest plugin: %s' % plugin)
obj.rest.mount(self)
def root_with_msg(self, message=None, message_type=None, changed=None):
return None
def root(self, *args, **kwargs):
return self.root_with_msg()
class Rest(RestPage):
def __init__(self, site, mount):
super(Rest, self).__init__(site)
self.title = None
self.mount = mount
self.url = '%s/%s' % (self.basepath, mount)
self.menu = [self]
@jsonout
def root(self, *args, **kwargs):
return rest_error(404, 'Not Found')
def add_subtree(self, name, page):
self.__dict__[name] = page
def del_subtree(self, name):
del self.__dict__[name]
|