diff options
author | Robin ALEXANDER <colisee@hotmail.com> | 2022-02-11 17:50:03 +0100 |
---|---|---|
committer | Robin ALEXANDER <colisee@hotmail.com> | 2022-02-11 17:50:03 +0100 |
commit | 193b06825a1adb8cb90a7712cf8524deeda7bba6 (patch) | |
tree | 36bee38d9eeca8e3fb5ce4fe44c0f0a2272e7029 /gui/odr-dabmux-gui.py | |
parent | daf36c1ec9a705a9920e75dba7937b8b083ca298 (diff) | |
download | dabmux-193b06825a1adb8cb90a7712cf8524deeda7bba6.tar.gz dabmux-193b06825a1adb8cb90a7712cf8524deeda7bba6.tar.bz2 dabmux-193b06825a1adb8cb90a7712cf8524deeda7bba6.zip |
Switch to cherrypy from bottle
Diffstat (limited to 'gui/odr-dabmux-gui.py')
-rwxr-xr-x | gui/odr-dabmux-gui.py | 257 |
1 files changed, 134 insertions, 123 deletions
diff --git a/gui/odr-dabmux-gui.py b/gui/odr-dabmux-gui.py index 873bd3f..c469b35 100755 --- a/gui/odr-dabmux-gui.py +++ b/gui/odr-dabmux-gui.py @@ -27,127 +27,125 @@ # You should have received a copy of the GNU General Public License # along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>. -from muxconfig import * -from muxrc import * -from bottle import route, run, request, static_file, jinja2_template as template -import jinja2 -import json - +import os import argparse +import json +import cherrypy +import muxconfig +import muxrc +import jinja2 -@route('/config') -def config(): - """Show the JSON ptree in a textbox for editing""" - - conf.load() - - return template('configeditor', - version = conf.get_mux_version(), - config = json.dumps(conf.get_full_configuration(), indent=4), - message = "") - -@route('/config', method="POST") -def config_json_post(): - """Update the ODR-DabMux configuration with the JSON ptree - given as POST data""" - - new_config = request.forms.get('config') - print("New config %s" % new_config) - - success = conf.set_full_configuration(new_config) - - if success: - successmessage = "Success" - else: - successmessage = "Failure" - - conf.load() - - return template('configeditor', - version = conf.get_mux_version(), - config = json.dumps(conf.get_full_configuration(), indent=4), - message = successmessage) - -@route('/config.json', method="GET") -def config_json_get(): - """Return a application/json containing the full - ptree of the mux""" - - conf.load() - - return { 'version': conf.get_mux_version(), - 'config': conf.get_full_configuration() } - - -@route('/rc/<module>/<param>', method="GET") -def rc_get(module, param): - rc.load() - - value = rc.get_param_value(module, param) - - if param in paramObj: - paramList = paramObj[param] - label = paramObj["labels"][param] - else: - paramList = [] - label = "" - - return template('rcparam', - module = module, - param = param, - value = value, - label = label, - list = paramList) - -@route('/rc/<module>/<param>', method="POST") -def rc_post(module, param): - value = request.forms.get('newvalue') - - rc.set_param_value(module, param, value) - - value = rc.get_param_value(module, param) - - return """<html><head> - <meta http-equiv="refresh" content="0;url='/'" /> - </head></html>""" - -@route('/') -def index(): - conf.load() - rc.load() - - return template('index', - version = conf.get_mux_version(), - g = conf.get_general_options(), - services = conf.get_services(), - subchannels = conf.get_subchannels(), - components = conf.get_components(), - rcmodules = rc.get_modules()) - -@route('/services') -def index(): - conf.load() - - return template('services', - version = conf.get_mux_version(), - services = conf.get_services()) - -@route('/stats') -def index(): - conf.load() - - return template('stats', - version = conf.get_mux_version()) - -@route('/stats.json') -def stats_json(): - return conf.get_stats_dict() - - -@route('/static/<filename:path>') -def send_static(filename): - return static_file(filename, root='./static') - +class Root: + def __init__(self, env, conf, rc): + self.mconf = conf + self.mrc = rc + self.env = env + self.mparam = ModuleParameter(env, rc) + + def _cp_dispatch(self, vpath): + if len(vpath) == 3: + vpath.pop(0) # /rc/ + cherrypy.request.params['module'] = vpath.pop(0) # /module name/ + cherrypy.request.params['param'] = vpath.pop(0) # /parameter name/ + return self.mparam + + return vpath + + @cherrypy.expose + def config(self, config=None): + if config == None: + """Show the JSON ptree in a textbox for editing""" + self.mconf.load() + tmpl = self.env.get_template('configeditor.tpl') + return tmpl.render( + version = self.mconf.get_mux_version(), + config = json.dumps(self.mconf.get_full_configuration(), indent=4), + message = "") + else: + """Record the new configuration""" + success = self.mconf.set_full_configuration(config) + if success: + successmessage = "Success" + else: + successmessage = "Failure" + self.mconf.load() + return template('configeditor', + version = self.mconf.get_mux_version(), + config = json.dumps(self.mconf.get_full_configuration(), indent=4), + message = successmessage) + + @cherrypy.expose + @cherrypy.tools.json_out() + def config_json(self): + """Return a application/json containing the full + ptree of the mux""" + + self.mconf.load() + return { 'version': self.mconf.get_mux_version(), + 'config': self.mconf.get_full_configuration() } + + @cherrypy.expose + def index(self): + self.mconf.load() + self.mrc.load() + tmpl = self.env.get_template('index.tpl') + return tmpl.render( + version = self.mconf.get_mux_version(), + g = self.mconf.get_general_options(), + services = self.mconf.get_services(), + subchannels = self.mconf.get_subchannels(), + components = self.mconf.get_components(), + rcmodules = self.mrc.get_modules()) + + @cherrypy.expose + def services(self): + self.mconf.load() + tmpl = self.env.get_template('services.tpl') + return tmpl.render( + version = self.mconf.get_mux_version(), + services = self.mconf.get_services()) + + @cherrypy.expose + def stats(self): + self.mconf.load() + tmpl = self.env.get_template('stats.tpl') + return tmpl.render( + version = self.mconf.get_mux_version()) + + + @cherrypy.expose + @cherrypy.tools.json_out() + def stats_json(self): + return self.mconf.get_stats_dict() + +class ModuleParameter: + def __init__(self, env, rc): + self.mrc = rc + self.env = env + + @cherrypy.expose + def index(self, module, param, newvalue=None): + if newvalue != None: + rc.set_param_value(module, param, newvalue) + raise cherrypy.HTTPRedirect('/#rcmodules') + else: + self.mrc.load() + value = self.mrc.get_param_value(module, param) + + if param in paramObj: + paramList = paramObj[param] + label = paramObj["labels"][param] + else: + paramList = [] + label = "" + + tmpl = self.env.get_template('rcparam.tpl') + return tmpl.render( + module = module, + param = param, + value = value, + label = label, + list = paramList) if __name__ == '__main__': # Get configuration file in argument @@ -159,13 +157,26 @@ if __name__ == '__main__': parser.add_argument('--rcport', default='12722', help='mux zmq rc port (default: 12722)',required=False) cli_args = parser.parse_args() - conf = ConfigurationHandler(cli_args.mhost, cli_args.mport) - - rc = MuxRemoteControl(cli_args.mhost, cli_args.rcport) + # Instanciate mux-configuration and mux-remote-control objects + conf = muxconfig.ConfigurationHandler(cli_args.mhost, int(cli_args.mport)) + rc = muxrc.MuxRemoteControl(cli_args.mhost, int(cli_args.rcport)) # Import selectable paramaters values paramFile = open("rcparam.json") paramStr = paramFile.read() paramObj = json.loads(paramStr) - run(host=cli_args.host, port=int(cli_args.port), debug=True, reloader=False)
\ No newline at end of file + # Start cherrypy + env = jinja2.Environment(loader=jinja2.FileSystemLoader('views'), trim_blocks=True) + cherrypy.config.update({'server.socket_host': cli_args.host, 'server.socket_port': int(cli_args.port),}) + appconf = { + '/': { + 'tools.sessions.on': True, + 'tools.staticdir.root': os.path.abspath(os.getcwd()) + }, + '/static': { + 'tools.staticdir.on': True, + 'tools.staticdir.dir': './static' + } + } + cherrypy.quickstart(Root(env, conf, rc), '/', appconf) |