def test_not_implemented_callable_raises_exception_with_given_message(self): try: not_implemented('foo')() except RuntimeError as e: msg = e.args[0] else: msg = 'bar' self.assertEqual('foo', msg)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ MsgPack serializer """ import importlib from pygrapes.serializer.abstract import Abstract from pygrapes.util import not_implemented # discover parser try: t = importlib.import_module("msgpack") _decode = getattr(t, "loads") _encode = getattr(t, "dumps") except ImportError: _decode = _encode = not_implemented("A MsgPack module is required!") class MsgPack(Abstract): """ Msgpack serializer """ def loads(self, data): """ Method converts given string to internal objects """ return _decode(data) def dumps(self, data): """
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = "mib" __date__ = "$2011-01-22 12:02:41$" from pygrapes.util import not_implemented from pygrapes.adapter.abstract import Abstract from pygrapes.adapter.local import Local try: from pygrapes.adapter.zeromq import Zmq except ImportError: Zmq = not_implemented('A working pyzmq lib is required!') try: from pygrapes.adapter.amqp import Amqp except ImportError: Amqp = not_implemented('A working amqplib lib is required!') __all__ = ['Abstract', 'Amqp', 'Local', 'Zmq']
def test_not_implemented_callable_raises_runtime_error(self): self.assertRaises(RuntimeError, not_implemented(None))
def test_not_implemented_returns_callable(self): self.assertTrue(isinstance(not_implemented(None), collections.Callable))
#!/usr/bin/env python # -*- coding: utf-8 -*- """ JSON serializer """ import importlib from pygrapes.serializer.abstract import Abstract from pygrapes.util import not_implemented # discover JSON parser _implementations = [('json', 'loads', 'dumps'), ('cjson', 'decode', 'encode'), \ ('simplejson', 'loads', 'dumps'), ('django.utils.simplejson', 'loads', \ 'dumps')] _decode = _encode = not_implemented('A JSON parser is required!') for (module, loads, dumps) in _implementations: try: t = importlib.import_module(module) _decode = getattr(t, loads) _encode = getattr(t, dumps) break except ImportError: pass class Json(Abstract): """ JSON serializer """