Beispiel #1
0
    def unregisterUtility(self, component=None, provided=None, name=u'',
                          factory=None):
        if factory:
            if component:
                raise TypeError("Can't specify factory and component.")
            component = factory()

        if provided is None:
            if component is None:
                raise TypeError("Must specify one of component, factory and "
                                "provided")
            provided = _getUtilityProvided(component)

        # If the existing registration is a ComponentPathWrapper, we
        # convert the component that is to be unregistered to a wrapper.
        # This ensures that our custom comparision methods are called.
        if component is not None:
            old = self._utility_registrations.get((provided, name))
            if old is not None:
                if isinstance(old[0], ComponentPathWrapper):
                    unwrapped_component = Acquisition.aq_base(component)
                    component = ComponentPathWrapper(unwrapped_component,'')
            
        return super(PersistentComponents, self).unregisterUtility(
            component=component, provided=provided, name=name)
Beispiel #2
0
    def unregisterUtility(self,
                          component=None,
                          provided=None,
                          name=u'',
                          factory=None):
        if factory:
            if component:
                raise TypeError("Can't specify factory and component.")
            component = factory()

        if provided is None:
            if component is None:
                raise TypeError("Must specify one of component, factory and "
                                "provided")
            provided = _getUtilityProvided(component)

        # If the existing registration is a ComponentPathWrapper, we
        # convert the component that is to be unregistered to a wrapper.
        # This ensures that our custom comparision methods are called.
        if component is not None:
            old = self._utility_registrations.get((provided, name))
            if old is not None:
                if isinstance(old[0], ComponentPathWrapper):
                    unwrapped_component = Acquisition.aq_base(component)
                    component = ComponentPathWrapper(unwrapped_component, '')

        return super(PersistentComponents,
                     self).unregisterUtility(component=component,
                                             provided=provided,
                                             name=name)
Beispiel #3
0
    def registerUtility(self, component=None, provided=None, name=u'', info=u'',
                        event=True, factory=None):
        if factory:
            if component:
                raise TypeError("Can't specify factory and component.")
            component = factory()

        if provided is None:
            provided = _getUtilityProvided(component)

        reg = self._utility_registrations.get((provided, name))
        if reg is not None:
            if reg[:2] == (component, info):
                # already registered
                if isinstance(reg[0], ComponentPathWrapper):
                    # update path
                    self.utilities.unsubscribe((), provided, reg[0])
                    reg[0].path = component.getPhysicalPath()
                    self.utilities.subscribe((), provided, reg[0])
                return
            self.unregisterUtility(reg[0], provided, name)

        subscribed = False
        for ((p, _), data) in self._utility_registrations.iteritems():
            if p == provided and data[0] == component:
                subscribed = True
                break

        wrapped_component = component
        if getattr(component, 'aq_parent', None) is not None:
            # component is acquisition wrapped, so try to store path
            if getattr(component, 'getPhysicalPath', None) is None:
                raise AttributeError(
                    'Component %r does not implement getPhysicalPath, '
                    'so register it unwrapped or implement this method.' %
                    component)
            path = component.getPhysicalPath()
            # If the path is relative we can't store it because we
            # have nearly no chance to use the path for traversal in
            # getUtility.
            if path[0] == '':
                # We have an absolute path, so we can store it.
                wrapped_component = ComponentPathWrapper(
                    Acquisition.aq_base(component), path)

        self._utility_registrations[(provided, name)] = (
            wrapped_component, info, factory)
        self.utilities.register((), provided, name, wrapped_component)

        if not subscribed:
            self.utilities.subscribe((), provided, wrapped_component)

        if event:
            zope.event.notify(zope.component.interfaces.Registered(
                UtilityRegistration(
                    self, provided, name, component, info, factory)
                ))
Beispiel #4
0
    def registerUtility(self,
                        component=None,
                        provided=None,
                        name=u'',
                        info=u'',
                        event=True,
                        factory=None):
        if factory:
            if component:
                raise TypeError("Can't specify factory and component.")
            component = factory()

        if provided is None:
            provided = _getUtilityProvided(component)

        reg = self._utility_registrations.get((provided, name))
        if reg is not None:
            if reg[:2] == (component, info):
                # already registered
                if isinstance(reg[0], ComponentPathWrapper):
                    # update path
                    self.utilities.unsubscribe((), provided, reg[0])
                    reg[0].path = component.getPhysicalPath()
                    self.utilities.subscribe((), provided, reg[0])
                return
            self.unregisterUtility(reg[0], provided, name)

        subscribed = False
        for ((p, _), data) in self._utility_registrations.iteritems():
            if p == provided and data[0] == component:
                subscribed = True
                break

        wrapped_component = component
        if getattr(component, 'aq_parent', None) is not None:
            # component is acquisition wrapped, so try to store path
            if getattr(component, 'getPhysicalPath', None) is None:
                raise AttributeError(
                    'Component %r does not implement getPhysicalPath, '
                    'so register it unwrapped or implement this method.' %
                    component)
            path = component.getPhysicalPath()
            # If the path is relative we can't store it because we
            # have nearly no chance to use the path for traversal in
            # getUtility.
            if path[0] == '':
                # We have an absolute path, so we can store it.
                wrapped_component = ComponentPathWrapper(
                    Acquisition.aq_base(component), path)

        self._utility_registrations[(provided, name)] = (wrapped_component,
                                                         info, factory)
        self.utilities.register((), provided, name, wrapped_component)

        if not subscribed:
            self.utilities.subscribe((), provided, wrapped_component)

        if event:
            zope.event.notify(
                zope.component.interfaces.Registered(
                    UtilityRegistration(self, provided, name, component, info,
                                        factory)))