Example #1
0
    def get_template(self, uri):
        """Fetch a template from the cache, or check the filesystem
        for it

        In addition to the basic filesystem lookup, this subclass will
        use pkg_resource to load a file using the asset
        specification syntax.

        """
        isabs = os.path.isabs(uri)
        if (not isabs) and (':' in uri):
            # Windows can't cope with colons in filenames, so we replace the
            # colon with a dollar sign in the filename mako uses to actually
            # store the generated python code in the mako module_directory or
            # in the temporary location of mako's modules
            adjusted = uri.replace(':', '$')
            try:
                if self.filesystem_checks:
                    return self._check(adjusted, self._collection[adjusted])
                else:
                    return self._collection[adjusted]
            except KeyError:
                asset = AssetResolver().resolve(uri)
                if asset.exists():
                    srcfile = asset.abspath()
                    return self._load(srcfile, adjusted)
                raise TopLevelLookupException(
                    "Can not locate template for uri %r" % uri)
        try:
            return TemplateLookup.get_template(self, uri)
        except TemplateLookupException:
            if isabs:
                return self._load(uri, uri)
            else:
                raise
Example #2
0
    def get_template(self, uri):
        """
        Fetch a template from this lookup.

        If uri containers the ``super_delimiter``, expect it to join a 2-tuple
        of ``(uri, dir)``. The ``dir`` component refers to the specific
        directory within the lookup that the template should be fetched from.
        """
        try:
            if self.filesystem_checks:
                return self._check(uri, self._collection[uri])
            else:
                return self._collection[uri]

        except KeyError:

            if self.super_delimiter in uri:
                file_path, from_dir = uri.split(self.super_delimiter)
                u = re.sub(r'^\/+', '', file_path)
                srcfile = posixpath.normpath(posixpath.join(from_dir, u))
                return self._load(srcfile, uri)

            else:
                dir, srcfile = self.find_dir_for_uri(uri)
                if not dir:
                    raise TopLevelLookupException(
                        "Cant locate template for uri %r" % uri)
                return self._load(srcfile, uri)
Example #3
0
def get_template_name(environ):
    """
    Extract the template name from the environment
    """
    try:
        template = environ['selector.vars']['template']  # the template
    except KeyError:
        try:
            # try and get the template as the first path entry
            template = environ.get('PATH_INFO', '').split('/')[1]
        except (KeyError, IndexError):
            from mako.exceptions import TopLevelLookupException
            raise TopLevelLookupException('No template is specified')

    return template
Example #4
0
    def get_template(self, uri):
        try:
            return super(AppTemplateLookup, self).get_template(uri)
        except TopLevelLookupException:
            if not hasattr(self, "app_dirs"):
                from django.template.utils import get_app_template_dirs
                setattr(self, "app_dirs", get_app_template_dirs("templates"))

            u = re.sub(r'^\/+', '', uri)
            for dir in self.app_dirs:
                dir = dir.replace(os.path.sep, posixpath.sep)
                srcfile = posixpath.normpath(posixpath.join(dir, u))
                if os.path.isfile(srcfile):
                    return self._load(srcfile, uri)
            else:
                raise TopLevelLookupException("Cant locate template for uri %r" % uri)