-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpyrest.py
executable file
·135 lines (114 loc) · 5.52 KB
/
pyrest.py
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
#!/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
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
define('port', default=8080, type=int, help="server port")
define("debug", default=False, type=bool, help="Enable Tornado debug mode. Reloads code")
_ID = r'/([^/]+)'
_LIST = r'/?'
_DOMAIN_PATH = r'/redhawk/rest/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'
class Application(tornado.web.Application):
def __init__(self, *args, **kwargs):
# explicit _ioloop for unit testing
_ioloop = kwargs.get('_ioloop', None)
cwd = os.path.abspath(os.path.dirname(__import__(__name__).__file__))
# REDHAWK Service
redhawk = Redhawk()
handlers = [
(r"/apps/(.*)/$", IndexHandler),
(r"/apps/(.*)", tornado.web.StaticFileHandler, {"path": os.path.join(cwd, "apps")}),
# 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)),
]
tornado.web.Application.__init__(self, handlers, *args, **kwargs)
class IndexHandler(tornado.web.RequestHandler):
def get(self, path):
self.render("apps/"+path+"/index.html")
def main():
tornado.options.parse_command_line()
application = Application(debug=options.debug)
application.listen(options.port)
ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()