-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpyrest.py
More file actions
executable file
·181 lines (153 loc) · 7.39 KB
/
pyrest.py
File metadata and controls
executable file
·181 lines (153 loc) · 7.39 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
#
# This file is protected by Copyright. Please refer to the COPYRIGHT file
# distributed with this source distribution.
#
# This file is part of REDHAWK rest-python.
#
# REDHAWK rest-python is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# REDHAWK rest-python is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
import os
from rest.domain import DomainInfo, DomainProperties
from rest.application import Applications
from rest.component import Component, ComponentProperties
from rest.devicemanager import DeviceManagers
from rest.device import Devices, DeviceProperties
from rest.port import PortHandler
from rest.bulkio_handler import BulkIOWebsocketHandler
from rest.sysinfo import SysInfoHandler
import tornado.httpserver
import tornado.web
import tornado.websocket
from tornado import ioloop
from model.redhawk import Redhawk
# setup command line options
from tornado.options import define, options
rhweb_path = os.getenv('RHWEBPATH', '/var/redhawk/web')
default_docpath = os.path.join(rhweb_path, 'redhawk-rest-doc')
define('port', default=8080, type=int, help="server port")
define("debug", default=False, type=bool, help="Enable Tornado debug mode. Reloads code")
define('staticpath', default=None, type=str, help='Path to the static content')
define('docpath', default=default_docpath, type=str, help='Path to the REDHAWK REST documentation')
_ID = r'/([^/]+)'
_LIST = r'/?'
_REST_PATH = r'/redhawk/rest'
_DOMAIN_PATH = _REST_PATH + '/domains'
_APPLICATION_PATH = _DOMAIN_PATH + _ID + r'/applications'
_COMPONENT_PATH = _APPLICATION_PATH + _ID + r'/components'
_DEVICE_MGR_PATH = _DOMAIN_PATH + _ID + r'/deviceManagers'
_DEVICE_PATH = _DEVICE_MGR_PATH + _ID + r'/devices'
_PROPERTIES_PATH = r'/properties'
_PORT_PATH = r'/ports'
_BULKIO_PATH = _PORT_PATH + _ID + r'/bulkio'
_DOC_PATH = _REST_PATH + r'/doc'
class Application(tornado.web.Application):
def __init__(self, *args, **kwargs):
'''
Create the REDHAWK Rest Application
Parameters
----------
docpath - str
Optional path to RESTful documentation.
Defaults to $REDHAWKWEB/share/redhawk-rest-doc.
static-path - str
Optional static content path. Defaults to None (no content)
Served as root content. The RESTful content takes precident over
static content.
_ioloop - tornado.ioloop.IOLoop
Optional Tornado IOLoop instance used for
registering callbacks. Useful for unit test
which have their own IOLoop. Defaults to current
IOLoop.
'''
# explicit _ioloop for unit testing
_ioloop = kwargs.get('_ioloop', None)
static_path = kwargs.get('static_path', None)
docpath = kwargs.get('docpath', None)
if not docpath:
docpath = default_docpath
# REDHAWK Service
redhawk = Redhawk()
handlers = [
(_REST_PATH + r'/sysinfo', SysInfoHandler, dict(redhawk=redhawk)),
# Domains
(_DOMAIN_PATH + _LIST, DomainInfo, dict(redhawk=redhawk)),
(_DOMAIN_PATH + _ID, DomainInfo, dict(redhawk=redhawk)),
(_DOMAIN_PATH + _ID + _PROPERTIES_PATH + _LIST, DomainProperties,
dict(redhawk=redhawk)),
(_DOMAIN_PATH + _ID + _PROPERTIES_PATH + _ID, DomainProperties,
dict(redhawk=redhawk)),
# Applications
(_APPLICATION_PATH + _LIST, Applications, dict(redhawk=redhawk)),
(_APPLICATION_PATH + _ID, Applications, dict(redhawk=redhawk)),
(_APPLICATION_PATH + _ID + _PORT_PATH + _LIST, PortHandler,
dict(redhawk=redhawk, kind='application')),
(_APPLICATION_PATH + _ID + _PORT_PATH + _ID, PortHandler,
dict(redhawk=redhawk, kind='application')),
(_APPLICATION_PATH + _ID + _BULKIO_PATH, BulkIOWebsocketHandler,
dict(redhawk=redhawk, kind='application', _ioloop=_ioloop)),
# Components
(_COMPONENT_PATH + _LIST, Component, dict(redhawk=redhawk)),
(_COMPONENT_PATH + _ID, Component, dict(redhawk=redhawk)),
(_COMPONENT_PATH + _ID + _PROPERTIES_PATH + _LIST, ComponentProperties,
dict(redhawk=redhawk)),
(_COMPONENT_PATH + _ID + _PROPERTIES_PATH + _ID, ComponentProperties,
dict(redhawk=redhawk)),
(_COMPONENT_PATH + _ID + _PORT_PATH + _LIST, PortHandler,
dict(redhawk=redhawk, kind='component')),
(_COMPONENT_PATH + _ID + _PORT_PATH + _ID, PortHandler,
dict(redhawk=redhawk, kind='component')),
(_COMPONENT_PATH + _ID + _BULKIO_PATH, BulkIOWebsocketHandler,
dict(redhawk=redhawk, kind='component', _ioloop=_ioloop)),
# Device Managers
(_DEVICE_MGR_PATH + _LIST, DeviceManagers, dict(redhawk=redhawk)),
(_DEVICE_MGR_PATH + _ID, DeviceManagers, dict(redhawk=redhawk)),
# Devices
(_DEVICE_PATH + _LIST, Devices, dict(redhawk=redhawk)),
(_DEVICE_PATH + _ID, Devices, dict(redhawk=redhawk)),
(_DEVICE_PATH + _ID + _PROPERTIES_PATH + _LIST, DeviceProperties,
dict(redhawk=redhawk)),
(_DEVICE_PATH + _ID + _PROPERTIES_PATH + _ID, DeviceProperties,
dict(redhawk=redhawk)),
(_DEVICE_PATH + _ID + _PORT_PATH + _LIST, PortHandler,
dict(redhawk=redhawk, kind='device')),
(_DEVICE_PATH + _ID + _PORT_PATH + _ID, PortHandler,
dict(redhawk=redhawk, kind='device')),
(_DEVICE_PATH + _ID + _BULKIO_PATH, BulkIOWebsocketHandler,
dict(redhawk=redhawk, kind='device', _ioloop=_ioloop)),
(_DOC_PATH + "/$", IndexHandler, dict(basepath=docpath)),
(_DOC_PATH + "/(.*)$", tornado.web.StaticFileHandler, dict(path=docpath))
]
if static_path:
filehandles = [
(r"/$", IndexHandler, {"basepath": static_path}),
(r"/(.*)/$", IndexHandler, {"basepath": static_path}),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": static_path})
]
handlers.extend(filehandles)
tornado.web.Application.__init__(self, handlers, *args, **kwargs)
class IndexHandler(tornado.web.RequestHandler):
def initialize(self, basepath):
self._basepath = basepath
def get(self, path=""):
filepath = os.path.join(self._basepath, path, "index.html")
print "Rendering %s" % filepath
self.render(filepath)
def main():
tornado.options.parse_command_line()
application = Application(debug=options.debug, static_path=options['staticpath'], docpath=options['docpath'])
application.listen(options.port)
ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()