def _duplicateAdapterForClassOrInterfaceAllowed(self, original):
        """
        Verify that when C{components.ALLOW_DUPLICATES} is set to C{True}, new
        adapter registrations for a particular from-type/interface and
        to-interface pair replace older registrations.
        """
        firstAdapter = lambda o: False
        secondAdapter = lambda o: True
        class TheInterface(Interface):
            pass
        components.registerAdapter(firstAdapter, original, TheInterface)
        components.ALLOW_DUPLICATES = True
        try:
            components.registerAdapter(secondAdapter, original, TheInterface)
            self.assertIs(
                components.getAdapterFactory(original, TheInterface, None),
                secondAdapter)
        finally:
            components.ALLOW_DUPLICATES = False

        # It should be rejected again at this point
        self.assertRaises(
            ValueError,
            components.registerAdapter,
            firstAdapter, original, TheInterface)

        self.assertIs(
            components.getAdapterFactory(original, TheInterface, None),
            secondAdapter)
Beispiel #2
0
    def _duplicateAdapterForClassOrInterfaceAllowed(self, original):
        """
        Verify that when C{components.ALLOW_DUPLICATES} is set to C{True}, new
        adapter registrations for a particular from-type/interface and
        to-interface pair replace older registrations.
        """
        firstAdapter = lambda o: False
        secondAdapter = lambda o: True

        class TheInterface(Interface):
            pass

        components.registerAdapter(firstAdapter, original, TheInterface)
        components.ALLOW_DUPLICATES = True
        try:
            components.registerAdapter(secondAdapter, original, TheInterface)
            self.assertIs(
                components.getAdapterFactory(original, TheInterface, None),
                secondAdapter)
        finally:
            components.ALLOW_DUPLICATES = False

        # It should be rejected again at this point
        self.assertRaises(ValueError, components.registerAdapter, firstAdapter,
                          original, TheInterface)

        self.assertIs(
            components.getAdapterFactory(original, TheInterface, None),
            secondAdapter)
 def _multipleInterfacesForClassOrInterface(self, original):
     """
     Verify that an adapter can be registered for multiple to-interfaces at a
     time.
     """
     adapter = lambda o: None
     components.registerAdapter(adapter, original, ITest, ITest2)
     self.assertIs(components.getAdapterFactory(original, ITest, None), adapter)
     self.assertIs(components.getAdapterFactory(original, ITest2, None), adapter)
 def _multipleInterfacesForClassOrInterface(self, original):
     """
     Verify that an adapter can be registered for multiple to-interfaces at a
     time.
     """
     adapter = lambda o: None
     components.registerAdapter(adapter, original, ITest, ITest2)
     self.assertIs(
         components.getAdapterFactory(original, ITest, None), adapter)
     self.assertIs(
         components.getAdapterFactory(original, ITest2, None), adapter)
Beispiel #5
0
 def _multipleInterfacesForClassOrInterface(self, original):
     adapter = lambda o: None
     class FirstInterface(Interface):
         pass
     class SecondInterface(Interface):
         pass
     components.registerAdapter(adapter, original, FirstInterface, SecondInterface)
     self.assertIdentical(
         components.getAdapterFactory(original, FirstInterface, None),
         adapter)
     self.assertIdentical(
         components.getAdapterFactory(original, SecondInterface, None),
         adapter)
 def _multipleInterfacesForClassOrInterface(self, original):
     adapter = lambda o: None
     class FirstInterface(Interface):
         pass
     class SecondInterface(Interface):
         pass
     components.registerAdapter(adapter, original, FirstInterface, SecondInterface)
     self.assertIdentical(
         components.getAdapterFactory(original, FirstInterface, None),
         adapter)
     self.assertIdentical(
         components.getAdapterFactory(original, SecondInterface, None),
         adapter)
Beispiel #7
0
 def _subclassAdapterRegistrationForClassOrInterface(self, original):
     firstAdapter = lambda o: True
     secondAdapter = lambda o: False
     class TheSubclass(original):
         pass
     class TheInterface(Interface):
         pass
     components.registerAdapter(firstAdapter, original, TheInterface)
     components.registerAdapter(secondAdapter, TheSubclass, TheInterface)
     self.assertIdentical(
         components.getAdapterFactory(original, TheInterface, None),
         firstAdapter)
     self.assertIdentical(
         components.getAdapterFactory(TheSubclass, TheInterface, None),
         secondAdapter)
 def _subclassAdapterRegistrationForClassOrInterface(self, original):
     firstAdapter = lambda o: True
     secondAdapter = lambda o: False
     class TheSubclass(original):
         pass
     class TheInterface(Interface):
         pass
     components.registerAdapter(firstAdapter, original, TheInterface)
     components.registerAdapter(secondAdapter, TheSubclass, TheInterface)
     self.assertIdentical(
         components.getAdapterFactory(original, TheInterface, None),
         firstAdapter)
     self.assertIdentical(
         components.getAdapterFactory(TheSubclass, TheInterface, None),
         secondAdapter)
Beispiel #9
0
 def __conform__(self, interface, registry=None, default=None):
     for providedInterface in self.provided:
         if providedInterface.isOrExtends(interface):
             return self.load()
         if getAdapterFactory(providedInterface, interface, None) is not None:
             return interface(self.load(), default)
     return default
Beispiel #10
0
 def __conform__(self, interface, registry=None, default=None):
     for providedInterface in self.provided:
         if providedInterface.isOrExtends(interface):
             return self.load()
         if getAdapterFactory(providedInterface, interface, None) is not None:
             return interface(self.load(), default)
     return default
Beispiel #11
0
 def _registerAdapterForClassOrInterface(self, original):
     adapter = lambda o: None
     class TheInterface(Interface):
         pass
     components.registerAdapter(adapter, original, TheInterface)
     self.assertIdentical(
         components.getAdapterFactory(original, TheInterface, None),
         adapter)
 def _registerAdapterForClassOrInterface(self, original):
     adapter = lambda o: None
     class TheInterface(Interface):
         pass
     components.registerAdapter(adapter, original, TheInterface)
     self.assertIdentical(
         components.getAdapterFactory(original, TheInterface, None),
         adapter)
 def _registerAdapterForClassOrInterface(self, original):
     """
     Register an adapter with L{components.registerAdapter} for the given
     class or interface and verify that the adapter can be looked up with
     L{components.getAdapterFactory}.
     """
     adapter = lambda o: None
     components.registerAdapter(adapter, original, ITest)
     self.assertIs(components.getAdapterFactory(original, ITest, None), adapter)
Beispiel #14
0
    def _subclassAdapterRegistrationForClassOrInterface(self, original):
        """
        Verify that a new adapter can be registered for a particular
        to-interface from a subclass of a type or interface which already has an
        adapter registered to that interface and that the subclass adapter takes
        precedence over the base class adapter.
        """
        firstAdapter = lambda o: True
        secondAdapter = lambda o: False

        class TheSubclass(original):
            pass

        components.registerAdapter(firstAdapter, original, ITest)
        components.registerAdapter(secondAdapter, TheSubclass, ITest)
        self.assertIs(components.getAdapterFactory(original, ITest, None),
                      firstAdapter)
        self.assertIs(components.getAdapterFactory(TheSubclass, ITest, None),
                      secondAdapter)
 def _subclassAdapterRegistrationForClassOrInterface(self, original):
     """
     Verify that a new adapter can be registered for a particular
     to-interface from a subclass of a type or interface which already has an
     adapter registered to that interface and that the subclass adapter takes
     precedence over the base class adapter.
     """
     firstAdapter = lambda o: True
     secondAdapter = lambda o: False
     class TheSubclass(original):
         pass
     components.registerAdapter(firstAdapter, original, ITest)
     components.registerAdapter(secondAdapter, TheSubclass, ITest)
     self.assertIs(
         components.getAdapterFactory(original, ITest, None),
         firstAdapter)
     self.assertIs(
         components.getAdapterFactory(TheSubclass, ITest, None),
         secondAdapter)
 def _registerAdapterForClassOrInterface(self, original):
     """
     Register an adapter with L{components.registerAdapter} for the given
     class or interface and verify that the adapter can be looked up with
     L{components.getAdapterFactory}.
     """
     adapter = lambda o: None
     components.registerAdapter(adapter, original, ITest)
     self.assertIs(
         components.getAdapterFactory(original, ITest, None),
         adapter)
Beispiel #17
0
 def _duplicateAdapterForClassOrInterface(self, original):
     """
     Verify that L{components.registerAdapter} raises L{ValueError} if the
     from-type/interface and to-interface pair is not unique.
     """
     firstAdapter = lambda o: False
     secondAdapter = lambda o: True
     components.registerAdapter(firstAdapter, original, ITest)
     self.assertRaises(ValueError, components.registerAdapter,
                       secondAdapter, original, ITest)
     # Make sure that the original adapter is still around as well
     self.assertIs(components.getAdapterFactory(original, ITest, None),
                   firstAdapter)
 def _duplicateAdapterForClassOrInterface(self, original):
     firstAdapter = lambda o: False
     secondAdapter = lambda o: True
     class TheInterface(Interface):
         pass
     components.registerAdapter(firstAdapter, original, TheInterface)
     self.assertRaises(
         ValueError,
         components.registerAdapter,
         secondAdapter, original, TheInterface)
     # Make sure that the original adapter is still around as well
     self.assertIdentical(
         components.getAdapterFactory(original, TheInterface, None),
         firstAdapter)
Beispiel #19
0
    def _duplicateAdapterForClassOrInterface(self, original):
        firstAdapter = lambda o: False
        secondAdapter = lambda o: True

        class TheInterface(Interface):
            pass

        components.registerAdapter(firstAdapter, original, TheInterface)
        self.assertRaises(ValueError, components.registerAdapter,
                          secondAdapter, original, TheInterface)
        # Make sure that the original adapter is still around as well
        self.assertIdentical(
            components.getAdapterFactory(original, TheInterface, None),
            firstAdapter)
    def _duplicateAdapterForClassOrInterfaceAllowed(self, original):
        firstAdapter = lambda o: False
        secondAdapter = lambda o: True
        class TheInterface(Interface):
            pass
        components.registerAdapter(firstAdapter, original, TheInterface)
        components.ALLOW_DUPLICATES = True
        try:
            components.registerAdapter(secondAdapter, original, TheInterface)
            self.assertIdentical(
                components.getAdapterFactory(original, TheInterface, None),
                secondAdapter)
        finally:
            components.ALLOW_DUPLICATES = False

        # It should be rejected again at this point
        self.assertRaises(
            ValueError,
            components.registerAdapter,
            firstAdapter, original, TheInterface)

        self.assertIdentical(
            components.getAdapterFactory(original, TheInterface, None),
            secondAdapter)
Beispiel #21
0
    def _duplicateAdapterForClassOrInterfaceAllowed(self, original):
        firstAdapter = lambda o: False
        secondAdapter = lambda o: True

        class TheInterface(Interface):
            pass

        components.registerAdapter(firstAdapter, original, TheInterface)
        components.ALLOW_DUPLICATES = True
        try:
            components.registerAdapter(secondAdapter, original, TheInterface)
            self.assertIdentical(
                components.getAdapterFactory(original, TheInterface, None),
                secondAdapter)
        finally:
            components.ALLOW_DUPLICATES = False

        # It should be rejected again at this point
        self.assertRaises(ValueError, components.registerAdapter, firstAdapter,
                          original, TheInterface)

        self.assertIdentical(
            components.getAdapterFactory(original, TheInterface, None),
            secondAdapter)
Beispiel #22
0
  def __init__(self, portal, config):
    assert isinstance(portal, Portal)
    assert isinstance(config, ConfigParser)

    self.portal = portal

    self.privateKeys = {
      'ssh-rsa': keys.Key.fromFile(config.get("DEFAULT", "privateKeyLocation"))
    }

    self.publicKeys = {
      'ssh-rsa': keys.Key.fromFile(config.get("DEFAULT", "publicKeyLocation"))
    }

    if components.getAdapterFactory(ConchUser, ISession, None) is None:
      components.registerAdapter(Session, ConchUser, ISession)
 def _duplicateAdapterForClassOrInterface(self, original):
     """
     Verify that L{components.registerAdapter} raises L{ValueError} if the
     from-type/interface and to-interface pair is not unique.
     """
     firstAdapter = lambda o: False
     secondAdapter = lambda o: True
     components.registerAdapter(firstAdapter, original, ITest)
     self.assertRaises(
         ValueError,
         components.registerAdapter,
         secondAdapter, original, ITest)
     # Make sure that the original adapter is still around as well
     self.assertIs(
         components.getAdapterFactory(original, ITest, None),
         firstAdapter)
Beispiel #24
0
def registerIfNotRegistered(adapter, from_, to):
    if not components.getAdapterFactory(from_, to, None):
        components.registerAdapter(adapter, from_, to)
Beispiel #25
0
def registerIfNotRegistered(adapter, from_, to):
    if not components.getAdapterFactory(from_, to, None):
        components.registerAdapter(adapter, from_, to)