def subclass_with_self(self, Class, name=None, attribute='app', reverse=None, **kw): """Subclass an app-compatible class by setting its app attribute to be this app instance. App-compatible means that the class has a class attribute that provides the default app it should use, e.g. ``class Foo: app = None``. :param Class: The app-compatible class to subclass. :keyword name: Custom name for the target class. :keyword attribute: Name of the attribute holding the app, default is 'app'. """ Class = symbol_by_name(Class) reverse = reverse if reverse else Class.__name__ has_reduce_args = getattr(Class, '__reduce_args__', None) is not None def __reduce__(self): args = self.__reduce_args__() if has_reduce_args else [] return (_unpickle, (Class.__name__, args)) attrs = dict({attribute: self}, __module__=Class.__module__, __doc__=Class.__doc__, __reduce__=__reduce__, **kw) return type(name or Class.__name__, (Class, ), attrs)
def subclass_with_self(self, Class, name=None, attribute='app', reverse=None, **kw): """Subclass an app-compatible class by setting its app attribute to be this app instance. App-compatible means that the class has a class attribute that provides the default app it should use, e.g. ``class Foo: app = None``. :param Class: The app-compatible class to subclass. :keyword name: Custom name for the target class. :keyword attribute: Name of the attribute holding the app, default is 'app'. """ Class = symbol_by_name(Class) reverse = reverse if reverse else Class.__name__ has_reduce_args = getattr(Class, '__reduce_args__', None) is not None def __reduce__(self): args = self.__reduce_args__() if has_reduce_args else [] return (_unpickle, (Class.__name__, args)) attrs = dict({attribute: self}, __module__=Class.__module__, __doc__=Class.__doc__, __reduce__=__reduce__, **kw) return type(name or Class.__name__, (Class,), attrs)
def Processor(self): """Create safe processor.""" cls = symbol_by_name(self.processor_cls) attrs = dict(__module__=cls.__module__) return type(cls.__name__, (ProcessorMixin, cls), attrs)
def Handler(self): """Recreate handler class.""" return symbol_by_name(self.handler_cls)
def test_instance_returns_instance(self): instance = object() self.assertIs(symbol_by_name(instance), instance)
def test_package(self): from thriftworker.hub import Hub self.assertIs(symbol_by_name('.hub:Hub', package='thriftworker'), Hub) self.assertTrue(symbol_by_name(':ThriftWorker', package='thriftworker'))
def test_imp_reraises_ValueError(self): imp = Mock() imp.side_effect = ValueError() with self.assertRaises(ValueError): symbol_by_name('thriftworker:ThriftWorker', imp=imp)
def test_returns_default(self): default = object() self.assertIs(symbol_by_name('xyz.ryx.qedoa.weq:foz', default=default), default)
def test_returns_default(self): default = object() self.assertIs(symbol_by_name('xyz.ryx.qedoa.weq:foz', default=default), default)
def protocol_factory(self): """Create handler instance.""" ProtocolFactory = symbol_by_name(self.config.PROTOCOL_FACTORY_CLS) return ProtocolFactory()
def request_stack(self): """Store current requests.""" RequestStack = symbol_by_name('thriftpool.request.stack:RequestStack') return RequestStack()
def test_package(self): from thriftworker.hub import Hub self.assertIs(symbol_by_name('.hub:Hub', package='thriftworker'), Hub) self.assertTrue(symbol_by_name(':ThriftWorker', package='thriftworker'))
def test_imp_reraises_ValueError(self): imp = Mock() imp.side_effect = ValueError() with self.assertRaises(ValueError): symbol_by_name('thriftworker:ThriftWorker', imp=imp)
def test_no_default(self): with self.assertRaises(ImportError): symbol_by_name('xyz.ryx.qedoa.weq:foz')
def Handler(self): """Recreate handler class.""" return symbol_by_name(self.handler_cls)
def test_no_default(self): with self.assertRaises(ImportError): symbol_by_name('xyz.ryx.qedoa.weq:foz')
def Processor(self): """Create safe processor.""" cls = symbol_by_name(self.processor_cls) attrs = dict(__module__=cls.__module__) return type(cls.__name__, (ProcessorMixin, cls), attrs)
def test_instance_returns_instance(self): instance = object() self.assertIs(symbol_by_name(instance), instance)