Skip to content

Commit

Permalink
Merge pull request #100 from johanols/changing-io-typing
Browse files Browse the repository at this point in the history
Changing generic `IO` type to `BinaryIO`.
  • Loading branch information
xhochy authored Nov 28, 2023
2 parents 334dedd + 6f67d27 commit 198bf53
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 59 deletions.
4 changes: 4 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
*********

1.8.5
=====
* Changed generic `IO` type to `BinaryIO`.

1.8.4
=====
* Removing invalid BSD-3 Clause license classifier.
Expand Down
26 changes: 13 additions & 13 deletions minimalkv/_key_value_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from io import BytesIO
from types import TracebackType
from typing import IO, Dict, Iterator, List, Optional, Type, Union
from typing import BinaryIO, Dict, Iterator, List, Optional, Type, Union

from uritools import SplitResult

Expand Down Expand Up @@ -92,7 +92,7 @@ def get(self, key: str) -> bytes:
self._check_valid_key(key)
return self._get(key)

def get_file(self, key: str, file: Union[str, IO]) -> str:
def get_file(self, key: str, file: Union[str, BinaryIO]) -> str:
"""Write data at key to file.
Like :meth:`~mininmalkv.KeyValueStore.put_file`, this method allows backends to
Expand All @@ -106,7 +106,7 @@ def get_file(self, key: str, file: Union[str, IO]) -> str:
----------
key : str
The key to be read.
file : file-like or str
file : BinaryIO or str
Output filename or file-like object with a ``write`` method.
Raises
Expand Down Expand Up @@ -188,7 +188,7 @@ def keys(self, prefix: str = "") -> List[str]:
"""
return list(self.iter_keys(prefix))

def open(self, key: str) -> IO:
def open(self, key: str) -> BinaryIO:
"""Open record at key.
Parameters
Expand All @@ -198,7 +198,7 @@ def open(self, key: str) -> IO:
Returns
-------
file: file-like
file: BinaryIO
Read-only file-like object for reading data at key.
Raises
Expand Down Expand Up @@ -240,7 +240,7 @@ def put(self, key: str, data: bytes) -> str:
raise OSError("Provided data is not of type bytes")
return self._put(key, data)

def put_file(self, key: str, file: Union[str, IO]) -> str:
def put_file(self, key: str, file: Union[str, BinaryIO]) -> str:
"""Store contents of file at key.
Store data from a file into key. ``file`` can be a string, which will be
Expand All @@ -253,7 +253,7 @@ def put_file(self, key: str, file: Union[str, IO]) -> str:
----------
key : str
Key where to store data in file.
file : file-like or str
file : BinaryIO or str
A filename or a file-like object with a read method.
Returns
Expand Down Expand Up @@ -313,14 +313,14 @@ def _get(self, key: str) -> bytes:

return buf.getvalue()

def _get_file(self, key: str, file: IO) -> str:
def _get_file(self, key: str, file: BinaryIO) -> str:
"""Write data at key to file-like object file.
Parameters
----------
key : str
Key of data to be written to file.
file : file-like
file : BinaryIO
File-like object with a *write* method to be written.
"""
bufsize = 1024 * 1024
Expand Down Expand Up @@ -365,7 +365,7 @@ def _has_key(self, key: str) -> bool:
"""
return key in self.keys()

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:
"""Open record at key.
Parameters
Expand All @@ -375,7 +375,7 @@ def _open(self, key: str) -> IO:
Returns
-------
file: file-like
file: BinaryIO
Opened file.
"""
raise NotImplementedError
Expand All @@ -398,14 +398,14 @@ def _put(self, key: str, data: bytes) -> str:
"""
return self._put_file(key, BytesIO(data))

def _put_file(self, key: str, file: IO) -> str:
def _put_file(self, key: str, file: BinaryIO) -> str:
"""Store data from file-like object at key.
Parameters
----------
key : str
Key at which to store contents of file.
file : file-like
file : BinaryIO
File-like object to store data from.
Returns
Expand Down
13 changes: 8 additions & 5 deletions minimalkv/_mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from io import BytesIO
from typing import IO, Callable, Optional, Union
from typing import BinaryIO, Callable, Optional, Union

from minimalkv._constants import FOREVER, NOT_SET, VALID_KEY_RE_EXTENDED

Expand Down Expand Up @@ -161,7 +161,7 @@ def put(
def put_file(
self,
key: str,
file: Union[str, IO],
file: Union[str, BinaryIO],
ttl_secs: Optional[Union[float, int, str]] = None,
) -> str:
"""Store contents of file at key.
Expand All @@ -181,7 +181,7 @@ def put_file(
----------
key : str
Key where to store data in file.
file : file-like or str
file : BinaryIO or str
A filename or an object with a read method.
ttl_secs : str or numeric or None, optional, default = None
Number of seconds until the key expires.
Expand Down Expand Up @@ -232,15 +232,18 @@ def _put(
return self._put_file(key, BytesIO(data), ttl_secs)

def _put_file(
self, key: str, file: IO, ttl_secs: Optional[Union[str, float, int]] = None
self,
key: str,
file: BinaryIO,
ttl_secs: Optional[Union[str, float, int]] = None,
):
"""Store contents of file at key.
Parameters
----------
key : str
Key under which data should be stored.
file : file-like
file : BinaryIO
File-like object with a ``read`` method.
ttl_secs : str or numeric or None, optional, default = None
Number of seconds until the key expires.
Expand Down
14 changes: 7 additions & 7 deletions minimalkv/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import IO, Union
from typing import BinaryIO, Union

from minimalkv._key_value_store import KeyValueStore
from minimalkv.decorator import StoreDecorator
Expand Down Expand Up @@ -81,7 +81,7 @@ def get(self, key: str) -> bytes:
# cache error, ignore completely and return from backend
return self._dstore.get(key)

def get_file(self, key: str, file: Union[str, IO]) -> str:
def get_file(self, key: str, file: Union[str, BinaryIO]) -> str:
"""Write data at key to file.
If a cache miss occurs, the value is retrieved, stored in the cache and
Expand All @@ -98,7 +98,7 @@ def get_file(self, key: str, file: Union[str, IO]) -> str:
----------
key : str
The key to be read.
file : file-like or str
file : BinaryIO or str
Output filename or file-like object with a ``write`` method.
"""
Expand All @@ -114,7 +114,7 @@ def get_file(self, key: str, file: Union[str, IO]) -> str:
# if an IOError occured, file pointer may be dirty - cannot proceed
# safely

def open(self, key: str) -> IO:
def open(self, key: str) -> BinaryIO:
"""Open record at key.
If a cache miss occurs, the value is retrieved, stored in the cache,
Expand All @@ -133,7 +133,7 @@ def open(self, key: str) -> IO:
Returns
-------
file: file-like
file: BinaryIO
Read-only file-like object for reading data at key.
"""
Expand Down Expand Up @@ -205,7 +205,7 @@ def put(self, key: str, data: bytes) -> str:
finally:
self.cache.delete(key)

def put_file(self, key: str, file: Union[str, IO]) -> str:
def put_file(self, key: str, file: Union[str, BinaryIO]) -> str:
"""Store contents of file at key.
Will store the value in the backing store. Afterwards delete the (original)
Expand All @@ -215,7 +215,7 @@ def put_file(self, key: str, file: Union[str, IO]) -> str:
----------
key : str
Key where to store data in file.
file : file-like or str
file : BinaryIO or str
A filename or a file-like object with a read method.
Returns
Expand Down
6 changes: 3 additions & 3 deletions minimalkv/db/mongo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pickle
import re
from io import BytesIO
from typing import IO, Iterator
from typing import BinaryIO, Iterator

from bson.binary import Binary

Expand Down Expand Up @@ -37,7 +37,7 @@ def _get(self, key: str) -> bytes:
except StopIteration as e:
raise KeyError(key) from e

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:
return BytesIO(self._get(key))

def _put(self, key: str, value: bytes) -> str:
Expand All @@ -46,7 +46,7 @@ def _put(self, key: str, value: bytes) -> str:
)
return key

def _put_file(self, key: str, file: IO) -> str:
def _put_file(self, key: str, file: BinaryIO) -> str:
return self._put(key, file.read())

def iter_keys(self, prefix: str = "") -> Iterator[str]:
Expand Down
6 changes: 3 additions & 3 deletions minimalkv/db/sql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from io import BytesIO
from typing import IO, Iterator
from typing import BinaryIO, Iterator

from sqlalchemy import Column, LargeBinary, String, Table, exists, select
from sqlalchemy.orm import Session
Expand Down Expand Up @@ -39,7 +39,7 @@ def _get(self, key: str) -> bytes:

return rv

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:
return BytesIO(self._get(key))

def _copy(self, source: str, dest: str):
Expand Down Expand Up @@ -79,7 +79,7 @@ def _put(self, key: str, data: bytes) -> str:
session.commit()
return key

def _put_file(self, key: str, file: IO) -> str:
def _put_file(self, key: str, file: BinaryIO) -> str:
return self._put(key, file.read())

def iter_keys(self, prefix: str = "") -> Iterator[str]: # noqa D
Expand Down
6 changes: 3 additions & 3 deletions minimalkv/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os.path
import shutil
import urllib.parse
from typing import IO, Any, Callable, Iterator, List, Optional, Union, cast
from typing import Any, BinaryIO, Callable, Iterator, List, Optional, Union, cast

from minimalkv._key_value_store import KeyValueStore
from minimalkv._mixins import CopyMixin, UrlMixin
Expand Down Expand Up @@ -76,7 +76,7 @@ def _fix_permissions(self, filename: str) -> None:
def _has_key(self, key: str) -> bool:
return os.path.exists(self._build_filename(key))

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:
try:
f = open(self._build_filename(key), "rb")
return f
Expand Down Expand Up @@ -109,7 +109,7 @@ def _ensure_dir_exists(self, path: str) -> None:
if not os.path.isdir(path):
raise e

def _put_file(self, key: str, file: IO, *args, **kwargs) -> str:
def _put_file(self, key: str, file: BinaryIO, *args, **kwargs) -> str:
bufsize = self.bufsize

target = self._build_filename(key)
Expand Down
8 changes: 4 additions & 4 deletions minimalkv/fsspecstore.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import io
import warnings
from typing import IO, TYPE_CHECKING, Iterator, Optional, Union
from typing import TYPE_CHECKING, BinaryIO, Iterator, Optional, Union

from minimalkv.net._net_common import LAZY_PROPERTY_ATTR_PREFIX, lazy_property

Expand Down Expand Up @@ -189,21 +189,21 @@ def _delete(self, key: str) -> None:
except FileNotFoundError:
pass

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:
try:
return self._fs.open(f"{self._prefix}{key}")
except FileNotFoundError as e:
raise KeyError(key) from e

# Required to prevent error when credentials are not sufficient for listing objects
def _get_file(self, key: str, file: IO) -> str:
def _get_file(self, key: str, file: BinaryIO) -> str:
try:
file.write(self._fs.cat_file(f"{self._prefix}{key}"))
return key
except FileNotFoundError as e:
raise KeyError(key) from e

def _put_file(self, key: str, file: IO) -> str:
def _put_file(self, key: str, file: BinaryIO) -> str:
self._fs.pipe_file(f"{self._prefix}{key}", file.read(), **self._write_kwargs)
return key

Expand Down
6 changes: 3 additions & 3 deletions minimalkv/git.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import time
from io import BytesIO
from typing import IO, Iterator, List, Optional, Union
from typing import BinaryIO, Iterator, List, Optional, Union

from dulwich.objects import Blob, Commit, Tree
from dulwich.repo import Repo
Expand Down Expand Up @@ -197,10 +197,10 @@ def iter_keys(self, prefix: str = "") -> Iterator[str]: # noqa D
if o.path.decode("ascii").startswith(prefix):
yield o.path.decode("ascii")

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:
return BytesIO(self._get(key))

def _put_file(self, key: str, file: IO) -> str:
def _put_file(self, key: str, file: BinaryIO) -> str:
# FIXME: it may be worth to try to move large files directly into the
# store here
return self._put(key, file.read())
Expand Down
Loading

0 comments on commit 198bf53

Please sign in to comment.