Skip to content

Commit 9e8542e

Browse files
committed
Fix writing yaml to strings
1 parent 909ad35 commit 9e8542e

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

gateway/gateway.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import os
99
from datetime import date, timedelta
10+
from io import StringIO
1011
from pathlib import Path
1112
from typing import Dict, NotRequired, TypedDict
1213

@@ -74,6 +75,11 @@ def write_yaml(path: Path, yaml_dict: dict | list):
7475
yaml = ruyaml.YAML()
7576
yaml.dump(yaml_dict, file)
7677

78+
def to_yaml_string(yaml_dict: dict | list):
79+
yaml = ruyaml.YAML()
80+
stream = StringIO()
81+
yaml.dump(yaml_dict, stream)
82+
return stream.getvalue()
7783

7884
def write_str(path: Path, content: str):
7985
with open(path, "w") as file:
@@ -190,11 +196,9 @@ def update_actions(dummy_path: Path, actions_path: Path):
190196
actions: ActionsYAML = load_yaml(actions_path)
191197

192198
update_refs(steps, actions)
193-
yaml = ruyaml.YAML()
194-
gha_print(yaml.dump(actions), "Generated List")
199+
gha_print(to_yaml_string(actions), "Generated List")
195200
write_yaml(actions_path, actions)
196201

197-
198202
def create_pattern(actions: ActionsYAML) -> list[str]:
199203
"""
200204
Create a pattern list of valid action references.
@@ -228,8 +232,7 @@ def update_patterns(pattern_path: Path, list_path: Path):
228232
actions: ActionsYAML = load_yaml(list_path)
229233
patterns = create_pattern(actions)
230234
comment = f"# This file was generated from {pattern_path} by gateway/gateway.py. DO NOT UPDATE MANUALLY.\n"
231-
yaml = ruyaml.YAML()
232-
patterns_str = comment + yaml.safe_dump(patterns)
235+
patterns_str = comment + to_yaml_string(patterns)
233236
gha_print(patterns_str, "Generated Patterns")
234237
write_str(pattern_path, patterns_str)
235238

@@ -283,6 +286,5 @@ def clean_actions(actions_path: Path):
283286
"""
284287
actions: ActionsYAML = load_yaml(actions_path)
285288
remove_expired_refs(actions)
286-
yaml = ruyaml.YAML()
287-
gha_print(yaml.safe_dump(actions), "Cleaned Actions")
289+
gha_print(to_yaml_string(actions), "Cleaned Actions")
288290
write_yaml(actions_path, actions)

0 commit comments

Comments
 (0)