Skip to content

Commit

Permalink
Minor cleanup to support Paths better.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlevy committed Nov 1, 2024
1 parent 381947a commit 44589e9
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions frontmatter_format/yaml_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
YAML file storage. Wraps ruamel.yaml with a few extra features.
"""

import os
from io import StringIO
from pathlib import Path
from typing import Any, Callable, List, Optional, TextIO, Type

from ruamel.yaml import Representer, YAML
Expand Down Expand Up @@ -93,12 +93,12 @@ def from_yaml_string(yaml_string: str) -> Any:
return new_yaml().load(yaml_string)


def read_yaml_file(filename: str) -> Any:
def read_yaml_file(path: str | Path) -> Any:
"""
Read YAML file into a Python object.
"""
with open(filename, "r") as f:
return new_yaml().load(f)
path = Path(path)
return new_yaml().load(path.read_text(encoding="utf-8"))


def to_yaml_string(
Expand All @@ -122,19 +122,21 @@ def dump_yaml(


def write_yaml_file(
value: Any, filename: str, key_sort: Optional[KeySort] = None, stringify_unknown: bool = False
value: Any,
path: str | Path,
key_sort: Optional[KeySort] = None,
stringify_unknown: bool = False,
):
"""
Write the given value to the YAML file, creating it atomically.
"""
temp_filename = f"{filename}.yml.tmp" # Same directory with a temporary suffix.
path = Path(path)
temp_path = path.with_suffix(".yml.tmp")
try:
with open(temp_filename, "w", encoding="utf-8") as f:
dump_yaml(value, f, key_sort, stringify_unknown=stringify_unknown)
os.replace(temp_filename, filename)
temp_path.write_text(
to_yaml_string(value, key_sort, stringify_unknown=stringify_unknown), encoding="utf-8"
)
temp_path.replace(path)
except Exception as e:
try:
os.remove(temp_filename)
except FileNotFoundError:
pass
temp_path.unlink(missing_ok=True)
raise e

0 comments on commit 44589e9

Please sign in to comment.