def unhook_zca(self): """ Call :func:`zope.component.getSiteManager.reset` to undo the action of :meth:`pyramid.config.Configurator.hook_zca`. If :mod:`zope.component` cannot be imported, this method will raise an :exc:`ImportError`.""" getSiteManager.reset()
def load_zcml(self, zcml_asset_specification): def _get_site_manager(context=None): return self if ':' not in zcml_asset_specification: import alpaca config_package = alpaca config_file = zcml_asset_specification else: package_name, config_file = zcml_asset_specification.split(':') __import__(package_name) config_package = sys.modules[package_name] context = ConfigurationMachine() context.package = config_package xmlconfig.registerCommonDirectives(context) xmlconfig.file( config_file, package=config_package, context=context, execute=False ) getSiteManager.sethook(_get_site_manager) try: context.execute_actions() finally: getSiteManager.reset()
def tearDown(unhook_zca=True): """Undo the effects :func:`pyramid.testing.setUp`. Use this function in the ``tearDown`` method of a unit test that uses :func:`pyramid.testing.setUp` in its ``setUp`` method. If the ``unhook_zca`` argument is ``True`` (the default), call :func:`zope.component.getSiteManager.reset`. This undoes the action of :func:`pyramid.testing.setUp` called with the argument ``hook_zca=True``. If :mod:`zope.component` cannot be imported, ignore the argument. """ global have_zca if unhook_zca and have_zca: try: from zope.component import getSiteManager getSiteManager.reset() except ImportError: # pragma: no cover have_zca = False info = manager.pop() manager.clear() if info is not None: registry = info['registry'] if hasattr(registry, '__init__') and hasattr(registry, '__name__'): try: registry.__init__(registry.__name__) except TypeError: # calling __init__ is largely for the benefit of # people who want to use the global ZCA registry; # however maybe somebody's using a registry we don't # understand, let's not blow up pass
def tearDown(self): from pyramid.threadlocal import manager manager.clear() getSiteManager = self._getSM() if getSiteManager is not None: getSiteManager.reset()
def tearDown(unhook_zca=True): """Undo the effects of :func:`pyramid.testing.setUp`. Use this function in the ``tearDown`` method of a unit test that uses :func:`pyramid.testing.setUp` in its ``setUp`` method. If the ``unhook_zca`` argument is ``True`` (the default), call :func:`zope.component.getSiteManager.reset`. This undoes the action of :func:`pyramid.testing.setUp` when called with the argument ``hook_zca=True``. If :mod:`zope.component` cannot be imported, ``unhook_zca`` is set to ``False``. """ global have_zca if unhook_zca and have_zca: try: from zope.component import getSiteManager getSiteManager.reset() except ImportError: # pragma: no cover have_zca = False info = manager.pop() manager.clear() if info is not None: registry = info['registry'] if hasattr(registry, '__init__') and hasattr(registry, '__name__'): try: registry.__init__(registry.__name__) except TypeError: # calling __init__ is largely for the benefit of # people who want to use the global ZCA registry; # however maybe somebody's using a registry we don't # understand, let's not blow up pass
def pushGlobalRegistry(new=None): """Set a new global component registry that uses the current registry as a a base. If you use this, you *must* call ``popGlobalRegistry()`` to restore the original state. If ``new`` is not given, a new registry is created. If given, you must provide a ``zope.component.globalregistry.BaseGlobalComponents`` object. Returns the new registry. """ from zope.component import globalregistry # Save the current top of the stack in a registry current = globalregistry.base # The first time we're called, we need to put the default global # registry at the bottom of the stack, and then patch the class to use # the stack for loading pickles. Otherwise, we end up with POSKey and # pickling errors when dealing with persistent registries that have the # global registry (globalregistry.base) as a base if len(_REGISTRIES) == 0: _REGISTRIES.append(current) globalregistry.BaseGlobalComponents._old__reduce__ = ( globalregistry.BaseGlobalComponents.__reduce__) globalregistry.BaseGlobalComponents.__reduce__ = ( lambda self: (loadRegistry, (self.__name__,))) if new is None: name = 'test-stack-{0}'.format(len(_REGISTRIES)) new = globalregistry.BaseGlobalComponents(name=name, bases=(current,)) logger.debug( 'New component registry: %s based on %s', name, current.__name__) else: logger.debug('Push component registry: %s', new.__name__) _REGISTRIES.append(new) # Monkey patch this into the three (!) places where zope.component # references it as a module global variable _hookRegistry(new) # Reset the site manager hook so that getSiteManager() returns the base # again from zope.component import getSiteManager getSiteManager.reset() try: from zope.component.hooks import setSite, setHooks except ImportError: pass else: setSite() setHooks() return new
def test_it_with_settings_passed_implicit_registry(self): from zope.component import getSiteManager from pyramid.threadlocal import manager try: config = self._callFUT(hook_zca=False, settings=dict(a=1)) self.assertEqual(config.registry.settings['a'], 1) finally: getSiteManager.reset() manager.clear()
def test_it_with_request(self): from zope.component import getSiteManager from pyramid.threadlocal import manager request = object() try: self._callFUT(request=request) current = manager.get() self.assertEqual(current['request'], request) finally: getSiteManager.reset() manager.clear()
def test_it_with_hook_zca_false(self): from zope.component import getSiteManager from pyramid.threadlocal import manager registry = object() try: self._callFUT(registry=registry, hook_zca=False) sm = getSiteManager() self.failIf(sm is registry) finally: getSiteManager.reset() manager.clear()
def test_it_with_hook_zca_false(self): from zope.component import getSiteManager from pyramid.threadlocal import manager from pyramid.registry import Registry registry = Registry() try: self._callFUT(registry=registry, hook_zca=False) sm = getSiteManager() self.failIf(sm is registry) finally: getSiteManager.reset() manager.clear()
def test_it_with_registry(self): from pyramid.registry import Registry from zope.component import getSiteManager from pyramid.threadlocal import manager registry = Registry() try: self._callFUT(registry=registry) current = manager.get() self.assertEqual(current['registry'], registry) finally: getSiteManager.reset() manager.clear()
def test_unhook_zc_false(self): from pyramid.threadlocal import manager from zope.component import getSiteManager hook = lambda *arg: None try: getSiteManager.sethook(hook) self._callFUT(unhook_zca=False) finally: result = getSiteManager.sethook(None) self.assertEqual(result, hook) getSiteManager.reset() manager.clear()
def test_unhook_zca(self): from zope.component import getSiteManager def foo(): '123' try: getSiteManager.sethook(foo) config = self._makeOne() config.unhook_zca() sm = getSiteManager() self.assertNotEqual(sm, '123') finally: getSiteManager.reset()
def test_hook_zca(self): from zope.component import getSiteManager def foo(): '123' try: config = self._makeOne() config.hook_zca() config.begin() sm = getSiteManager() self.assertEqual(sm, config.registry) finally: getSiteManager.reset()
def test_uses_configured_site_manager(self): from zope.interface.registry import Components from zope.component import getSiteManager from zope.component.testfiles.components import comp, IApp registry = Components() def dummy(context=None): return registry getSiteManager.sethook(dummy) try: self._callFUT('registerUtility', comp, IApp, u'') self.assertTrue(registry.getUtility(IApp) is comp) finally: getSiteManager.reset()
def test_defaults(self): from pyramid.threadlocal import manager from zope.component import getSiteManager registry = DummyRegistry() old = {'registry':registry} hook = lambda *arg: None try: getSiteManager.sethook(hook) manager.push(old) self._callFUT() current = manager.get() self.assertNotEqual(current, old) self.assertEqual(registry.inited, 2) finally: result = getSiteManager.sethook(None) self.assertNotEqual(result, hook) getSiteManager.reset() manager.clear()
def test_uses_configured_site_manager(self): from zope.interface.registry import Components from zope.component import getSiteManager from zope.component.testfiles.components import comp, IApp from zope.component._compat import _BLANK registry = Components() def dummy(context=None): return registry getSiteManager.sethook(dummy) try: self._callFUT('registerUtility', comp, IApp, _BLANK) self.assertTrue(registry.getUtility(IApp) is comp) finally: getSiteManager.reset()
def test_defaults(self): from pyramid.threadlocal import manager from zope.component import getSiteManager registry = DummyRegistry() old = {'registry': registry} hook = lambda *arg: None try: getSiteManager.sethook(hook) manager.push(old) self._callFUT() current = manager.get() self.assertNotEqual(current, old) self.assertEqual(registry.inited, 2) finally: result = getSiteManager.sethook(None) self.assertNotEqual(result, hook) getSiteManager.reset() manager.clear()
def test_it_defaults(self): from pyramid.threadlocal import manager from pyramid.threadlocal import get_current_registry from pyramid.registry import Registry from zope.component import getSiteManager old = True manager.push(old) try: config = self._callFUT() current = manager.get() self.failIf(current is old) self.assertEqual(config.registry, current['registry']) self.assertEqual(current['registry'].__class__, Registry) self.assertEqual(current['request'], None) finally: result = getSiteManager.sethook(None) self.assertEqual(result, get_current_registry) getSiteManager.reset() manager.clear()
def tearDown(unhook_zca=True): """Undo the effects :func:`pyramid.testing.setUp`. Use this function in the ``tearDown`` method of a unit test that uses :func:`pyramid.testing.setUp` in its ``setUp`` method. If the ``unhook_zca`` argument is ``True`` (the default), call :func:`zope.component.getSiteManager.reset`. This undoes the action of :func:`pyramid.testing.setUp` called with the argument ``hook_zca=True``. If :mod:`zope.component` cannot be imported, ignore the argument. .. warning:: Although this method of tearing a test setup down will never disappear, after :app:`Pyramid` 1.0, using the ``begin`` and ``end`` methods of a ``Configurator`` are preferred to using ``pyramid.testing.setUp`` and ``pyramid.testing.tearDown``. See :ref:`unittesting_chapter` for more information. """ if unhook_zca: try: from zope.component import getSiteManager getSiteManager.reset() except ImportError: # pragma: no cover pass info = manager.pop() manager.clear() if info is not None: registry = info["registry"] if hasattr(registry, "__init__") and hasattr(registry, "__name__"): try: registry.__init__(registry.__name__) except TypeError: # calling __init__ is largely for the benefit of # people who want to use the global ZCA registry; # however maybe somebody's using a registry we don't # understand, let's not blow up pass _clearContext() # XXX why?
def popGlobalRegistry(): """Restore the global component registry form the top of the stack, as set with ``pushGlobalRegistry()``. """ from zope.component import globalregistry if not _REGISTRIES or not _REGISTRIES[-1] is globalregistry.base: msg = ("popGlobalRegistry() called out of sync with " "pushGlobalRegistry()") raise ValueError(msg) current = _REGISTRIES.pop() previous = current.__bases__[0] # If we only have the default registry on the stack, return it to its # original state and empty the stack if len(_REGISTRIES) == 1: assert _REGISTRIES[0] is previous _REGISTRIES.pop() globalregistry.BaseGlobalComponents.__reduce__ = ( globalregistry.BaseGlobalComponents._old__reduce__) _hookRegistry(previous) # Reset the site manager hook so that getSiteManager() returns the base # again from zope.component import getSiteManager getSiteManager.reset() try: from zope.component.hooks import setSite, setHooks except ImportError: pass else: setSite() setHooks() return previous
def popGlobalRegistry(): """Restore the global component registry form the top of the stack, as set with ``pushGlobalRegistry()``. """ from zope.component import globalregistry if not _REGISTRIES or not _REGISTRIES[-1] is globalregistry.base: msg = ('popGlobalRegistry() called out of sync with ' 'pushGlobalRegistry()') raise ValueError(msg) current = _REGISTRIES.pop() previous = current.__bases__[0] # If we only have the default registry on the stack, return it to its # original state and empty the stack if len(_REGISTRIES) == 1: assert _REGISTRIES[0] is previous _REGISTRIES.pop() globalregistry.BaseGlobalComponents.__reduce__ = ( globalregistry.BaseGlobalComponents._old__reduce__) _hookRegistry(previous) # Reset the site manager hook so that getSiteManager() returns the base # again from zope.component import getSiteManager getSiteManager.reset() try: from zope.component.hooks import setSite, setHooks except ImportError: pass else: setSite() setHooks() return previous
def tearDown(self): getSiteManager.reset()
def tearDown(self): from pyramid.threadlocal import manager manager.clear() getSiteManager.reset()
def unhook_zca(self): """Call :func:`zope.component.getSiteManager.reset` to undo the action of :meth:`pyramid.config.Configurator.hook_zca`.""" from zope.component import getSiteManager getSiteManager.reset()
def tearDown(self): from zope.component import getSiteManager getSiteManager.reset()
def unhook_zca(self): """ Call :func:`zope.component.getSiteManager.reset` to undo the action of :meth:`pyramid.config.Configurator.hook_zca`.""" from zope.component import getSiteManager getSiteManager.reset()