Skip to content

Commit

Permalink
chore: allow passing kwargs to pathhelpers.write_file
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Oct 24, 2024
1 parent f7261cf commit 4ba2727
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions mknodes/utils/pathhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import pathlib
import shutil
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import upath

Expand Down Expand Up @@ -94,18 +94,26 @@ def clean_directory(
path.unlink()


def write_file(content: str | bytes, output_path: str | os.PathLike[str]):
def write_file(
content: str | bytes,
output_path: str | os.PathLike[str],
**kwargs: Any,
):
"""Write content to output_path, making sure any parent directories exist.
Encoding mode will be chosen automatically based on given content.
Arguments:
content: Content to write
output_path: path where file should get written to.
kwargs: Additional keyword arguments passed to "open"
"""
output_p = upath.UPath(output_path)
output_p.parent.mkdir(parents=True, exist_ok=True)
mode = "wb" if isinstance(content, bytes) else "w"
encoding = None if "b" in mode else "utf-8"
with output_p.open(mode=mode, encoding=encoding) as f: # type: ignore[call-overload]
kwargs["encoding"] = encoding
with output_p.open(mode=mode) as f: # type: ignore[call-overload]
f.write(content)


Expand Down

0 comments on commit 4ba2727

Please sign in to comment.