Esempio n. 1
0
    def test_split_template_path(self):
        assert split_template_path('foo/bar') == ['foo', 'bar']
        assert split_template_path('./foo/bar') == ['foo', 'bar']
        assert split_template_path('foo//bar') == ['foo', 'bar']
        assert split_template_path('foo/../bar') == ['bar']
        assert split_template_path('../foo') == ['..', 'foo']

        pytest.raises(TemplateNotFound, split_template_path, '/foo/bar')
Esempio n. 2
0
    def _load_template(self, name, globals):
        ctx = get_ctx()

        try:
            rv = jinja2.Environment._load_template(self, name, globals)
            if ctx is not None:
                filename = rv.filename
                if PY2 and is_windows:
                    try:
                        filename = filename.decode('utf-8')
                    except UnicodeDecodeError:
                        pass
                ctx.record_dependency(filename)
            return rv
        except jinja2.TemplateSyntaxError as e:
            if ctx is not None:
                ctx.record_dependency(e.filename)
            raise
        except jinja2.TemplateNotFound as e:
            if ctx is not None:
                # If we can't find the template we want to record at what
                # possible locations the template could exist.  This will help
                # out watcher to pick up templates that will appear in the
                # future.  This assumes the loader is a file system loader.
                for template_name in e.templates:
                    pieces = split_template_path(template_name)
                    for base in self.loader.searchpath:
                        ctx.record_dependency(os.path.join(base, *pieces))
            raise
Esempio n. 3
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        bp = '/'.join((self.package_path,) + tuple(pieces))

        languages = self.get_language()
        languages.append('')

        for lang in languages:
            if lang is None or lang == '':
                p = bp
            else:
                p = bp + '.' + lang

            if not self.provider.has_resource(p):
                continue

            filename = uptodate = None
            if self.filesystem_bound:
                filename = self.provider.get_resource_filename(self.manager, p)
                mtime = os.path.getmtime(filename)
                def uptodate():
                    return os.path.getmtime(filename) == mtime

            source = self.provider.get_resource_string(self.manager, p)
            return source.decode(self.encoding), filename, uptodate

        raise TemplateNotFound(template)
Esempio n. 4
0
 def resources_include_url(name):
     env = self.environment
     mime_type, encoding = mimetypes.guess_type(name)
     try:
         # we try to load via the jinja loader, but that tries to load
         # as (encoded) text
         data = env.loader.get_source(env, name)[0].encode('utf8')
     except UnicodeDecodeError:
         # if that fails (for instance a binary file, png or ttf)
         # we mimic jinja2
         pieces = split_template_path(name)
         for searchpath in self.template_paths:
             filename = os.path.join(searchpath, *pieces)
             print(filename, os.path.exists(filename))
             if os.path.exists(filename):
                 with open(filename, "rb") as f:
                     data = f.read()
                     break
         else:
             raise ValueError("No file %r found in %r" %
                              (name, searchpaths))
     data = base64.b64encode(data)
     data = data.replace(b'\n', b'').decode('ascii')
     src = 'data:{mime_type};base64,{data}'.format(mime_type=mime_type,
                                                   data=data)
     return jinja2.Markup(src)
    def get_source(self, environment, template):
        # uptodate is a callable that returns True if a file has not been
        # modified.
        #
        # If JINJA2_CACHE_MSTAT_DISABLED is True, uptodate always returns True.
        # Setting JINJA2_CACHE_MSTAT_DISABLED to True also causes template file
        # search results to be cached.
        uptodate = lambda: True

        if JINJA2_CACHE_MSTAT_DISABLED:
            if template in self.template_search_cache:
                return self.template_search_cache[template], uptodate

        pieces = loaders.split_template_path(template)
        for searchpath in self.searchpath:
            filename = os.path.join(searchpath, *pieces)
            f = open_if_exists(filename)
            if f is None:
                continue
            f.close()

            if JINJA2_CACHE_MSTAT_DISABLED:
                self.template_search_cache[template] = filename
            else:
                uptodate = self._get_uptodatefunc(filename)

            return filename, uptodate

        raise TemplateNotFound(template)
Esempio n. 6
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        for searchpath in self.searchpath:
            globbed_filename = os.path.join(searchpath, *pieces)
            filenames = glob.glob(globbed_filename)

            # Filter out files if they match any of the ignore patterns
            for ig in self.ignores:
                filenames = [ f for f in filenames 
                        if not fnmatch.fnmatch(os.path.basename(f), ig) ]

            if len(filenames) > 1:
                raise AmbiguousTemplate(template)
            elif len(filenames) < 1:
                continue
            filename = filenames[0]

            with open(filename) as f:
                contents = f.read().decode(self.encoding)

            mtime = os.path.getmtime(filename)
            def uptodate():
                try:
                    return os.path.getmtime(filename) == mtime
                except OSError:
                    return False
            return contents, filename, uptodate
        else:
            raise TemplateNotFound(template)
Esempio n. 7
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        for searchpath in self.searchpath:
            filename = os.path.join(searchpath, *pieces)
            f = open_if_exists(filename)
            if f is None:
                continue
            try:
                contents = f.read().decode(self.encoding)
            finally:
                f.close()

            mtime = os.path.getmtime(filename)
            # Need to save original raw template before compilation
            environment.sql_params.setdefault('raws', {}).update(
                {template: [c.strip() for c in contents.splitlines()]})

            def uptodate():
                try:
                    return os.path.getmtime(filename) == mtime
                except OSError:
                    return False

            return contents, filename, uptodate

        raise TemplateNotFound(template)
Esempio n. 8
0
 def get_source(self, environment, template):
     # if the template name starts with * then this should be
     # treated specially.
     # format is *<search path parent index>*<template name>
     # so we only search from then downwards.  This allows recursive
     # ckan_extends tags
     if template.startswith("*"):
         parts = template.split("*")
         template = parts[2]
         searchpaths = self.searchpath[int(parts[1]) + 1 :]
     else:
         searchpaths = self.searchpath
     # end of ckan changes
     pieces = loaders.split_template_path(template)
     for searchpath in searchpaths:
         filename = path.join(searchpath, *pieces)
         f = open_if_exists(filename)
         if f is None:
             continue
         try:
             contents = f.read().decode(self.encoding)
         except UnicodeDecodeError, e:
             log.critical("Template corruption in `%s` unicode decode errors" % filename)
             raise e
         finally:
Esempio n. 9
0
File: jinja.py Progetto: wummel/wok
    def get_source(self, environment, template):
        """Get template source."""
        pieces = split_template_path(template)
        for searchpath in self.searchpath:
            globbed_filename = os.path.join(searchpath, *pieces)
            filenames = glob.glob(globbed_filename)
            if len(filenames) > 1:
                raise AmbiguousTemplate(template)
            elif len(filenames) < 1:
                continue
            filename = filenames[0]

            with codecs.open(filename, 'r', self.encoding) as f:
                contents = f.read()

            mtime = os.path.getmtime(filename)
            def uptodate():
                """Check if file is uptodate."""
                try:
                    return os.path.getmtime(filename) == mtime
                except OSError:
                    return False
            return contents, filename, uptodate
        else:
            raise TemplateNotFound(template)
Esempio n. 10
0
 def get_source(self, environment, template):
     # if the template name starts with * then this should be
     # treated specially.
     # format is *<search path parent index>*<template name>
     # so we only search from then downwards.  This allows recursive
     # ckan_extends tags
     if template.startswith('*'):
         parts = template.split('*')
         template = parts[2]
         searchpaths = self.searchpath[int(parts[1]) + 1:]
     else:
         searchpaths = self.searchpath
     # end of ckan changes
     pieces = loaders.split_template_path(template)
     for searchpath in searchpaths:
         filename = path.join(searchpath, *pieces)
         f = open_if_exists(filename)
         if f is None:
             continue
         try:
             contents = f.read().decode(self.encoding)
         except UnicodeDecodeError, e:
             log.critical(
                 'Template corruption in `%s` unicode decode errors' %
                 filename)
             raise e
         finally:
Esempio n. 11
0
	def get_source(self, environment, template):
		if callable(self.path_filter):
			pieces = split_template_path(template)
			if not self._combined_filter(os.path.join(*pieces)):
				raise TemplateNotFound(template)

		return FileSystemLoader.get_source(self, environment, template)
Esempio n. 12
0
 def get_source(self, environment, template):
     pieces = split_template_path(template)
     for searchpath in self.searchpath:
         filename = os.path.join(searchpath, *pieces)
         
         if self.ignore_sources is not None:
             if filename in self.ignore_sources:
                 continue
             
         f = open_if_exists(filename)
         if f is None:
             continue
         try:
             contents = f.read().decode(self.encoding)
         finally:
             f.close()
 
         mtime = os.path.getmtime(filename)
 
         def uptodate():
             try:
                 return os.path.getmtime(filename) == mtime
             except OSError:
                 return False
         
         if self.ignore_sources is not None:
             self.ignore_sources.append(filename)
         return contents, filename, uptodate
     raise TemplateNotFound(template)
Esempio n. 13
0
    def get_source(self, environment, template):
        if callable(self.path_filter):
            pieces = split_template_path(template)
            if not self._combined_filter(os.path.join(*pieces)):
                raise TemplateNotFound(template)

        return FileSystemLoader.get_source(self, environment, template)
Esempio n. 14
0
    def _load_template(self, name, globals):
        ctx = get_ctx()

        try:
            rv = jinja2.Environment._load_template(self, name, globals)
            if ctx is not None:
                filename = rv.filename
                if PY2 and is_windows:
                    try:
                        filename = filename.decode('utf-8')
                    except UnicodeDecodeError:
                        pass
                ctx.record_dependency(filename)
            return rv
        except jinja2.TemplateSyntaxError as e:
            if ctx is not None:
                ctx.record_dependency(e.filename)
            raise
        except jinja2.TemplateNotFound as e:
            if ctx is not None:
                # If we can't find the template we want to record at what
                # possible locations the template could exist.  This will help
                # out watcher to pick up templates that will appear in the
                # future.  This assumes the loader is a file system loader.
                for template_name in e.templates:
                    pieces = split_template_path(template_name)
                    for base in self.loader.searchpath:
                        ctx.record_dependency(os.path.join(base, *pieces))
            raise
Esempio n. 15
0
    def get_source(self, environment, template):
        # if the template name starts with * then this should be
        # treated specially.
        # format is *<search path parent index>*<template name>
        # so we only search from then downwards.  This allows recursive
        # ckan_extends tags
        if template.startswith('*'):
            parts = template.split('*')
            template = parts[2]
            searchpaths = self.searchpath[int(parts[1]) + 1:]
        else:
            searchpaths = self.searchpath
        # end of ckan changes
        pieces = loaders.split_template_path(template)
        for searchpath in searchpaths:
            filename = path.join(searchpath, *pieces)
            f = open_if_exists(filename)
            if f is None:
                continue
            try:
                contents = f.read().decode(self.encoding)
            finally:
                f.close()

            mtime = path.getmtime(filename)

            def uptodate():
                try:
                    return path.getmtime(filename) == mtime
                except OSError:
                    return False
            return contents, filename, uptodate
        raise TemplateNotFound(template)
Esempio n. 16
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        for searchpath in self.searchpath:
            filename = os.path.join(searchpath, *pieces)
            f = open_if_exists(filename)
            if f is None:
                continue
            try:
                contents = f.read().decode(self.encoding)
            finally:
                f.close()

            mtime = os.path.getmtime(filename)
            # Need to save original raw template before compilation
            environment.sql_params.setdefault('raws', {}).update({
                template: [c.strip() for c in contents.splitlines()]
            })

            def uptodate():
                try:
                    return os.path.getmtime(filename) == mtime
                except OSError:
                    return False
            return contents, filename, uptodate

        raise TemplateNotFound(template)
Esempio n. 17
0
 def _get_matching_filenames(self, template):
     pieces = split_template_path(template)
     matching_filenames = []
     for searchpath in self.loader.searchpath:
         filename = os.path.join(searchpath, *pieces)
         if os.path.exists(filename):
             matching_filenames.append(filename)
     return matching_filenames
 def template_source(self, environment, template):
     if template != self.allowed_template:
         return ('={t}='.format(t=template), template, True)
     pieces = split_template_path(template)
     p = '/'.join((self.package_path, ) + tuple(pieces))
     filename = self.provider.get_resource_filename(self.manager, p)
     source = self.provider.get_resource_string(self.manager, p)
     return (source.decode('utf-8'), filename, True)
 def exists(self, template: str):
     """Check if a template exists."""
     pieces = split_template_path(template)
     for searchpath in self.env.loader.searchpath:
         filename = os.path.join(searchpath, *pieces)
         if os.path.exists(filename):
             return True
     return False
 def template_source(self, environment, template):
     if template != self.allowed_template:
         return ('={t}='.format(t=template), template, True)
     pieces = split_template_path(template)
     p = '/'.join((self.package_path,) + tuple(pieces))
     filename = self.provider.get_resource_filename(self.manager, p)
     source = self.provider.get_resource_string(self.manager, p)
     return (source.decode('utf-8'), filename, True)
Esempio n. 21
0
 def _get_matching_filenames(self, template):
     pieces = split_template_path(template)
     matching_filenames = []
     for searchpath in self.loader.searchpath:
         filename = os.path.join(searchpath, *pieces)
         if os.path.exists(filename):
             matching_filenames.append(filename)
     return matching_filenames
	def get_full_template_path(self, template):
		
		for path in self.search_paths:
			for pieces in self._get_filenames(split_template_path(template)):
				filename = os.path.join(path, *pieces)
				
				if os.path.exists(filename):
					ext = os.path.splitext(pieces[-1])[-1]
					return filename, ext
		
		raise TemplateNotFound(template)
Esempio n. 23
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        filename = posixpath.join(self.template_folder, *pieces)
        if filename in current_app.blohg.changectx.files:
            filectx = current_app.blohg.changectx.get_filectx(filename)

            def up2date():
                if current_app.blohg.changectx is None:
                    return False
                return not \
                       current_app.blohg.changectx.filectx_needs_reload(filectx)

            return filectx.content, \
                   os.path.join(self.template_folder, *pieces), up2date
        raise TemplateNotFound(template)
Esempio n. 24
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        filename = posixpath.join(self.template_folder, *pieces)
        if filename in current_app.blohg.changectx.files:
            filectx = current_app.blohg.changectx.get_filectx(filename)

            def up2date():
                if current_app.blohg.changectx is None:
                    return False
                return not \
                       current_app.blohg.changectx.filectx_needs_reload(filectx)

            return filectx.content, \
                   os.path.join(self.template_folder, *pieces), up2date
        raise TemplateNotFound(template)
Esempio n. 25
0
 def get_source(self, environment, template):
     path = posixpath.join(*split_template_path(template))
     if template[0] == '~':
         return self._get_fallback(environment, template[1:], path[1:], customization_ignored=True)
     try:
         plugin, path = path.split(':', 1)
     except ValueError:
         plugin = None
     prefix = posixpath.join('plugins', plugin) if plugin else 'core'
     path = posixpath.join(prefix, path)
     try:
         rv = self.fs_loader.get_source(environment, path)
         if self.debug:
             self.logger.debug('Customized: %s', path)
         return rv
     except TemplateNotFound:
         return self._get_fallback(environment, template, path)
Esempio n. 26
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        for searchpath in self._searchpath:
            filename = join(searchpath, *pieces)
            f = open_if_exists(filename)
            if f is None:
                continue
            try:
                contents = f.read().decode(self._encoding)
            finally:
                f.close()

            mtime_map = _new_mtime_map(self._searchpath)
            def uptodate():
                return mtime_map == _new_mtime_map(self._searchpath)
            return contents, filename, uptodate
        raise TemplateNotFound(template)
Esempio n. 27
0
 def get_source(self, environment, template):
     path = posixpath.join(*split_template_path(template))
     if template[0] == '~':
         return self._get_fallback(environment, template[1:], path[1:], customization_ignored=True)
     try:
         plugin, path = path.split(':', 1)
     except ValueError:
         plugin = None
     prefix = posixpath.join('plugins', plugin) if plugin else 'core'
     path = posixpath.join(prefix, path)
     try:
         rv = self.fs_loader.get_source(environment, path)
         if self.debug:
             self.logger.debug('Customized: %s', path)
         return rv
     except TemplateNotFound:
         return self._get_fallback(environment, template, path)
Esempio n. 28
0
    def get_source(self, environment, template):
        # checks for relative '..' paths
        template = path.join(*split_template_path(template))
        self.check_cache(template)
        filepath = path.join(self.searchpath, template)
        with open(filepath, 'rb') as f:
            try:
                contents = f.read().decode(self.encoding)
            except IOError:
                raise TemplateNotFound(template)
        mtime = path.getmtime(filepath)

        def uptodate():
            try:
                return path.getmtime(filepath) == mtime
            except OSError:
                return False
        return contents, filepath, uptodate
Esempio n. 29
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        for searchpath in self._searchpath:
            filename = join(searchpath, *pieces)
            f = open_if_exists(filename)
            if f is None:
                continue
            try:
                contents = f.read().decode(self._encoding)
            finally:
                f.close()

            mtime_map = _new_mtime_map(self._searchpath)

            def uptodate():
                return mtime_map == _new_mtime_map(self._searchpath)

            return contents, filename, uptodate
        raise TemplateNotFound(template)
Esempio n. 30
0
File: jinja.py Progetto: nkhuyu/salt
    def get_source(self, environment, template):
        # checks for relative '..' paths
        template = path.join(*split_template_path(template))
        self.check_cache(template)
        filepath = path.join(self.searchpath, template)
        with open(filepath, 'rb') as f:
            try:
                contents = f.read().decode(self.encoding)
            except IOError:
                raise TemplateNotFound(template)
        mtime = path.getmtime(filepath)

        def uptodate():
            try:
                return path.getmtime(filepath) == mtime
            except OSError:
                return False

        return contents, filepath, uptodate
Esempio n. 31
0
    def get_source(self, environment, template):
        searchpaths = self.searchpath[:]
        use_theme_template = False
        pieces = split_template_path(template)
        if current_user.is_authenticated:
            if current_user.theme and current_user.theme.directory_name:
                theme_pieces = pieces[:]
                theme_pieces[
                    -1] = current_user.theme.directory_name + "-" + theme_pieces[
                        -1]
                theme_path = path.join(app.config["DEFAULT_TEMPLATE_DIR"],
                                       "themes",
                                       current_user.theme.directory_name,
                                       *theme_pieces)
                if path.exists(theme_path):
                    use_theme_template = True

        for searchpath in searchpaths:
            if use_theme_template:
                filename = theme_path
            else:
                filename = path.join(searchpath, *pieces)
            f = open_if_exists(filename)

            if f is None:
                continue
            try:
                contents = f.read().decode(self.encoding)
            finally:
                f.close()

            mtime = path.getmtime(filename)

            def uptodate():
                try:
                    return path.getmtime(filename) == mtime
                except OSError:
                    return False

            return contents, filename, uptodate
        raise TemplateNotFound(template)
Esempio n. 32
0
    def get_source(self, environment, template):
        pieces = jinja_loaders.split_template_path(template)
        skin = askbot_settings.ASKBOT_DEFAULT_SKIN
        skin_path = utils.get_path_to_skin(skin)
        filename = os.path.join(skin_path, 'templates', *pieces)
        print 'want file %s' % filename
        f = open_if_exists(filename)
        if f is None:
            raise TemplateNotFound(template)
        try:
            contents = f.read().decode('utf-8')
        finally:
            f.close()

        mtime = os.path.getmtime(filename)
        def uptodate():
            try:
                return os.path.getmtime(filename) == mtime
            except OSError:
                return False
        return contents, filename, uptodate
Esempio n. 33
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        searchpath = getcwd()
        filename = path.join(searchpath, *pieces)
        f = open_if_exists(filename)
        if f is None:
            raise TemplateNotFound(template)
        try:
            contents = f.read().decode('utf-8')
        finally:
            f.close()

        mtime = path.getmtime(filename)

        def uptodate():
            try:
                return path.getmtime(filename) == mtime
            except OSError:
                return False

        return contents, filename, uptodate
Esempio n. 34
0
    def get_source(self, environment, template):
        # if the template name starts with * then this should be
        # treated specially.
        # format is *<search path parent directory>*<template name>
        # so we only search from then downwards.  This allows recursive
        # ckan_extends tags
        if template.startswith('*'):
            parts = template.split('*')
            template = parts[2]
            index = self.searchpath.index(parts[1])
            searchpaths = self.searchpath[index + 1:]
        else:
            searchpaths = self.searchpath
        # end of ckan changes
        pieces = loaders.split_template_path(template)
        for searchpath in searchpaths:
            filename = path.join(searchpath, *pieces)
            f = open_if_exists(filename)
            if f is None:
                continue
            try:
                contents = f.read().decode(self.encoding)
            except UnicodeDecodeError as e:
                log.critical(
                    'Template corruption in `%s` unicode decode errors' %
                    filename)
                raise e
            finally:
                f.close()

            mtime = path.getmtime(filename)

            def uptodate():
                try:
                    return path.getmtime(filename) == mtime
                except OSError:
                    return False

            return contents, filename, uptodate
        raise TemplateNotFound(template)
Esempio n. 35
0
    def get_source(self, environment, template):
        pieces = jinja_loaders.split_template_path(template)
        skin = askbot_settings.ASKBOT_DEFAULT_SKIN
        skin_path = utils.get_path_to_skin(skin)
        filename = os.path.join(skin_path, 'templates', *pieces)
        print 'want file %s' % filename
        f = open_if_exists(filename)
        if f is None:
            raise TemplateNotFound(template)
        try:
            contents = f.read().decode('utf-8')
        finally:
            f.close()

        mtime = os.path.getmtime(filename)

        def uptodate():
            try:
                return os.path.getmtime(filename) == mtime
            except OSError:
                return False

        return contents, filename, uptodate
Esempio n. 36
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        for searchpath in self.searchpath:
            globbed_filename = os.path.join(searchpath, *pieces)
            filenames = glob.glob(globbed_filename)
            if len(filenames) > 1:
                raise AmbiguousTemplate(template)
            elif len(filenames) < 1:
                continue
            filename = filenames[0]

            with open(filename) as f:
                contents = f.read().decode(self.encoding)

            mtime = os.path.getmtime(filename)
            def uptodate():
                try:
                    return os.path.getmtime(filename) == mtime
                except OSError:
                    return False
            return contents, filename, uptodate
        else:
            raise TemplateNotFound(template)
Esempio n. 37
0
 def resources_include_url(name):
     env = self.environment
     mime_type, encoding = mimetypes.guess_type(name)
     try:
         # we try to load via the jinja loader, but that tries to load
         # as (encoded) text
         data = env.loader.get_source(env, name)[0].encode("utf8")
     except UnicodeDecodeError:
         # if that fails (for instance a binary file, png or ttf)
         # we mimic jinja2
         pieces = split_template_path(name)
         for searchpath in self.template_paths:
             filename = os.path.join(searchpath, *pieces)
             if os.path.exists(filename):
                 with open(filename, "rb") as f:
                     data = f.read()
                     break
         else:
             raise ValueError(
                 f"No file {name!r} found in {searchpath!r}")
     data = base64.b64encode(data)
     data = data.replace(b"\n", b"").decode("ascii")
     src = f"data:{mime_type};base64,{data}"
     return markupsafe.Markup(src)
Esempio n. 38
0
 def test_split_template_path(self):
     assert split_template_path('foo/bar') == ['foo', 'bar']
     assert split_template_path('./foo/bar') == ['foo', 'bar']
     self.assert_raises(TemplateNotFound, split_template_path, '../foo')
Esempio n. 39
0
def test_package_zip_source(package_zip_loader, template, expect):
    source, name, up_to_date = package_zip_loader.get_source(None, template)
    assert source.rstrip() == expect
    assert name.endswith(os.path.join(*split_template_path(template)))
    assert up_to_date is None
Esempio n. 40
0
 def test_split_template_path(self):
     assert split_template_path("foo/bar") == ["foo", "bar"]
     assert split_template_path("./foo/bar") == ["foo", "bar"]
     pytest.raises(TemplateNotFound, split_template_path, "../foo")