Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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:
Ejemplo n.º 4
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)
	def get_source(self, environment, template):
		
		filename, ext = self.get_full_template_path(template)
		fp = open_if_exists(filename)
		if fp is None:
			raise TemplateNotFound(template)
		try:
			#print("Loading: {}".format(filename))
			contents = fp.read().decode('utf-8')
			
			if hamlpy and ext[1:] in hamlpy.VALID_EXTENSIONS:
				haml_parser = hamlpy.Compiler()
				contents = haml_parser.process(contents)
		except UnicodeDecodeError:
			raise TemplateNotFound(template)
		
		finally:
			fp.close()
		
		mtime = os.path.getmtime(filename)
		def uptodate():
			try:
				return os.path.getmtime(filename) == mtime
			except OSError:
				return False
		return contents, filename, uptodate
Ejemplo n.º 6
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:
Ejemplo 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
            if filename.endswith('.gpg'):
                f.close()
                _, contents = decrypt(filename, self.encoding)
            else:
                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
            return contents, filename, uptodate
        raise TemplateNotFound(template)
Ejemplo n.º 8
0
    def get_source(self, environment, template):
        """
        Overridden to also accept absolute paths.
        """
        if not os.path.isabs(template):
            return super(AbsolutePathLoader,
                         self).get_source(environment, template)

        # Security check, ensure the abs path is part of a valid search path
        if not any(template.startswith(s) for s in self.searchpath):
            raise TemplateNotFound(template)

        f = open_if_exists(template)
        if f is None:
            raise TemplateNotFound(template)
        try:
            contents = f.read().decode(self.encoding)
        finally:
            f.close()

        mtime = os.path.getmtime(template)

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

        return contents, template, uptodate
Ejemplo n.º 9
0
    def get_source(self, environment, template):
        pieces = split_template_path(template)
        searchpaths = self.searchpath
        if not self.strict and path.isabs(template):
            searchpaths = [
                template,
            ]
            pieces = tuple()
        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)
Ejemplo 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)
            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)
Ejemplo n.º 11
0
    def get_source(self, environment, template):
        """
        Overridden to also accept absolute paths.
        """
        if not os.path.isabs(template):
            return super(AbsolutePathLoader, self).get_source(environment, template)

        # Security check, ensure the abs path is part of a valid search path
        if not any(template.startswith(s) for s in self.searchpath):
            raise TemplateNotFound(template)

        f = open_if_exists(template)
        if f is None:
            raise TemplateNotFound(template)
        try:
            contents = f.read().decode(self.encoding)
        finally:
            f.close()

        mtime = os.path.getmtime(template)

        def uptodate():
            try:
                return os.path.getmtime(template) == mtime
            except OSError:
                return False
        return contents, template, uptodate
Ejemplo 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)
            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)
Ejemplo n.º 13
0
 def load_bytecode(self, bucket):
     f = open_if_exists(self._get_cache_filename(bucket), 'rb')
     if f is not None:
         try:
             bucket.load_bytecode(f)
         finally:
             f.close()
Ejemplo n.º 14
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)
Ejemplo n.º 15
0
 def load_bytecode(self, bucket):
     f = open_if_exists(self._get_cache_filename(bucket), 'rb')
     if f is not None:
         try:
             bucket.load_bytecode(f)
         finally:
             f.close()
Ejemplo n.º 16
0
 def _get_filename_contents(self, filename):
     f = open_if_exists(filename)
     if f is not None:
         try:
             contents = f.read().decode(self.encoding)
         finally:
             f.close()
         return contents
     else:
         raise Exception("template file %s not found" % (filename))
Ejemplo n.º 17
0
    def _delay_init(self):
        if '_mtime' in self.__dict__:
            return

        f = open_if_exists(self.filename)
        if f is None:
            raise TemplateNotFound(self.filename)
        self._mtime = os.path.getmtime(self.filename)
        try:
            self._contents = f.read().decode(self.encoding)
        finally:
            f.close()
Ejemplo n.º 18
0
    def _delay_init(self):
        if '_mtime' in self.__dict__:
            return

        f = open_if_exists(self.filename)
        if f is None:
            raise TemplateNotFound(self.filename)
        self._mtime = os.path.getmtime(self.filename)
        try:
            self._contents = f.read().decode(self.encoding)
        finally:
            f.close()
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
    def get_source(self, environment: Environment, template: str) -> Tuple[str, str, Callable]:
        for searchpath in self.searchpath:
            filename = path.join(searchpath, template)
            f = open_if_exists(filename)
            if f is None:
                continue
            with f:
                contents = f.read().decode(self.encoding)

            mtime = path.getmtime(filename)

            def uptodate() -> bool:
                try:
                    return path.getmtime(filename) == mtime
                except OSError:
                    return False
            return contents, filename, uptodate
        raise TemplateNotFound(template)
Ejemplo n.º 21
0
    def get_source(self, environment, template):
        for searchpath in self.searchpath:
            filename = path.join(searchpath, template)
            f = open_if_exists(filename)
            if f is None:
                continue
            with f:
                contents = f.read().decode(self.encoding)

            mtime = path.getmtime(filename)

            def uptodate():
                try:
                    return path.getmtime(filename) == mtime
                except OSError:
                    return False
            return contents, filename, uptodate
        raise TemplateNotFound(template)
Ejemplo n.º 22
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)
Ejemplo n.º 23
0
    def get_source(self, environment, template):
        for searchpath in self.searchpath:
            filename = path.join(searchpath, template)
            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)
Ejemplo n.º 24
0
    def get_source(self, environment, template):
        filename = f'templates/{template}/template.html'
        f = open_if_exists(filename)
        if f:
            try:
                contents = f.read().decode('utf-8')
            finally:
                f.close()

            mtime = posixpath.getmtime(filename)

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

            return contents, filename, uptodate

        raise TemplateNotFound(template)
Ejemplo n.º 25
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)
Ejemplo n.º 26
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
Ejemplo n.º 27
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
Ejemplo n.º 28
0
    def get_source(self, environment: Any, template: str) -> Any:
        # 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)
Ejemplo n.º 29
0
    def get_source(self, environment, template):
        '''Overwritten FileSystemLoader.get_source method that extracts the
        filename that is used to load each filename and adds it to 
        self.loaded_filenames.
        '''
        for searchpath in self.searchpath:
            filename = os.path.join(searchpath, template)
            f = open_if_exists(filename)
            if f is None:
                continue
            try:
                contents = f.read().decode(self.encoding)
            finally:
                f.close()

            self.loaded_filenames.add(filename)

            return super(FileSystemLoaderRecorder,
                         self).get_source(environment, template)

        # If the template isn't found, then we have to drop out.
        raise TemplateNotFound(template)
Ejemplo n.º 30
0
    def get_source(self, environment, template):
        '''Overwritten FileSystemLoader.get_source method that extracts the
        filename that is used to load each filename and adds it to 
        self.loaded_filenames.
        '''
        for searchpath in self.searchpath:
            filename = os.path.join(searchpath, template)
            f = open_if_exists(filename)
            if f is None:
                continue
            try:
                contents = f.read().decode(self.encoding)
            finally:
                f.close()

            self.loaded_filenames.add(filename)

            return super(FileSystemLoaderRecorder, self).get_source(
                    environment, template)

        # If the template isn't found, then we have to drop out.
        raise TemplateNotFound(template)
Ejemplo n.º 31
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
Ejemplo n.º 32
0
        def get_source(self, environment, template):
            # based on https://github.com/pallets/jinja/blob/ca8b0b0287e320fe1f4a74f36910ef7ae3303d99/src/jinja2/loaders.py#L174
            pieces = self.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)

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

                return contents, filename, uptodate
            raise TemplateNotFound(template)
Ejemplo n.º 33
0
    def get_source(self, environment, template):
        if template == self.base_ref:
            fn = self.base_fn
        else:
            fn = os.path.join(self.disttemp_path, template + self.template_postfix)

        f = open_if_exists(fn)
        if not f:
            return TemplateNotFound(template)
        try:
            contents = f.read().decode(self.encoding)
        finally:
            f.close()

        mtime = os.path.getmtime(self.base_fn)

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

        return contents, fn, uptodate
Ejemplo n.º 34
0
    def get_source(self, environment, template):
        if template == self.base_ref:
            fn = self.base_fn
        else:
            fn = os.path.join(self.disttemp_path,
                              template + self.template_postfix)

        f = open_if_exists(fn)
        if not f:
            return TemplateNotFound(template)
        try:
            contents = f.read().decode(self.encoding)
        finally:
            f.close()

        mtime = os.path.getmtime(self.base_fn)

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

        return contents, fn, uptodate