Skip to content

Commit

Permalink
Add overloads for lzma
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Nov 15, 2024
1 parent 24acde0 commit 5fef95b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
36 changes: 33 additions & 3 deletions src/pystow/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""API functions for PyStow."""

import bz2
import io
import lzma
import sqlite3
from contextlib import contextmanager
from io import BytesIO, StringIO
Expand All @@ -19,7 +21,7 @@
overload,
)

from .constants import JSON, BytesOpener, Opener, Provider
from .constants import JSON, BytesOpener, Provider
from .impl import Module

if TYPE_CHECKING:
Expand Down Expand Up @@ -503,6 +505,34 @@ def ensure_open_zip(
yield yv


@overload
@contextmanager
def ensure_open_lzma(
key: str,
*subkeys: str,
url: str,
name: Optional[str],
force: bool,
download_kwargs: Optional[Mapping[str, Any]],
mode: Literal["r", "w", "rt", "wt"] = "rt",
open_kwargs: Optional[Mapping[str, Any]],
) -> Generator[io.TextIOWrapper[lzma.LZMAFile], None, None]: ...


@overload
@contextmanager
def ensure_open_lzma(
key: str,
*subkeys: str,
url: str,
name: Optional[str],
force: bool,
download_kwargs: Optional[Mapping[str, Any]],
mode: Literal["rb", "wb"] = ...,
open_kwargs: Optional[Mapping[str, Any]],
) -> Generator[lzma.LZMAFile, None, None]: ...


@contextmanager
def ensure_open_lzma(
key: str,
Expand All @@ -511,9 +541,9 @@ def ensure_open_lzma(
name: Optional[str] = None,
force: bool = False,
download_kwargs: Optional[Mapping[str, Any]] = None,
mode: str = "r",
mode: Literal["r", "rb", "w", "wb", "rt", "wt"] = "rt",
open_kwargs: Optional[Mapping[str, Any]] = None,
) -> Opener:
) -> Generator[Union[lzma.LZMAFile, io.TextIOWrapper[lzma.LZMAFile]], None, None]:
"""Ensure a LZMA-compressed file is downloaded and open a file inside it.
:param key:
Expand Down
33 changes: 30 additions & 3 deletions src/pystow/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import bz2
import gzip
import io
import json
import logging
import lzma
Expand All @@ -27,7 +28,7 @@
)

from . import utils
from .constants import JSON, BytesOpener, Opener, Provider
from .constants import JSON, BytesOpener, Provider
from .utils import (
base_from_gzip_name,
download_from_google,
Expand Down Expand Up @@ -485,6 +486,32 @@ def open_gz(
with gzip.open(path, **open_kwargs) as file:
yield file

@overload
@contextmanager
def ensure_open_lzma(
self,
*subkeys: str,
url: str,
name: Optional[str],
force: bool,
download_kwargs: Optional[Mapping[str, Any]],
mode: Literal["r", "w", "rt", "wt"] = "rt",
open_kwargs: Optional[Mapping[str, Any]],
) -> Generator[io.TextIOWrapper[lzma.LZMAFile], None, None]: ...

@overload
@contextmanager
def ensure_open_lzma(
self,
*subkeys: str,
url: str,
name: Optional[str],
force: bool,
download_kwargs: Optional[Mapping[str, Any]],
mode: Literal["rb", "wb"] = ...,
open_kwargs: Optional[Mapping[str, Any]],
) -> Generator[lzma.LZMAFile, None, None]: ...

@contextmanager
def ensure_open_lzma(
self,
Expand All @@ -493,9 +520,9 @@ def ensure_open_lzma(
name: Optional[str] = None,
force: bool = False,
download_kwargs: Optional[Mapping[str, Any]] = None,
mode: str = "rt",
mode: Literal["r", "rb", "w", "wb", "rt", "wt"] = "rt",
open_kwargs: Optional[Mapping[str, Any]] = None,
) -> Opener:
) -> Generator[Union[lzma.LZMAFile, io.TextIOWrapper[lzma.LZMAFile]], None, None]:
"""Ensure a LZMA-compressed file is downloaded and open a file inside it.
:param subkeys:
Expand Down

0 comments on commit 5fef95b

Please sign in to comment.