Пример #1
0
    def do_POST(self):  # noqa
        from dbnav.command import navigator, exporter, differ, executer
        from dbnav.command import grapher

        commands = {
            'navigator': navigator,
            'exporter': exporter,
            'differ': differ,
            'executer': executer,
            'grapher': grapher
        }

        parts = self.path.split('/')
        command = parts[1]
        if command == 'server-status':
            self.send_response(200)
            self.end_headers()
            return
        if command == 'server-stop':
            self.send_response(200)
            self.end_headers()
            self.server.active = False
            return
        if command not in commands:
            self.send_error(404)
            return

        args = json.loads(self.rfile.read(
            int(self.headers.getheader('content-length'))))

        try:
            items = mute_stderr(commands[command].execute)(args)
            self.send_response(200)
            self.send_header('Content-Type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(items, cls=Encoder))
        except BaseException as e:
            logger.debug(e)
            self.send_response(500)
            self.send_header('Content-Type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps({
                '__cls__': str(e.__class__.__name__),
                'message': e.message,
                'traceback': as_json(traceback.extract_tb(sys.exc_info()[2]))
            }))
Пример #2
0
 def default(self, obj):  # pylint: disable=method-hidden
     if isinstance(obj, Jsonable):
         return obj.as_json()
     return as_json(obj)