Пример #1
0
 def __init__(self,
              loader: BaseLoader,
              trim_blocks: bool = False,
              lstrip_blocks: bool = False):
     super().__init__(
         loader=loader,  # nosec
         extensions=[
             nunavut.jinja.jinja2.ext.do,
             nunavut.jinja.jinja2.ext.loopcontrols, JinjaAssert
         ],
         autoescape=select_autoescape(enabled_extensions=('htm', 'html',
                                                          'xml', 'json'),
                                      default_for_string=False,
                                      default=False),
         undefined=StrictUndefined,
         keep_trailing_newline=True,
         lstrip_blocks=lstrip_blocks,
         trim_blocks=trim_blocks,
         auto_reload=False,
         cache_size=0)
Пример #2
0
    def __init__(
        self,
        namespace: nunavut.Namespace,
        generate_namespace_types: bool,
        language_context: nunavut.lang.LanguageContext,
        templates_dir: typing.Optional[typing.Union[
            pathlib.Path, typing.List[pathlib.Path]]] = None,
        followlinks: bool = False,
        trim_blocks: bool = False,
        lstrip_blocks: bool = False,
        additional_filters: typing.Optional[typing.Dict[
            str, typing.Callable]] = None,
        additional_tests: typing.Optional[typing.Dict[str,
                                                      typing.Callable]] = None,
        additional_globals: typing.Optional[typing.Dict[str,
                                                        typing.Any]] = None,
    ):

        super(Generator, self).__init__(namespace, generate_namespace_types,
                                        language_context)

        if templates_dir is None:
            templates_dirs = []  # type: typing.List[pathlib.Path]
        else:
            if not isinstance(templates_dir, list):
                templates_dirs = [templates_dir]
            else:
                templates_dirs = templates_dir

            for templates_dir_item in templates_dirs:
                if templates_dir_item is None:
                    raise ValueError("Templates directory argument was None")
                if not pathlib.Path(templates_dir_item).exists:
                    raise ValueError(
                        "Templates directory {} did not exist?".format(
                            templates_dir_item))

        self._templates_dirs = templates_dirs

        self._type_to_template_lookup_cache = dict(
        )  # type: typing.Dict[pydsdl.Any, pathlib.Path]

        self._templates_list = None  # type: typing.Optional[typing.List[pathlib.Path]]

        logger.info("Loading templates from {}".format(templates_dir))

        fs_loader = FileSystemLoader((str(d) for d in self._templates_dirs),
                                     followlinks=followlinks)

        target_language = self._language_context.get_target_language()

        if target_language is not None:
            template_loader = ChoiceLoader([
                fs_loader,
                PackageLoader(target_language.get_templates_package_name())
            ])
        else:
            template_loader = fs_loader

        autoesc = select_autoescape(enabled_extensions=('htm', 'html', 'xml',
                                                        'json'),
                                    default_for_string=False,
                                    default=False)

        self._env = Environment(
            loader=template_loader,  # nosec
            extensions=[
                nunavut.jinja.jinja2.ext.do,
                nunavut.jinja.jinja2.ext.loopcontrols, JinjaAssert
            ],
            autoescape=autoesc,
            undefined=StrictUndefined,
            keep_trailing_newline=True,
            lstrip_blocks=lstrip_blocks,
            trim_blocks=trim_blocks,
            auto_reload=False,
            cache_size=0)

        self._add_language_support()

        if additional_globals is not None:
            self._env.globals.update(additional_globals)

        self._add_nunavut_globals()
        self._add_instance_tests_from_root(pydsdl.SerializableType)
        self._add_filters_and_tests(additional_filters, additional_tests)
        nunavut.lang.LanguageContext.inject_into_globals(
            self._language_context, self._env.globals)