Пример #1
0
    def __load_environment(self, template):
        """Private method for loading all the environments.

        Arguments:
            template {string} -- Template to load environment from.
        """

        self.template = template
        self.filename = template + self.extension

        if template.startswith('/'):
            # Filter blanks strings from the split
            location = list(filter(None, template.split('/')))
            self.filename = location[-1] + self.extension

            loader = ChoiceLoader(
                [PackageLoader(location[0], '/'.join(location[1:-1]))] +
                self.environments)

            # Set the searchpath since some packages look for this object
            # This is sort of a hack for now
            loader.searchpath = ''

            self.env = Environment(loader=loader,
                                   autoescape=select_autoescape(
                                       ['html', 'xml']),
                                   extensions=self._jinja_extensions)

        else:
            loader = ChoiceLoader([PackageLoader('resources', 'templates')] +
                                  self.environments)

            # Set the searchpath since some packages look for this object
            # This is sort of a hack for now
            loader.searchpath = ''

            self.env = Environment(loader=loader,
                                   autoescape=select_autoescape(
                                       ['html', 'xml']),
                                   extensions=self._jinja_extensions)

        self.env.filters.update(self._filters)
Пример #2
0
    def load_template(self, template):
        """Private method for loading all the locations into the current environment.

        Arguments:
            template {string} -- Template to load environment from.
        """
        self.template = template
        # transform given template path into a real file path with the configured extension
        self.filename = (as_filepath(template).replace(self.extension, "") +
                         self.extension)
        # assess if new loaders are required for the given template
        template_loaders = []
        # Case 1: the templates needs to be loaded from a namespace
        if ":" in template:
            namespace, rel_template_path = template.split(":")
            self.filename = (
                as_filepath(rel_template_path).replace(self.extension, "") +
                self.extension)
            namespace_paths = self.namespaces.get(namespace, None)
            if not namespace_paths:
                raise Exception(f"No such view namespace {namespace}.")
            for namespace_path in namespace_paths:
                package_name, package_path = path_to_package(namespace_path)
                template_loaders.append(
                    PackageLoader(package_name, package_path))

        # Case 2: an absolute path has been given
        elif template.startswith("/"):
            directory, filename = split(template)
            self.filename = filename.replace(self.extension,
                                             "") + self.extension
            package_name, package_path = path_to_package(directory)
            template_loaders.append(PackageLoader(package_name, package_path))

        # Else: use already defined view locations to load this template
        loader = ChoiceLoader(template_loaders + self.loaders)

        # @josephmancuso: what is this ??
        # Set the searchpath since some packages look for this object
        # This is sort of a hack for now
        loader.searchpath = ""

        self.env = Environment(
            loader=loader,
            autoescape=select_autoescape(["html", "xml"]),
            extensions=self._jinja_extensions,
            line_statement_prefix="@",
        )
        # add filters to environment
        self.env.filters.update(self._filters)
Пример #3
0
    def __load_environment(self, template):
        """Private method for loading all the environments.

        Arguments:
            template {string} -- Template to load environment from.
        """
        self.template = template
        self.filename = (
            template.replace(self._splice, "/").replace(".", "/") + self.extension
        )

        if template.startswith("/"):
            # Filter blanks strings from the split
            location = list(filter(None, template.split("/")))
            self.filename = location[-1] + self.extension

            loader = ChoiceLoader(
                [PackageLoader(location[0], "/".join(location[1:-1]))]
                + self.environments
            )
            self.env = Environment(
                loader=loader,
                autoescape=select_autoescape(["html", "xml"]),
                extensions=self._jinja_extensions,
                line_statement_prefix="@",
            )

        else:
            loader = ChoiceLoader(
                [PackageLoader("resources", "templates")] + self.environments
            )

            # Set the searchpath since some packages look for this object
            # This is sort of a hack for now
            loader.searchpath = ""

            self.env = Environment(
                loader=loader,
                autoescape=select_autoescape(["html", "xml"]),
                extensions=self._jinja_extensions,
                line_statement_prefix="@",
            )

        self.env.filters.update(self._filters)