def test_w_file_passed(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration import tests from zope.configuration.tests import simple context = ConfigurationMachine() registerCommonDirectives(context) before_stack = context.stack[:] context.package = tests fqn = _packageFile(tests, 'simple.zcml') logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): self._callFUT(context, 'simple.zcml') self.assertEqual(len(logger.debugs), 1) self.assertEqual(logger.debugs[0], ('include %s' % fqn, (), {})) self.assertEqual(len(context.actions), 3) action = context.actions[0] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqn, )) self.assertEqual(action['args'][0].path, _packageFile(tests, 'simple.py')) action = context.actions[1] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqn, )) self.assertEqual(action['args'][0].path, _packageFile(tests, 'simple.zcml')) action = context.actions[2] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqn, )) self.assertEqual(action['args'][0].path, _packageFile(tests, '__init__.py')) self.assertEqual(context.stack, before_stack) self.assertEqual(len(context._seen_files), 1) self.assertIn(fqn, context._seen_files)
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 test_w_file_passed(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration import tests from zope.configuration.tests import simple context = ConfigurationMachine() registerCommonDirectives(context) before_stack = context.stack[:] context.package = tests fqn = _packageFile(tests, 'simple.zcml') logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): self._callFUT(context, 'simple.zcml') self.assertEqual(len(logger.debugs), 1) self.assertEqual(logger.debugs[0], ('include %s', (fqn,), {})) self.assertEqual(len(context.actions), 3) action = context.actions[0] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqn,)) self.assertEqual(action['args'][0].path, _packageFile(tests, 'simple.py')) action = context.actions[1] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqn,)) self.assertEqual(action['args'][0].path, _packageFile(tests, 'simple.zcml')) action = context.actions[2] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqn,)) self.assertEqual(action['args'][0].path, _packageFile(tests, '__init__.py')) self.assertEqual(context.stack, before_stack) self.assertEqual(len(context._seen_files), 1) self.assertIn(fqn, context._seen_files)
def test_neither_file_nor_files_passed(self): from zope.configuration.config import ConfigurationMachine from zope.configuration.tests import samplepackage context = ConfigurationMachine() context.package = samplepackage fqn = _packageFile(samplepackage, 'configure.zcml') self._callFUT(context) self.assertEqual(len(context.actions), 0) self.assertEqual(len(context._seen_files), 1) self.assertIn(fqn, context._seen_files)
def test_w_file_passed(self): from zope.configuration.config import ConfigurationMachine from zope.configuration import tests context = ConfigurationMachine() context.package = tests fqn = _packageFile(tests, 'simple.zcml') self._callFUT(context, 'simple.zcml') self.assertEqual(len(context.actions), 0) self.assertEqual(len(context._seen_files), 1) self.assertIn(fqn, context._seen_files)
def zcml_configure(name, package): """ Given a ZCML filename as ``name`` and a Python package as ``package`` which the filename should be relative to, load the ZCML into the current ZCML registry. """ context = ConfigurationMachine() xmlconfig.registerCommonDirectives(context) context.package = package xmlconfig.include(context, name, package) context.execute_actions(clear=False) # the raison d'etre return context.actions
def file(name, package=None, context=None, execute=True): """Execute a zcml file """ if context is None: context = ConfigurationMachine() registerCommonDirectives(context) context.package = package include(context, name, package) if execute: context.execute_actions() return context
def test_actions_have_parents_includepath(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration import tests from zope.configuration.tests import simple context = ConfigurationMachine() fqp = _packageFile(tests, 'configure.zcml') registerCommonDirectives(context) before_stack = context.stack[:] context.package = tests # dummy action, path from "previous" include context.includepath = (fqp, ) def _callable(): pass context.actions.append({ 'discriminator': None, 'callable': _callable, }) fqn = _packageFile(tests, 'simple.zcml') logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): self._callFUT(context, 'simple.zcml') self.assertEqual(len(logger.debugs), 1) self.assertEqual(logger.debugs[0], ('include %s' % fqn, (), {})) self.assertEqual(len(context.actions), 4) action = context.actions[0] self.assertEqual(action['discriminator'], None) self.assertEqual(action['callable'], _callable) action = context.actions[1] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqp, )) self.assertEqual(action['args'][0].path, _packageFile(tests, 'simple.py')) action = context.actions[2] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqp, )) self.assertEqual(action['args'][0].path, _packageFile(tests, 'simple.zcml')) action = context.actions[3] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqp, )) self.assertEqual(action['args'][0].path, _packageFile(tests, '__init__.py')) self.assertEqual(context.stack, before_stack) self.assertEqual(len(context._seen_files), 1) self.assertIn(fqn, context._seen_files)
def test_neither_file_nor_files_passed_already_seen(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration.tests import samplepackage context = ConfigurationMachine() registerCommonDirectives(context) context.package = samplepackage fqn = _packageFile(samplepackage, 'configure.zcml') context._seen_files.add(fqn) logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): self._callFUT(context) #skips self.assertEqual(len(logger.debugs), 0) self.assertEqual(len(context.actions), 0)
def get_configuration_context(package=None): """Get configuration context. Various functions take a configuration context as argument. From looking at zope.configuration.xmlconfig.file the following seems about right. Note: this is a copy of a function in plone.autoinclude.tests.utils. The duplication is deliberate: I don't want one package to import code from the other, for now. """ context = ConfigurationMachine() registerCommonDirectives(context) if package is not None: # When you set context.package, context.path(filename) works nicely. context.package = package return context
def test_actions_have_parents_includepath(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration import tests from zope.configuration.tests import simple context = ConfigurationMachine() fqp = _packageFile(tests, 'configure.zcml') registerCommonDirectives(context) before_stack = context.stack[:] context.package = tests # dummy action, path from "previous" include context.includepath = (fqp,) def _callable(): raise AssertionError("should not be called") context.actions.append({'discriminator': None, 'callable': _callable, }) fqn = _packageFile(tests, 'simple.zcml') logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): self._callFUT(context, 'simple.zcml') self.assertEqual(len(logger.debugs), 1) self.assertEqual(logger.debugs[0], ('include %s', (fqn,), {})) self.assertEqual(len(context.actions), 4) action = context.actions[0] self.assertEqual(action['discriminator'], None) self.assertEqual(action['callable'], _callable) action = context.actions[1] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqp,)) self.assertEqual(action['args'][0].path, _packageFile(tests, 'simple.py')) action = context.actions[2] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqp,)) self.assertEqual(action['args'][0].path, _packageFile(tests, 'simple.zcml')) action = context.actions[3] self.assertEqual(action['callable'], simple.file_registry.append) self.assertEqual(action['includepath'], (fqp,)) self.assertEqual(action['args'][0].path, _packageFile(tests, '__init__.py')) self.assertEqual(context.stack, before_stack) self.assertEqual(len(context._seen_files), 1) self.assertIn(fqn, context._seen_files)
def test_neither_file_nor_files_passed(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration.tests import samplepackage from zope.configuration.tests.samplepackage import foo context = ConfigurationMachine() registerCommonDirectives(context) before_stack = context.stack[:] context.package = samplepackage fqn = _packageFile(samplepackage, 'configure.zcml') logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): self._callFUT(context) self.assertEqual(len(logger.debugs), 1) self.assertEqual(logger.debugs[0], ('include %s', (fqn,), {})) self.assertEqual(len(context.actions), 1) action = context.actions[0] self.assertEqual(action['callable'], foo.data.append) self.assertEqual(action['includepath'], (fqn,)) self.assertEqual(context.stack, before_stack) self.assertEqual(len(context._seen_files), 1) self.assertIn(fqn, context._seen_files)
def test_wo_execute_w_context(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration.tests import samplepackage from zope.configuration.tests.samplepackage import foo context = ConfigurationMachine() context.package = samplepackage registerCommonDirectives(context) file_name = path("samplepackage", "configure.zcml") logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): ret = self._callFUT('configure.zcml', context=context, execute=False) self.assertTrue(ret is context) self.assertEqual(len(logger.debugs), 1) self.assertEqual(logger.debugs[0], ('include %s', (file_name,), {})) self.assertEqual(len(foo.data), 0) self.assertEqual(len(context.actions), 1) action = context.actions[0] self.assertEqual(action['discriminator'], (('x', (b'blah')), ('y', 0))) self.assertEqual(action['callable'], foo.data.append)
def test_neither_file_nor_files_passed(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration.tests import samplepackage from zope.configuration.tests.samplepackage import foo context = ConfigurationMachine() registerCommonDirectives(context) before_stack = context.stack[:] context.package = samplepackage fqn = _packageFile(samplepackage, 'configure.zcml') logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): self._callFUT(context) self.assertEqual(len(logger.debugs), 1) self.assertEqual(logger.debugs[0], ('include %s' % fqn, (), {})) self.assertEqual(len(context.actions), 1) action = context.actions[0] self.assertEqual(action['callable'], foo.data.append) self.assertEqual(action['includepath'], (fqn, )) self.assertEqual(context.stack, before_stack) self.assertEqual(len(context._seen_files), 1) self.assertIn(fqn, context._seen_files)
def test_wo_execute_w_context(self): from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration.tests import samplepackage from zope.configuration.tests.samplepackage import foo context = ConfigurationMachine() context.package = samplepackage registerCommonDirectives(context) file_name = path("samplepackage", "configure.zcml") logger = LoggerStub() with _Monkey(xmlconfig, logger=logger): ret = self._callFUT('configure.zcml', context=context, execute=False) self.assertTrue(ret is context) self.assertEqual(len(logger.debugs), 1) self.assertEqual(logger.debugs[0], ('include %s', (file_name, ), {})) self.assertEqual(len(foo.data), 0) self.assertEqual(len(context.actions), 1) action = context.actions[0] self.assertEqual(action['discriminator'], (('x', (b'blah')), ('y', 0))) self.assertEqual(action['callable'], foo.data.append)
def register_path(config, spec, discovery=False, indexes=[], request_type=None): """ Add a skin path to the current configuration state. If ``discovery`` is enabled, the path will automatically be monitored for changes. The ``indexes`` argument is an optional list of view registrations with the provided names. The ``request_type`` option decides the request type for which to make the registration. """ package_name, path = resolve_asset_spec(spec) if package_name is not None: path = pkg_resources.resource_filename(package_name, path) else: path = caller_path(path) if package_name is None: # absolute filename package = config.package else: __import__(package_name) package = sys.modules[package_name] context = ConfigurationMachine() context.registry = config.registry context.autocommit = False context.package = package context.route_prefix = getattr(config, 'route_prefix', None) directive = skins(context, path, discovery, request_type) for index in indexes: directive.view(config, index) for action in directive(): config.action(*action)
def load_zcml(self, spec='configure.zcml', lock=threading.Lock(), features=()): """ Load configuration from a :term:`ZCML` file into the current configuration state. The ``spec`` argument is an absolute filename, a relative filename, or a :term:`asset specification`, defaulting to ``configure.zcml`` (relative to the package of the method's caller). The ``features`` argument can be any iterable of strings. These are useful for conditionally including or excluding parts of a :term:`ZCML` file. """ package_name, filename = self._split_spec(spec) if package_name is None: # absolute filename package = self.package else: __import__(package_name) package = sys.modules[package_name] # To avoid breaking people's expectations of how ZCML works, we # cannot autocommit ZCML actions incrementally. If we commit actions # incrementally, configuration outcome will be controlled purely by # ZCML directive execution order, which isn't what anyone who uses # ZCML expects. So we don't autocommit each ZCML directive action # while parsing is happening, but we do make sure to commit right # after parsing if autocommit it True. context = ConfigurationMachine() for feature in features: context.provideFeature(feature) context.registry = self.registry context.autocommit = False context.package = package context.route_prefix = getattr(self, 'route_prefix', None) context.introspection = getattr(self, 'introspection', True) context.config_class = self.__class__ registerCommonDirectives(context) self.manager.push({'registry': self.registry, 'request': None}) lock.acquire() try: # old_action_state will be None for Pyramid 1.0 and 1.1, but # not for 1.2 old_action_state = getattr(self.registry, 'action_state', None) if old_action_state is not None: # For Pyramid 1.2+, we need to assign a temporary action state to # the registry, because the configurator actions must populate # the context's action list (instead of the registry action # state's action list) in order for includeOverrides to work # properly. from pyramid.config import ActionState self.registry.action_state = ActionState() self.registry.action_state.actions = context.actions xmlconfig.file(filename, package, context=context, execute=False) finally: if old_action_state is not None: # if we reassigned the action state, restore the old one (1.2 only) self.registry.action_state = old_action_state lock.release() self.manager.pop() self._ctx.actions.extend(context.actions) if self.autocommit: self.commit() return self.registry
def load_zcml(self, spec='configure.zcml', lock=threading.Lock(), features=()): """ Load configuration from a :term:`ZCML` file into the current configuration state. The ``spec`` argument is an absolute filename, a relative filename, or a :term:`asset specification`, defaulting to ``configure.zcml`` (relative to the package of the method's caller). The ``features`` argument can be any iterable of strings. These are useful for conditionally including or excluding parts of a :term:`ZCML` file. """ package_name, filename = self._split_spec(spec) if package_name is None: # absolute filename package = self.package else: __import__(package_name) package = sys.modules[package_name] # To avoid breaking people's expectations of how ZCML works, we # cannot autocommit ZCML actions incrementally. If we commit actions # incrementally, configuration outcome will be controlled purely by # ZCML directive execution order, which isn't what anyone who uses # ZCML expects. So we don't autocommit each ZCML directive action # while parsing is happening, but we do make sure to commit right # after parsing if autocommit it True. context = ConfigurationMachine() for feature in features: context.provideFeature(feature) context.registry = self.registry context.autocommit = False context.route_prefix = getattr(self, 'route_prefix', None) context.package = package registerCommonDirectives(context) self.manager.push({'registry':self.registry, 'request':None}) lock.acquire() try: # old_action_state will be None for Pyramid 1.0 and 1.1, but # not for 1.2 old_action_state = getattr(self.registry, 'action_state', None) if old_action_state is not None: # For Pyramid 1.2+, we need to assign a temporary action state to # the registry, because the configurator actions must populate # the context's action list (instead of the registry action # state's action list) in order for includeOverrides to work # properly. from pyramid.config import ActionState self.registry.action_state = ActionState() self.registry.action_state.actions = context.actions xmlconfig.file(filename, package, context=context, execute=False) finally: if old_action_state is not None: # if we reassigned the action state, restore the old one (1.2 only) self.registry.action_state = old_action_state lock.release() self.manager.pop() _ctx = self._ctx if _ctx is None: # pragma: no cover ; will never be true under 1.2a5+ _ctx = self._ctx = self._make_context(self.autocommit) _ctx.actions.extend(context.actions) if self.autocommit: self.commit() return self.registry