def register_icon(self, config, cls, name, icons):
    """Register an icon.
    """
    if not icons:
        return

    base_path = os.path.dirname(sys.modules[cls.__module__].__file__)
    for key, icon_prefix in self.icon_prefix.items():
        icon_path = icons.get(key)
        if not icon_path:
            continue
        fs_path = os.path.join(base_path, icon_path)
        identifier = ''.join((
                icon_prefix,
                name.strip().replace(' ', '-'),
                os.path.splitext(icon_path)[1] or '.png'))

        factory = IconResourceFactory(name, fs_path)
        config.action(
            discriminator = ('resource', identifier, IHTTPRequest, Interface),
            callable = provideAdapter,
            args = (factory, (IHTTPRequest,), Interface, identifier))

        icon_name = "++resource++" + identifier
        icon_registry.register((self.icon_namespaces[key], name), icon_name)
Beispiel #2
0
def registerIcon(config, extension_name, cls, icon):
    """Register icon for a class.
    """
    if icon is None:
        return

    if not IIcon.providedBy(icon):
        extension = extensionRegistry.get_extension(extension_name)
        fs_path = os.path.join(extension.module_directory, icon)
        name = ''.join((
                'icon-',
                cls.meta_type.strip().replace(' ', '-'),
                os.path.splitext(icon)[1] or '.png'))

        factory = IconResourceFactory(name, fs_path)
        config.action(
            discriminator = ('resource', name, IHTTPRequest, Interface),
            callable = provideAdapter,
            args = (factory, (IHTTPRequest,), Interface, name))

        icon = Icon("++resource++" + name)

    icon_registry.register(('meta_type', cls.meta_type), icon)
    cls.icon = icon.icon
Beispiel #3
0
    def test_custom_icons(self):
        """Test registring icons with a custom namespace and looking
        them up.
        """
        registry.register(('test-silva', 'model'), '++custom++foo.png')
        registry.register(('test-silva', 'page'), '++custom++bar.jpeg')
        registry.register(('default', 'test-silva'), '++custom++empty.gif')

        resolver = queryAdapter(TestRequest(), IIconResolver)
        self.assertTrue(verifyObject(IIconResolver, resolver))

        icon = resolver.get_identifier(('test-silva', 'model'))
        self.assertTrue(verifyObject(IIcon, icon))
        self.assertEqual(str(icon), '++custom++foo.png')
        self.assertEqual(
            resolver.get_identifier_url(('test-silva', 'model')),
            'http://localhost/root/++custom++foo.png')

        icon = resolver.get_identifier(('test-silva', 'missing'))
        self.assertTrue(verifyObject(IIcon, icon))
        self.assertEqual(str(icon), '++static++/silva.icons/generic.png')
        self.assertEqual(
            resolver.get_identifier_url(('test-silva', 'missing')),
            'http://localhost/root/++static++/silva.icons/generic.png')

        icon = resolver.get_identifier(
            ('test-silva', 'missing'), default='test-silva')
        self.assertTrue(verifyObject(IIcon, icon))
        self.assertEqual(str(icon), '++custom++empty.gif')
        self.assertEqual(
            resolver.get_identifier_url(
                ('test-silva', 'missing'), default='test-silva'),
            'http://localhost/root/++custom++empty.gif')

        icon = resolver.get_identifier(
            ('test-silva', 'missing'), default=('test-silva', 'page'))
        self.assertTrue(verifyObject(IIcon, icon))
        self.assertEqual(str(icon), '++custom++bar.jpeg')
        self.assertEqual(
            resolver.get_identifier_url(
                ('test-silva', 'missing'), default=('test-silva', 'page')),
            'http://localhost/root/++custom++bar.jpeg')

        icon = resolver.get_identifier(
            ('test-silva', 'missing'), default=('test-silva', 'metoo'))
        self.assertIs(icon, None)
        self.assertIs(
            resolver.get_identifier_url(
                ('test-silva', 'missing'), default=('test-silva', 'metoo')),
            None)
Beispiel #4
0
    def test_custom_icons(self):
        """Test registring icons with a custom namespace and looking
        them up.
        """
        registry.register(('test-silva', 'model'), '++custom++foo.png')
        registry.register(('test-silva', 'page'), '++custom++bar.jpeg')
        registry.register(('default', 'test-silva'), '++custom++empty.gif')

        resolver = queryAdapter(TestRequest(), IIconResolver)
        self.assertTrue(verifyObject(IIconResolver, resolver))

        icon = resolver.get_identifier(('test-silva', 'model'))
        self.assertTrue(verifyObject(IIcon, icon))
        self.assertEqual(str(icon), '++custom++foo.png')
        self.assertEqual(resolver.get_identifier_url(('test-silva', 'model')),
                         'http://localhost/root/++custom++foo.png')

        icon = resolver.get_identifier(('test-silva', 'missing'))
        self.assertTrue(verifyObject(IIcon, icon))
        self.assertEqual(str(icon), '++static++/silva.icons/generic.png')
        self.assertEqual(
            resolver.get_identifier_url(('test-silva', 'missing')),
            'http://localhost/root/++static++/silva.icons/generic.png')

        icon = resolver.get_identifier(('test-silva', 'missing'),
                                       default='test-silva')
        self.assertTrue(verifyObject(IIcon, icon))
        self.assertEqual(str(icon), '++custom++empty.gif')
        self.assertEqual(
            resolver.get_identifier_url(('test-silva', 'missing'),
                                        default='test-silva'),
            'http://localhost/root/++custom++empty.gif')

        icon = resolver.get_identifier(('test-silva', 'missing'),
                                       default=('test-silva', 'page'))
        self.assertTrue(verifyObject(IIcon, icon))
        self.assertEqual(str(icon), '++custom++bar.jpeg')
        self.assertEqual(
            resolver.get_identifier_url(('test-silva', 'missing'),
                                        default=('test-silva', 'page')),
            'http://localhost/root/++custom++bar.jpeg')

        icon = resolver.get_identifier(('test-silva', 'missing'),
                                       default=('test-silva', 'metoo'))
        self.assertIs(icon, None)
        self.assertIs(
            resolver.get_identifier_url(('test-silva', 'missing'),
                                        default=('test-silva', 'metoo')), None)

class RegistryBlockGrokker(martian.ClassGrokker):
    martian.component(Block)
    martian.directive(grok.name, get_default=default_name)
    martian.directive(silvaconf.icon)

    icon_namespaces = {
        None: 'silva.core.contentlayout.blocks'}
    icon_prefix = {
        None: 'icon-blocks-'}

    def execute(self, factory, name, icon, config, **kw):
        if icon is not None:
            register_icon(self, config, factory, name, icon)
        block_registry.register_block(name, factory)
        return True


class AssociateBlockViewGrokker(TemplateGrokker):
    martian.component(BlockView)


# Register default icons
for category, icon in (
    (('default', 'silva.core.contentlayout.designs'), 'design.png'),
    (('default', 'silva.core.contentlayout.models'), 'model.png')):
    icon_registry.register(
        category,
        '++static++/silva.core.contentlayout/' + icon)