Ejemplo n.º 1
0
    def test_django_loader_replace(self):
        from coffin.template.loaders import jinja_loader_from_django_loader
        from jinja2 import loaders

        # Test replacement of filesystem loader
        l = jinja_loader_from_django_loader('django.template.loaders.filesystem.Loader')
        assert isinstance(l, loaders.FileSystemLoader)

        # Since we don't do exact matches for the loader string, make sure we
        # are not replacing loaders that are outside the Django namespace.
        l = jinja_loader_from_django_loader('djangoaddon.template.loaders.filesystem.Loader')
        assert not isinstance(l, loaders.FileSystemLoader)
Ejemplo n.º 2
0
    def test_django_loader_replace(self):
        from coffin.template.loaders import jinja_loader_from_django_loader
        from jinja2 import loaders

        # Test replacement of filesystem loader
        l = jinja_loader_from_django_loader(
            'django.template.loaders.filesystem.Loader')
        assert isinstance(l, loaders.FileSystemLoader)

        # Since we don't do exact matches for the loader string, make sure we
        # are not replacing loaders that are outside the Django namespace.
        l = jinja_loader_from_django_loader(
            'djangoaddon.template.loaders.filesystem.Loader')
        assert not isinstance(l, loaders.FileSystemLoader)
Ejemplo n.º 3
0
    def _get_loaders(self):
        """Tries to translate each template loader given in the Django settings
        (:mod:`django.settings`) to a similarly-behaving Jinja loader.
        Warns if a similar loader cannot be found.
        Allows for Jinja2 loader instances to be placed in the template loader
        settings.
        """
        loaders = []

        from coffin.template.loaders import jinja_loader_from_django_loader
        from jinja2.loaders import BaseLoader as JinjaLoader

        from django.conf import settings

        _loaders = getattr(settings, "JINJA2_TEMPLATE_LOADERS", settings.TEMPLATE_LOADERS)
        for loader in _loaders:
            if isinstance(loader, JinjaLoader):
                loaders.append(loader)
            else:
                loader_name = args = None
                if isinstance(loader, basestring):
                    loader_name = loader
                    args = []
                elif isinstance(loader, (tuple, list)):
                    loader_name = loader[0]
                    args = loader[1]

                if loader_name:
                    loader_obj = jinja_loader_from_django_loader(loader_name, args)
                    if loader_obj:
                        loaders.append(loader_obj)
                        continue

                warnings.warn("Cannot translate loader: %s" % loader)
        return loaders
Ejemplo n.º 4
0
    def _get_loaders(self):
        """Tries to translate each template loader given in the Django settings
        (:mod:`django.settings`) to a similarly-behaving Jinja loader.
        Warns if a similar loader cannot be found.
        Allows for Jinja2 loader instances to be placed in the template loader
        settings.
        """
        loaders = []

        from coffin.template.loaders import jinja_loader_from_django_loader
        from jinja2.loaders import BaseLoader as JinjaLoader

        from django.conf import settings
        _loaders = getattr(settings, 'JINJA2_TEMPLATE_LOADERS', settings.TEMPLATE_LOADERS)
        for loader in _loaders:
            if isinstance(loader, JinjaLoader):
                loaders.append(loader)
            else:
                loader_name = args = None
                if isinstance(loader, basestring):
                    loader_name = loader
                    args = []
                elif isinstance(loader, (tuple, list)):
                    loader_name = loader[0]
                    args = loader[1]

                if loader_name:
                    loader_obj = jinja_loader_from_django_loader(loader_name, args)
                    if loader_obj:
                        loaders.append(loader_obj)
                        continue

                warnings.warn('Cannot translate loader: %s' % loader)
        return loaders
Ejemplo n.º 5
0
def _get_loaders():
    """Tries to translate each template loader given in the Django settings
    (:mod:`django.settings`) to a similarly-behaving Jinja loader.
    Warns if a similar loader cannot be found.
    Allows for Jinja2 loader instances to be placed in the template loader
    settings.
    """
    from coffin.template.loaders import jinja_loader_from_django_loader
    if _LOADERS:
        return _LOADERS
    from django.conf import settings
    for loader in settings.TEMPLATE_LOADERS:
        if isinstance(loader, basestring):
            loader_obj = jinja_loader_from_django_loader(loader)
            if loader_obj:
                _LOADERS.append(loader_obj)
            else:
                warnings.warn('Cannot translate loader: %s' % loader)
        else: # It's assumed to be a Jinja2 loader instance.
            _LOADERS.append(loader)
    return _LOADERS
Ejemplo n.º 6
0
def _get_loaders():
    """Tries to translate each template loader given in the Django settings
    (:mod:`django.settings`) to a similarly-behaving Jinja loader.
    Warns if a similar loader cannot be found.
    Allows for Jinja2 loader instances to be placed in the template loader
    settings.
    """
    from coffin.template.loaders import jinja_loader_from_django_loader
    if _LOADERS:
        return _LOADERS
    from django.conf import settings
    for loader in settings.TEMPLATE_LOADERS:
        if isinstance(loader, basestring):
            loader_obj = jinja_loader_from_django_loader(loader)
            if loader_obj:
                _LOADERS.append(loader_obj)
            else:
                warnings.warn('Cannot translate loader: %s' % loader)
        else:  # It's assumed to be a Jinja2 loader instance.
            _LOADERS.append(loader)
    return _LOADERS