Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
allow Paths in save()
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalTS committed Nov 30, 2020
1 parent 31febb5 commit 35bd7b2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions altair_saver/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from http import client
import io
import os
import pathlib
import socket
import subprocess
import sys
Expand Down Expand Up @@ -133,9 +134,10 @@ def temporary_filename(


@contextlib.contextmanager
def maybe_open(fp: Union[IO, str], mode: str = "w") -> Iterator[IO]:
"""Context manager to write to a file specified by filename or file-like object"""
if isinstance(fp, str):
def maybe_open(fp: Union[IO, str, pathlib.PurePath], mode: str = "w") -> Iterator[IO]:
"""Context manager to write to a file specified by filename, Path or
file-like object"""
if isinstance(fp, str) or isinstance(fp, pathlib.PurePath):
with open(fp, mode) as f:
yield f
elif isinstance(fp, io.TextIOBase) and "b" in mode:
Expand All @@ -150,11 +152,13 @@ def maybe_open(fp: Union[IO, str], mode: str = "w") -> Iterator[IO]:
yield fp


def extract_format(fp: Union[IO, str]) -> str:
"""Extract the altair_saver output format from a file or filename."""
def extract_format(fp: Union[IO, str, pathlib.PurePath]) -> str:
"""Extract the altair_saver output format from a file, filename or Path."""
filename: Optional[str]
if isinstance(fp, str):
filename = fp
elif isinstance(fp, pathlib.PurePath):
filename = str(fp)
else:
filename = getattr(fp, "name", None)
if filename is None:
Expand Down

0 comments on commit 35bd7b2

Please sign in to comment.