| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | |
| 19 | from jinja2 import Environment, PackageLoader, select_autoescape |
| 20 | import json |
| 21 | import yaml |
| 22 | |
| 23 | |
| 24 | def render_jinja_template(self, template_file, output_file=None, **kwargs): |
| 25 | """Renders a jinja template with the provided values |
| 26 | |
| 27 | Args: |
| 28 | template_file: Jinja template to be rendered |
| 29 | output_file: Output file |
| 30 | kwargs: (key,value) pairs to be replaced in the template |
| 31 | |
| 32 | Returns: |
| 33 | content: The content of the rendered template |
| 34 | """ |
| 35 | |
| 36 | # Load the template from file |
| 37 | # loader = FileSystemLoader("osm_lcm/odu_libs/templates") |
| 38 | loader = PackageLoader("osm_lcm", "odu_libs/templates") |
| 39 | self.logger.debug(f"Loader: {loader}") |
| 40 | env = Environment(loader=loader, autoescape=select_autoescape()) |
| 41 | self.logger.debug(f"Env: {env}") |
| 42 | |
| 43 | template_list = env.list_templates() |
| 44 | self.logger.debug(f"Template list: {template_list}") |
| 45 | template = env.get_template(template_file) |
| 46 | self.logger.debug(f"Template: {template}") |
| 47 | |
| 48 | # Replace kwargs |
| 49 | self.logger.debug(f"Kwargs: {kwargs}") |
| 50 | content = template.render(kwargs) |
| 51 | if output_file: |
| 52 | with open(output_file, "w") as c_file: |
| 53 | c_file.write(content) |
| 54 | return content |
| 55 | |
| 56 | |
| 57 | def render_yaml_template(self, template_file, output_file=None, **kwargs): |
| 58 | """Renders a YAML template with the provided values |
| 59 | |
| 60 | Args: |
| 61 | template_file: Yaml template to be rendered |
| 62 | output_file: Output file |
| 63 | kwargs: (key,value) pairs to be replaced in the template |
| 64 | |
| 65 | Returns: |
| 66 | content: The content of the rendered template |
| 67 | """ |
| 68 | |
| 69 | def print_yaml_json(document, to_json=False): |
| 70 | if to_json: |
| 71 | print(json.dumps(document, indent=4)) |
| 72 | else: |
| 73 | print( |
| 74 | yaml.safe_dump( |
| 75 | document, indent=4, default_flow_style=False, sort_keys=False |
| 76 | ) |
| 77 | ) |
| 78 | |
| 79 | # Load template in dictionary |
| 80 | with open(template_file, "r") as t_file: |
| 81 | content_dict = yaml.safe_load(t_file.read()) |
| 82 | # Replace kwargs |
| 83 | self.self.logger.debug(f"Kwargs: {kwargs}") |
| 84 | for k, v in kwargs: |
| 85 | content_dict[k] = v |
| 86 | |
| 87 | content = print_yaml_json(content_dict) |
| 88 | if output_file: |
| 89 | with open(output_file, "w") as c_file: |
| 90 | c_file.write(content) |
| 91 | return content |