Example #1
0
    def do_POST(self):  # noqa
        from dbmanagr.command import navigator, exporter, differ, executer
        from dbmanagr.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]))
                }))
Example #2
0
    def test_as_json(self):
        """Tests the jsonable.as_json function"""

        self.assertEqual(None, jsonable.as_json(None))
        self.assertEqual({}, jsonable.as_json({}))

        import datetime
        obj = datetime.datetime(2014, 5, 5)
        self.assertEqual(
            {
                '__cls__': 'datetime.datetime',
                'value': obj.isoformat()
            },
            jsonable.as_json(obj)
        )

        from sqlalchemy.util import KeyedTuple
        self.assertEqual(
            {
                '__cls__': 'sqlalchemy.util.KeyedTuple',
                '_labels': ['a'],
                'a': 1
            },
            jsonable.as_json(KeyedTuple([1], labels=['a']))
        )

        from sqlalchemy.sql.sqltypes import NullType
        self.assertEqual(None, jsonable.as_json(NullType()))

        self.assertEqual([1], jsonable.as_json([1]))
        self.assertEqual(u'a', jsonable.as_json('a'))

        class TestJsonable(jsonable.Jsonable):
            def __init__(self):
                self.a = 1
                self.b = jsonable.Jsonable()

        self.assertEqual(
            {
                '__cls__': "<class 'tests.test_jsonable.TestJsonable'>",
                'a': 1,
                'b': {
                    '__cls__': "<class 'dbmanagr.jsonable.Jsonable'>"
                }
            },
            jsonable.as_json(TestJsonable())
        )
Example #3
0
    def do_POST(self):  # noqa
        from dbmanagr.command import navigator, exporter, differ, executer
        from dbmanagr.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]))
            }))
Example #4
0
    def test_as_json(self):
        """Tests the jsonable.as_json function"""

        self.assertEqual(None, jsonable.as_json(None))
        self.assertEqual({}, jsonable.as_json({}))

        import datetime
        obj = datetime.datetime(2014, 5, 5)
        self.assertEqual(
            {
                '__cls__': 'datetime.datetime',
                'value': obj.isoformat()
            }, jsonable.as_json(obj))

        from sqlalchemy.util import KeyedTuple
        self.assertEqual(
            {
                '__cls__': 'sqlalchemy.util.KeyedTuple',
                '_labels': ['a'],
                'a': 1
            }, jsonable.as_json(KeyedTuple([1], labels=['a'])))

        from sqlalchemy.sql.sqltypes import NullType
        self.assertEqual(None, jsonable.as_json(NullType()))

        self.assertEqual([1], jsonable.as_json([1]))
        self.assertEqual(u'a', jsonable.as_json('a'))

        class TestJsonable(jsonable.Jsonable):
            def __init__(self):
                self.a = 1
                self.b = jsonable.Jsonable()

        self.assertEqual(
            {
                '__cls__': "<class 'tests.test_jsonable.TestJsonable'>",
                'a': 1,
                'b': {
                    '__cls__': "<class 'dbmanagr.jsonable.Jsonable'>"
                }
            }, jsonable.as_json(TestJsonable()))
Example #5
0
 def default(self, obj):  # pylint: disable=method-hidden
     if isinstance(obj, Jsonable):
         return obj.as_json()
     return as_json(obj)
Example #6
0
 def default(self, obj):  # pylint: disable=method-hidden
     if isinstance(obj, Jsonable):
         return obj.as_json()
     return as_json(obj)