def test_get_command_names(self): """Test that command names are returned from a config directory.""" exp = ['make-bash-completion', 'make-command', 'make-optparse', 'serve-html-interface'] obs = get_command_names('pyqi.interfaces.optparse.config') self.assertEqual(obs, exp) # Invalid config dir. with self.assertRaises(ImportError): _ = get_command_names('foo.bar.baz-aar')
def test_get_command_names(self): """Test that command names are returned from a config directory.""" exp = [ 'make-bash-completion', 'make-command', 'make-optparse', 'make-release', 'serve-html-interface' ] obs = get_command_names('pyqi.interfaces.optparse.config') self.assertEqual(obs, exp) # Invalid config dir. with self.assertRaises(ImportError): _ = get_command_names('foo.bar.baz-aar')
def run(self, **kwargs): driver = kwargs['driver_name'] cfg_mod_path = kwargs['command_config_module'] cfg_mod = _get_cfg_module(cfg_mod_path) command_names = get_command_names(cfg_mod_path) command_list = ' '.join(command_names) commands = [] for cmd in command_names: cmd_cfg, _ = get_command_config(cfg_mod_path, cmd, exit_on_failure=False) if cmd_cfg is not None: command_options = [] command_options.extend( sorted(['--%s' % p.Name for p in cmd_cfg.inputs])) opts = ' '.join(command_options) commands.append(command_fmt % { 'command': cmd, 'options': opts }) all_commands = ''.join(commands) return { 'result': script_fmt % { 'driver': driver, 'commands': all_commands, 'command_list': command_list } }
def run(self, **kwargs): driver = kwargs['driver_name'] cfg_mod_path = kwargs['command_config_module'] cfg_mod = _get_cfg_module(cfg_mod_path) command_names = get_command_names(cfg_mod_path) command_list = ' '.join(command_names) commands = [] for cmd in command_names: cmd_cfg, _ = get_command_config(cfg_mod_path, cmd, exit_on_failure=False) if cmd_cfg is not None: command_options = [] command_options.extend( sorted(['--%s' % p.Name for p in cmd_cfg.inputs])) opts = ' '.join(command_options) commands.append(command_fmt % {'command':cmd, 'options':opts}) all_commands = ''.join(commands) return {'result':script_fmt % {'driver':driver, 'commands':all_commands, 'command_list':command_list}}
def get_http_handler(module): """Return a subclassed BaseHTTPRequestHandler with module in scope.""" module_commands = get_command_names(module) class HTMLInterfaceHTTPHandler(BaseHTTPRequestHandler): """Handle incoming HTTP requests""" def __init__(self, *args, **kwargs): self._unrouted = True #Apparently this is an 'oldstyle' class, which doesn't allow the use of super() BaseHTTPRequestHandler.__init__(self, *args, **kwargs) def index(self, write): write("<html><head><title>") write("PyQi: " + module) write("</title>") write("<style>") write(HTMLInterface.css_style) write("</style>") write("</head><body>") write("<h1>Available Commands:</h1>") write("<ul>") for command in module_commands: write( '<li><a href="/%s">%s</a></li>'%(command, command) ) write("</ul>") write("</body></html>") def route(self, path, output_writer): """Define a route for an output_writer""" if self._unrouted and self.path == path: self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() output_writer(self.wfile.write) self.wfile.close() self._unrouted = False; def command_route(self, command): """Define a route for a command and write the command page""" if self._unrouted and self.path == ("/" + command): cmd_obj = get_cmd_obj(module, command) self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() cmd_obj.command_page_writer(self.wfile.write, [], {}) self.wfile.close() self._unrouted = False def post_route(self, command, postvars): """Define a route for user response and write the output or else provide errors""" if self._unrouted and self.path == ("/" + command): cmd_obj = get_cmd_obj(module, command) try: result = cmd_obj(postvars) except Exception as e: result = { 'type':'error', 'errors':[e] } if result['type'] == 'error': self.send_response(400) self.send_header('Content-type', 'text/html') self.end_headers() cmd_obj.command_page_writer(self.wfile.write, result['errors'], postvars) elif result['type'] == 'page': self.send_response(200) self.send_header('Content-type', result['mime_type']) self.end_headers() self.wfile.write(result['contents']) elif result['type'] == 'download': self.send_response(200) self.send_header('Content-type', 'application/octet-stream') self.send_header('Content-disposition', 'attachment; filename='+result['filename']) self.end_headers() self.wfile.write(result['contents']) self.wfile.close() self._unrouted = False def end_routes(self): """If a route hasn't matched the path up to now, return a 404 and close stream""" if self._unrouted: self.send_response(404) self.end_headers() self.wfile.close() self._unrouted = False def do_GET(self): """Handle GET requests""" self.route("/", self.index) self.route("/index", self.index) self.route("/home", self.index) def r(write):#host.domain.tld/help write("This is still a very in development interface, there is no help.") self.route("/help", r) for command in module_commands: self.command_route(command) self.end_routes() def do_POST(self): """Handle POST requests""" postvars = FieldStorage(fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type']}) for command in module_commands: self.post_route(command, postvars) self.end_routes() return HTMLInterfaceHTTPHandler
def get_http_handler(module): """Return a subclassed BaseHTTPRequestHandler with module in scope.""" module_commands = get_command_names(module) class HTMLInterfaceHTTPHandler(BaseHTTPRequestHandler): """Handle incoming HTTP requests""" def __init__(self, *args, **kwargs): self._unrouted = True #Apparently this is an 'oldstyle' class, which doesn't allow the use of super() BaseHTTPRequestHandler.__init__(self, *args, **kwargs) def index(self, write): write("<html><head><title>") write("PyQi: " + module) write("</title>") write("<style>") write(HTMLInterface.css_style) write("</style>") write("</head><body>") write("<h1>Available Commands:</h1>") write("<ul>") for command in module_commands: write('<li><a href="/%s">%s</a></li>' % (command, command)) write("</ul>") write("</body></html>") def route(self, path, output_writer): """Define a route for an output_writer""" if self._unrouted and self.path == path: self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() output_writer(self.wfile.write) self.wfile.close() self._unrouted = False def command_route(self, command): """Define a route for a command and write the command page""" if self._unrouted and self.path == ("/" + command): cmd_obj = get_cmd_obj(module, command) self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() cmd_obj.command_page_writer(self.wfile.write, [], {}) self.wfile.close() self._unrouted = False def post_route(self, command, postvars): """Define a route for user response and write the output or else provide errors""" if self._unrouted and self.path == ("/" + command): cmd_obj = get_cmd_obj(module, command) try: result = cmd_obj(postvars) except Exception as e: result = {'type': 'error', 'errors': [e]} if result['type'] == 'error': self.send_response(400) self.send_header('Content-type', 'text/html') self.end_headers() cmd_obj.command_page_writer(self.wfile.write, result['errors'], postvars) elif result['type'] == 'page': self.send_response(200) self.send_header('Content-type', result['mime_type']) self.end_headers() self.wfile.write(result['contents']) elif result['type'] == 'download': self.send_response(200) self.send_header('Content-type', 'application/octet-stream') self.send_header( 'Content-disposition', 'attachment; filename=' + result['filename']) self.end_headers() self.wfile.write(result['contents']) self.wfile.close() self._unrouted = False def end_routes(self): """If a route hasn't matched the path up to now, return a 404 and close stream""" if self._unrouted: self.send_response(404) self.end_headers() self.wfile.close() self._unrouted = False def do_GET(self): """Handle GET requests""" self.route("/", self.index) self.route("/index", self.index) self.route("/home", self.index) def r(write): #host.domain.tld/help write( "This is still a very in development interface, there is no help." ) self.route("/help", r) for command in module_commands: self.command_route(command) self.end_routes() def do_POST(self): """Handle POST requests""" postvars = FieldStorage(fp=self.rfile, headers=self.headers, environ={ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': self.headers['Content-Type'] }) for command in module_commands: self.post_route(command, postvars) self.end_routes() return HTMLInterfaceHTTPHandler