Ejemplo n.º 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:
                pname, path = resolve_asset_spec(uri)
                srcfile = abspath_from_asset_spec(path, pname)
                if os.path.isfile(srcfile):
                    return self._load(srcfile, adjusted)
                raise exceptions.TopLevelLookupException(
                    "Can not locate template for uri %r" % uri)
        return TemplateLookup.get_template(self, uri)
Ejemplo n.º 2
0
    def get_template(self, uri):
        """Return a :class:`.Template` object corresponding to the given
        ``uri``.

        .. note:: The ``relativeto`` argument is not supported here at
           the moment.

        """

        try:
            if self.filesystem_checks:
                return self._check(uri, self._collection[uri])
            else:
                return self._collection[uri]
        except KeyError as e:
            u = re.sub(r"^\/+", "", uri)
            for dir_ in self.directories:
                # make sure the path seperators are posix - os.altsep is empty
                # on POSIX and cannot be used.
                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 exceptions.TopLevelLookupException(
                    "Can't locate template for uri %r" % uri
                ) from e
Ejemplo n.º 3
0
    def get_template(self, uri, module=None):
        """Return a :class:`.Template` object corresponding to the given
        URL.

        Note the "relativeto" argument is not supported here at the moment.

        """

        try:
            if self.filesystem_checks:
                return self._check(uri, self._collection[uri])
            else:
                return self._collection[uri]
        except KeyError:

            if uri[0] == "/" and os.path.isfile(uri):
                return self._load(uri, uri)

            # Case 1: Used with Template.forModule
            if module != None and hasattr(module, "_dir"):
                srcfile = posixpath.normpath(posixpath.join(module._dir, uri))
                if os.path.isfile(srcfile):
                    return self._load(srcfile, srcfile)

            # Case 2: We look through the dirs in the TemplateLookup
            u = re.sub(r'^\/+', '', uri)
            for dir in self.directories:
                srcfile = posixpath.normpath(posixpath.join(dir, u))
                if os.path.isfile(srcfile):
                    return self._load(srcfile, uri)
            else:
                # We did not find anything, so we raise an exception
                raise exceptions.TopLevelLookupException(
                    'Can\'t locate template for uri {0!r}'.format(uri))
Ejemplo n.º 4
0
    def get_template(self, uri):
        """This is stolen and truncated from mako.lookup:TemplateLookup."""

        u = re.sub(r'^/+', '', uri)
        for dir in self.directories:
            filename = posixpath.normpath(posixpath.join(dir, u))
            if os.path.isfile(filename):
                return self._load(filename, uri)
        else:
            raise exceptions.TopLevelLookupException(
                "Cant locate template for uri %r" % uri)
Ejemplo n.º 5
0
 def get_template(self, uri):
     try:
         if self.filesystem_checks:
             return self.__check(uri, self.__collection[uri])
         else:
             return self.__collection[uri]
     except KeyError:
         u = re.sub(r'^\/+', '', uri)
         for dir in self.directories:
             srcfile = posixpath.normpath(posixpath.join(dir, u))
             if os.path.exists(srcfile):
                 return self.__load(srcfile, uri)
         else:
             raise exceptions.TopLevelLookupException("Cant locate template for uri '%s'" % uri)
Ejemplo n.º 6
0
    def get_template(self, uri, module=None):
        """Return a :class:`.Template` object corresponding to the given
        URL.

        Note the "relativeto" argument is not supported here at the moment.

        """

        try:
            if self.filesystem_checks:
                return self._check(uri, self._collection[uri])
            else:
                return self._collection[uri]
        except KeyError:

            if uri[0] == "/" and os.path.isfile(uri):
                return self._load(uri, uri)

            # Case 1: Used with Template.forModule
            if module != None and hasattr(module, "_dir"):
                srcfile = posixpath.normpath(posixpath.join(module._dir, uri))
                if os.path.isfile(srcfile):
                    return self._load(srcfile, srcfile)

            # Case 2: We look through the dirs in the TemplateLookup
            u = re.sub(r'^\/+', '', uri)
            for dir in self.directories:
                srcfile = posixpath.normpath(posixpath.join(dir, u))
                if os.path.isfile(srcfile):
                    return self._load(srcfile, uri)
            else:
                #Case 3: we look into the plugins
                uri_split = u.split("/")
                if len(uri_split) == 2:
                    srcfile = self.getPluginTPlDir(uri_split[0], None,
                                                   uri_split[1])
                    if os.path.isfile(srcfile):
                        return self._load(srcfile, uri)
                if len(uri_split) == 3:
                    srcfile = self.getPluginTPlDir(*uri_split)
                    if os.path.isfile(srcfile):
                        return self._load(srcfile, uri)

                # We do not find anything, so we raise the Exception
                raise exceptions.TopLevelLookupException(
                    "Cant locate template for uri %r" % uri)
Ejemplo n.º 7
0
 def get_template(self, uri):
     try:
         if self.filesystem_checks:
             return self._check(uri, self._collection[uri])
         else:
             return self._collection[uri]
     except KeyError:
         ## WHAT IS THIS!!!!!!!!
         is_abs_uri = os.path.isabs(uri)
         u = self.FISRT_IS_SLASH_RX.sub('', uri)
         for dir in self.directories:
             if is_abs_uri:
                 srcfile = uri
             else:
                 srcfile = posixpath.normpath(posixpath.join(dir, u))
             if os.path.isfile(srcfile):
                 return self._load(srcfile, uri)
         else:
             raise mako_exceptions.TopLevelLookupException(
                 "Cant locate template for uri %r" % uri)
Ejemplo n.º 8
0
    def get_template(self, uri):
        """Return a :class:`.Template` object corresponding to the given 
        URL.
 
        Note the "relativeto" argument is not supported here at the moment.
 
        """
 
        try:
            if self.filesystem_checks:
                return self._check(uri, self._collection[uri])
            else:
                return self._collection[uri]
        except KeyError:
            u = re.sub(r'^\/+', '', uri)
            for dir in self.directories:
                srcfile = posixpath.normpath(posixpath.join(dir, u))
                if os.path.isfile(srcfile):
                    return self._load(srcfile, uri)
            else:
                raise exceptions.TopLevelLookupException(
                                    "Cant locate template for uri %r" % uri)
Ejemplo n.º 9
0
    def get_template(self, uri):
        """Return a :class:`.Template` object corresponding to the given
        URL.

        Note the "relativeto" argument is not supported here at the moment.
        """
        try:
            if self.filesystem_checks:
                return self._check(uri, self._collection[uri])
            else:
                return self._collection[uri]
        except KeyError:
            if uri[0] == "/" and os.path.isfile(uri):
                return self._load(uri, uri)

            u = re.sub(r'^/+', '', uri)
            for dir_ in self.directories:
                srcfile = posixpath.normpath(posixpath.join(dir_, u))
                if os.path.isfile(srcfile):
                    return self._load(srcfile, uri)
            else:
                # We did not find anything, so we raise an exception
                raise exceptions.TopLevelLookupException('Can\'t locate template for uri {0!r}'.format(uri))
Ejemplo n.º 10
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):
         try:
             if self.filesystem_checks:
                 return self._check(uri, self._collection[uri])
             else:
                 return self._collection[uri]
         except KeyError:
             pname, path = resolve_asset_spec(uri)
             srcfile = abspath_from_asset_spec(path, pname)
             if os.path.isfile(srcfile):
                 return self._load(srcfile, uri)
             raise exceptions.TopLevelLookupException(
                 "Can not locate template for uri %r" % uri)
     return TemplateLookup.get_template(self, uri)
Ejemplo n.º 11
0
    def get_template(self, uri):
        """
        Override Mako's template lookup routine to add support for templates located
        by an absolute path rather than a relative one.
        Absolute path names must begin with an equals sign ('=') to denote
        that they are absolute relative to the filesystem root, rather than the root
        of one of the TemplateLookup directories

        Also allows templates to be defined in apps that contain a templates folder
        Priority is given to app defined templates so that they may override system 
        defined ones.

        If the requested uri has an appname followed by a colon (':') then followed
        by a template path, this will attempt to find the given template within the
        specified application.
        e.g. uri = '/some_application:/event_renderers/_some_renderer.html"
            will attempt to return _some_renderer.html from
            /etc/apps/some_application/appserver/event_renderers
        """
        try:
            if self.filesystem_checks:
                return self._check(uri, self._collection[uri])
            else:
                return self._collection[uri]
        except KeyError:
            if uri[0] == '=' and os.path.exists(uri[1:]):
                    return self._load(uri[1:], self._find_i18n_template(uri))
            else:
                u = re.sub(r'^\/+', '', uri)
                # see if an app has a custom template
                appflag = "APP/"
                if u.startswith(appflag):
                    basedir = os.path.abspath(util.get_apps_dir())
                    srcfile = self._normalize_template_path(os.path.join(basedir, u[len(appflag):]))
                    if os.path.exists(srcfile) and os.path.abspath(srcfile).startswith(basedir):
                        return self._load(srcfile, uri)
                else:
                    # see if it's been defined in a specific app. we only do this if we find a ':' in the requested path
                    if u.find(self.TEMPLATE_APPSCOPE_SEPARATOR) > -1:
                        appScope = u.split(self.TEMPLATE_APPSCOPE_SEPARATOR)[0]
                        if appScope in [k for k,v in local_apps.items()]:
                            app = local_apps.apps[appScope]
                            appPath = re.sub(r'^\/+', '', u[len(appScope)+1:] )
                            basedir = os.path.abspath(posixpath.join(app['full_path'], 'appserver'))
                            srcfile = self._normalize_template_path(posixpath.join(basedir, appPath ) )
                            if os.path.exists(srcfile) and os.path.abspath(srcfile).startswith(basedir):
                                return self._load(srcfile, uri)

                    # now see if an app has defined this template for a module
                    for appname, app in local_apps.items():
                        basedir = os.path.abspath(posixpath.join(app['full_path'], 'appserver', 'modules'))
                        srcfile = self._normalize_template_path(posixpath.join(basedir, u))
                        if os.path.exists(srcfile) and os.path.abspath(srcfile).startswith(basedir):
                            return self._load(srcfile, uri) 


                    # finally, look in the directories we were given at setup
                    for dir in self.directories:
                        srcfile = posixpath.normpath(posixpath.join(dir, u)) 
                        srcfile = self._find_i18n_template(srcfile)
                        if os.path.exists(srcfile) and os.path.abspath(srcfile).startswith(dir):
                            return self._load(srcfile, uri)
                raise exceptions.TopLevelLookupException(_("Splunk has failed to locate the template for uri '%s'." % uri))