当前位置: 首页>>代码示例>>Python>>正文


Python Components.registerUtility方法代码示例

本文整理汇总了Python中zope.interface.registry.Components.registerUtility方法的典型用法代码示例。如果您正苦于以下问题:Python Components.registerUtility方法的具体用法?Python Components.registerUtility怎么用?Python Components.registerUtility使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在zope.interface.registry.Components的用法示例。


在下文中一共展示了Components.registerUtility方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_init_forwards_kw

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import registerUtility [as 别名]
 def test_init_forwards_kw(self):
     from zope.interface import Interface
     from zope.interface.registry import Components
     dummy = object()
     c = Components()
     c.registerUtility(dummy, Interface)
     registry = self._makeOne(bases=(c,))
     self.assertEqual(registry.getUtility(Interface), dummy)
开发者ID:JDeuce,项目名称:pyramid,代码行数:10,代码来源:test_registry.py

示例2: test_extendning

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import registerUtility [as 别名]
    def test_extendning(self):
        c1 = Components('1')
        self.assertEqual(c1.__bases__, ())

        c2 = Components('2', (c1, ))
        self.assertTrue(c2.__bases__ == (c1, ))

        test_object1 = U1(1)
        test_object2 = U1(2)
        test_object3 = U12(1)
        test_object4 = U12(3)

        self.assertEqual(len(list(c1.registeredUtilities())), 0)
        self.assertEqual(len(list(c2.registeredUtilities())), 0)

        c1.registerUtility(test_object1)
        self.assertEqual(len(list(c1.registeredUtilities())), 1)
        self.assertEqual(len(list(c2.registeredUtilities())), 0)
        self.assertEqual(c1.queryUtility(I1), test_object1)
        self.assertEqual(c2.queryUtility(I1), test_object1)

        c1.registerUtility(test_object2)
        self.assertEqual(len(list(c1.registeredUtilities())), 1)
        self.assertEqual(len(list(c2.registeredUtilities())), 0)
        self.assertEqual(c1.queryUtility(I1), test_object2)
        self.assertEqual(c2.queryUtility(I1), test_object2)


        c3 = Components('3', (c1, ))
        c4 = Components('4', (c2, c3))
        self.assertEqual(c4.queryUtility(I1), test_object2)
    
        c1.registerUtility(test_object3, I2)
        self.assertEqual(c4.queryUtility(I2), test_object3)

        c3.registerUtility(test_object4, I2)
        self.assertEqual(c4.queryUtility(I2), test_object4)

        @adapter(I1)
        def handle1(x):
            self.assertEqual(x, test_object1)

        def handle(*objects):
            self.assertEqual(objects, (test_object1,))

        @adapter(I1)
        def handle3(x):
            self.assertEqual(x, test_object1)

        @adapter(I1)
        def handle4(x):
            self.assertEqual(x, test_object1)

        c1.registerHandler(handle1, info=u'First handler')
        c2.registerHandler(handle, required=[U])
        c3.registerHandler(handle3)
        c4.registerHandler(handle4)

        c4.handle(test_object1)
开发者ID:ChimmyTee,项目名称:oh-mainline,代码行数:61,代码来源:test_registry.py

示例3: test_nested

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import registerUtility [as 别名]
 def test_nested(self):
     from zope.component import getGlobalSiteManager
     from zope.interface.interfaces import IComponentLookup
     from zope.interface.registry import Components
     gsm = getGlobalSiteManager()
     gutil = _makeMyUtility('global', gsm)
     gsm.registerUtility(gutil, IMyUtility, 'myutil')
     sm1 = Components('sm1', bases=(gsm, ))
     sm1_1 = Components('sm1_1', bases=(sm1, ))
     util1 = _makeMyUtility('one', sm1)
     sm1.registerUtility(util1, IMyUtility, 'myutil')
     self.assertTrue(IComponentLookup(util1) is sm1)
     self.assertTrue(self._callFUT(util1, IMyUtility, 'myutil') is gutil)
     util1_1 = _makeMyUtility('one-one', sm1_1)
     sm1_1.registerUtility(util1_1, IMyUtility, 'myutil')
     self.assertTrue(IComponentLookup(util1_1) is sm1_1)
     self.assertTrue(self._callFUT(util1_1, IMyUtility, 'myutil') is util1)
开发者ID:zopefoundation,项目名称:zope.component,代码行数:19,代码来源:test__api.py

示例4: PackageConfigurator

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import registerUtility [as 别名]
class PackageConfigurator(object):
    default_depth = 3

    def __init__(self, registry, resolve=None, target='includeme'):
        self.registry = registry
        self._resolve = resolve
        self._target = target
        self._depth = self.default_depth
        self._configurator_registry = Components()

    def add_configurator(self, configurator, name=''):
        self._configurator_registry.registerUtility(configurator, IConfigurator, name)

    def include(self, name, *args, **kwargs):
        _logger.debug('start include: name=%s, args=%s, kwds=%s', name, args, kwargs)
        includeme = name if callable(name) else get_includeme(name, resolve=self._resolve, depth=self._depth)
        _logger.debug('include -> %s.%s', includeme.__module__, includeme.__name__)
        return includeme(self)

    def scan(self, package=None, **kw):
        name = 'scan'
        if package is None:
            package = '.'

        if isinstance(package, (six.string_types, six.text_type, six.binary_type)):
            package = get_abs_dotted_name_caller_module(package, **kw)

        for cfg in self._configurator_registry.getAllUtilitiesRegisteredFor(IConfigurator):
            if hasattr(cfg, name) and callable(getattr(cfg, name)):
                _logger.debug('scanning...: %s: %s', cfg.__module__, package)
                cfg.scan(package)

    def __getattr__(self, name):
        if name.startswith('_'):
            raise AttributeError(name)

        configs = [cfg
                   for cfg in self._configurator_registry.getAllUtilitiesRegisteredFor(IConfigurator)
                   if hasattr(cfg, name) and callable(getattr(cfg, name))]

        if configs:
            for config in configs:
                return getattr(config, name)
        else:
            raise AttributeError(name)
开发者ID:TakesxiSximada,项目名称:configram,代码行数:47,代码来源:core.py

示例5: test_passwords

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import registerUtility [as 别名]
 def test_passwords(self):
     from zope.interface.registry import Components
     from alpaca.common.domain.user import User
     from alpaca.common.utilities.interfaces import IPasswordProcessor
     from alpaca.common.utilities.pbkdf2_password_processor import (
         PBKDF2PasswordProcessor
     )
     processor = PBKDF2PasswordProcessor()
     registry = Components()
     registry.registerUtility(
         processor,
         IPasswordProcessor,
         'pbkdf2'
     )
     user = User()
     user.set_password('łąki łan 123', 'pbkdf2', processor)
     self.assertEqual(user.password_processor, 'pbkdf2')
     self.assertTrue(bool(user.password_hash))
     self.assertLessEqual(len(user.password_hash), 100)
     self.assertFalse(user.password_equals('łąki łany 123', registry))
     self.assertTrue(user.password_equals('łąki łan 123', registry))
开发者ID:msiedlarek,项目名称:alpaca,代码行数:23,代码来源:user.py

示例6: main

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import registerUtility [as 别名]
def main(argv=sys.argv[1:]):
    reg = Components()
    reg.registerUtility(GoogleVisionAPIFaceDetection(),
                        IDetection, 'gcp')
    reg.registerUtility(MSProjectoxfordDetection(get_ms_param()['API_TOKEN']),
                        IDetection, 'ms')
    reg.registerUtility(AkamaiCrop(),
                        ICrop, 'akamai')

    parser = argparse.ArgumentParser()
    parser.add_argument('mode', help='gcp or ms')
    parser.add_argument('target', help='url or path')
    parser.add_argument('--crop', default='akamai')
    args = parser.parse_args(argv)

    detect = reg.queryUtility(IDetection, args.mode)
    result = detect(args.target)
    crop = AkamaiCrop()
    url = crop(result)
    print(url)
开发者ID:TakesxiSximada,项目名称:facedetection,代码行数:22,代码来源:__init__.py

示例7: TestUtility

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import registerUtility [as 别名]
class TestUtility(unittest.TestCase):

    def setUp(self):
        self.components = Components('comps')

    def test_register_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)

    def test_register_utility_with_factory(self):
        test_object = U1(1)
        def factory():
           return test_object
        self.components.registerUtility(factory=factory)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(factory=factory))

    def test_register_utility_with_component_and_factory(self):
        def factory():
            return U1(1)
        self.assertRaises(
            TypeError,
            self.components.registerUtility, U1(1), factory=factory)

    def test_unregister_utility_with_and_without_component_and_factory(self):
        def factory():
            return U1(1)
        self.assertRaises(
            TypeError,
            self.components.unregisterUtility, U1(1), factory=factory)
        self.assertRaises(TypeError, self.components.unregisterUtility)

    def test_register_utility_with_no_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, A)

    def test_register_utility_with_two_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, U12(1))

    def test_register_utility_with_arguments(self):
        test_object1 = U12(1)
        test_object2 = U12(2)
        self.components.registerUtility(test_object1, I2)
        self.components.registerUtility(test_object2, I2, 'name')
        self.assertEqual(self.components.getUtility(I2), test_object1)
        self.assertEqual(self.components.getUtility(I2, 'name'), test_object2)

    def test_get_none_existing_utility(self):
        from zope.interface.interfaces import ComponentLookupError
        self.assertRaises(ComponentLookupError, self.components.getUtility, I3)

    def test_query_none_existing_utility(self):
        self.assertTrue(self.components.queryUtility(I3) is None)
        self.assertEqual(self.components.queryUtility(I3, default=42), 42)

    def test_registered_utilities_and_sorting(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object3, I2, u'name')
        self.components.registerUtility(test_object2, I2)

        sorted_utilities = sorted(self.components.registeredUtilities())
        sorted_utilities_name = map(lambda x: getattr(x, 'name'),
                                    sorted_utilities)
        sorted_utilities_component = map(lambda x: getattr(x, 'component'),
                                         sorted_utilities)
        sorted_utilities_provided = map(lambda x: getattr(x, 'provided'),
                                        sorted_utilities)

        self.assertEqual(len(sorted_utilities), 3)
        self.assertEqual(sorted_utilities_name, [u'', u'', u'name'])
        self.assertEqual(
            sorted_utilities_component,
            [test_object1, test_object2, test_object3])
        self.assertEqual(sorted_utilities_provided, [I1, I2, I2])

    def test_duplicate_utility(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        test_object4 = U1(4)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')
        self.assertEqual(self.components.getUtility(I1), test_object1)

        self.components.registerUtility(test_object4, info=u'use 4 now')
        self.assertEqual(self.components.getUtility(I1), test_object4)

    def test_unregister_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(provided=I1))
        self.assertFalse(self.components.unregisterUtility(provided=I1))

    def test_unregister_utility_extended(self):
        test_object = U1(1)
#.........这里部分代码省略.........
开发者ID:ChimmyTee,项目名称:oh-mainline,代码行数:103,代码来源:test_registry.py


注:本文中的zope.interface.registry.Components.registerUtility方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。