File: pipeline.py

package info (click to toggle)
bird2 2.17.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,584 kB
  • sloc: ansic: 74,910; sh: 3,712; perl: 3,444; lex: 883; python: 495; makefile: 464; xml: 260; sed: 13
file content (35 lines) | stat: -rwxr-xr-x 875 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3

import importlib
import jinja2
import pathlib
import subprocess
import sys
import yaml

# Find where we are
localdir = pathlib.Path(__file__).parent

# Prepare Jinja2 environment
env = jinja2.Environment(loader=jinja2.FileSystemLoader(str(localdir)))
env.filters.update({ "to_yaml": lambda x: "" if type(x) is jinja2.runtime.Undefined else yaml.dump(x).rstrip() })

# Load and process input data
try:
    data = yaml.safe_load(rendered := env.get_template(f'data.yml.j2').render({}))
except yaml.parser.ParserError as e:
    print("Failed to render input data, generated output here:")
    print(rendered)
    raise e

# Load the actual template
template = env.get_template(f'template.yml.j2')

# Render the template
final = template.render({ **data })

# YAML is picky about tabs, forbid them
assert('\t' not in final)

# Produce output
print(final)