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 6923a5c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 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 @@ -134,8 +135,9 @@ 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):
"""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 @@ -151,10 +153,12 @@ def maybe_open(fp: Union[IO, str], mode: str = "w") -> Iterator[IO]:


def extract_format(fp: Union[IO, str]) -> str:
"""Extract the altair_saver output format from a file or filename."""
"""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 6923a5c

Please sign in to comment.