Exemple #1
0
 def run_j2(self, template, undef_ok=False):
     if undef_ok:
         environment = jinja2.Environment(extensions=["jinja2.ext.do"],
                                          undefined=SilentUndefined)
     else:
         environment = jinja2.Environment(extensions=["jinja2.ext.do"])
         environment = j2_load_plugins(environment)
     return environment.from_string(template).render(self._data)
    def merge_yaml(
        self,
        path,
        dict_strategy="merge",
        list_strategy="exclusive",
        other_types_strategy="use_existing",
        type_conflict_strategy="use_existing",
        private_data=None,
    ):
        try:
            with open(path, "r") as stream:
                try:
                    raw_data = stream.read()
                    tmp = []
                    for l in raw_data.split("\n"):
                        if (u"{{" not in l and u"}}" not in l
                                and u"{%" not in l and u"%}" not in l):
                            tmp.append(l)
                    parsed_raw_data = yaml.safe_load("\n".join(tmp))
                    if not parsed_raw_data:
                        parsed_raw_data = {}

                    for k, v in self._data.items():
                        if k not in list(parsed_raw_data.keys()):
                            parsed_raw_data[k] = v

                    environment = jinja2.Environment(
                        extensions=["jinja2.ext.do"])
                    environment = j2_load_plugins(environment)

                    parsed_raw_data["configuration"] = self._data
                    if private_data:
                        parsed_raw_data["_configuration"] = deepcopy(
                            private_data)
                    parsed_data = environment.from_string(raw_data).render(
                        parsed_raw_data)

                    final_data = yaml.safe_load(parsed_data)
                    if not final_data:
                        final_data = {}
                    self.merge_dict(
                        final_data,
                        dict_strategy,
                        list_strategy,
                        other_types_strategy,
                        type_conflict_strategy,
                    )
                    return final_data
                except yaml.YAMLError as e:
                    self._log.error(e)
        except IOError:
            self._log.debug("{0} not found".format(path))
            pass
        return {}
Exemple #3
0
    def setup_jinja2(self):
        j2_loader = jinja2.FileSystemLoader(self.task_base_dir)
        self.j2_environment = jinja2.Environment(loader=j2_loader,
                                                 extensions=["jinja2.ext.do"])
        self.j2_environment.globals["parent"] = self
        self.j2_environment.globals["configuration"] = self.configuration._data
        self.j2_environment.globals[
            "_configuration"] = self._configuration._data
        self.j2_environment.trim_blocks = True
        self.j2_environment.lstrip_blocks = True

        self.j2_environment = j2_load_plugins(self.j2_environment)
Exemple #4
0
    def load_schema_yaml(self, path):

        try:
            with open(path, "r") as stream:
                try:
                    raw_data = stream.read()

                    environment = jinja2.Environment(
                        extensions=["jinja2.ext.do"])
                    environment = j2_load_plugins(environment)
                    parsed_data = environment.from_string(raw_data).render(
                        self.configuration._data)
                    # final_data=yaml.safe_load(parsed_data)
                    final_data = dotty(yaml.safe_load(parsed_data))

                    for schema_extension in self._schema_extensions:
                        self._log.error("DEPRECATED")
                        for key, value in schema_extension["data"].items():
                            # self._log.debug(key)
                            if schema_extension["path"] != ".":
                                # self._log.error(pformat(final_data))
                                final_data[schema_extension["path"]][
                                    key] = value["schema"]
                                if value.get("options"):
                                    options_path = (
                                        schema_extension["path"].replace(
                                            "schema", "options").replace(
                                                "properties", "items"))
                                    final_data[options_path][key] = value[
                                        "options"]
                            else:

                                final_data.update({key: value})

                    final_data = final_data.to_dict()
                    # self._log.error(pformat(final_data))

                    if not final_data:
                        final_data = {}
                    return final_data
                except yaml.YAMLError as e:
                    self._log.debug(parsed_data)
                    self._log.error(e)
        except IOError:
            self._log.debug("{0} not found".format(path))
            pass
        return {}