43 lines
1 KiB
Python
43 lines
1 KiB
Python
![]() |
#!/usr/bin/python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
from pathlib import Path
|
||
|
|
||
|
|
||
|
def load_yaml(infile: Path):
|
||
|
yaml_file = infile.parent / f'{infile.stem}.yaml'
|
||
|
if yaml_file.is_file():
|
||
|
with open(yaml_file, 'r') as fin:
|
||
|
lines = fin.readlines()
|
||
|
return lines
|
||
|
return None
|
||
|
|
||
|
|
||
|
def save_yaml(out_file: Path, lines: list[str]):
|
||
|
yaml_file = out_file.parent / f'{out_file.stem}.yaml'
|
||
|
with open(yaml_file, 'w', encoding='utf-8', newline='\n') as f_out:
|
||
|
f_out.writelines(lines)
|
||
|
|
||
|
|
||
|
IMAGE_TAG = 'image: '
|
||
|
|
||
|
|
||
|
def adjust_yaml_lines(lines: list[str], new_name: Path):
|
||
|
found = None
|
||
|
for idx, line in enumerate(lines):
|
||
|
if line.startswith(IMAGE_TAG):
|
||
|
found = idx
|
||
|
break
|
||
|
if found is None:
|
||
|
raise ValueError(f'yaml line starting with "{IMAGE_TAG}" not found!')
|
||
|
lines[found] = f'{IMAGE_TAG}{new_name.name}\n'
|
||
|
|
||
|
|
||
|
def create_corrected_yaml(src: Path, dst: Path):
|
||
|
my_lines = load_yaml(src)
|
||
|
adjust_yaml_lines(my_lines, dst)
|
||
|
save_yaml(dst, my_lines)
|
||
|
|
||
|
|
||
|
# if __name__ == '__main__':
|
||
|
# main()
|