Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing generic IO type to BinaryIO. #100

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 3 in minimalkv/_key_value_store.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/_key_value_store.py#L3

Added line #L3 was not covered by tests

from uritools import SplitResult

Expand Down Expand Up @@ -92,7 +92,7 @@
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:

Check warning on line 95 in minimalkv/_key_value_store.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/_key_value_store.py#L95

Added line #L95 was not covered by tests
"""Write data at key to file.

Like :meth:`~mininmalkv.KeyValueStore.put_file`, this method allows backends to
Expand All @@ -106,7 +106,7 @@
----------
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 @@
"""
return list(self.iter_keys(prefix))

def open(self, key: str) -> IO:
def open(self, key: str) -> BinaryIO:

Check warning on line 191 in minimalkv/_key_value_store.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/_key_value_store.py#L191

Added line #L191 was not covered by tests
"""Open record at key.

Parameters
Expand All @@ -198,7 +198,7 @@

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 @@
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:

Check warning on line 243 in minimalkv/_key_value_store.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/_key_value_store.py#L243

Added line #L243 was not covered by tests
"""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 @@
----------
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 @@

return buf.getvalue()

def _get_file(self, key: str, file: IO) -> str:
def _get_file(self, key: str, file: BinaryIO) -> str:

Check warning on line 316 in minimalkv/_key_value_store.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/_key_value_store.py#L316

Added line #L316 was not covered by tests
"""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 @@
"""
return key in self.keys()

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:

Check warning on line 368 in minimalkv/_key_value_store.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/_key_value_store.py#L368

Added line #L368 was not covered by tests
"""Open record at key.

Parameters
Expand All @@ -375,7 +375,7 @@

Returns
-------
file: file-like
file: BinaryIO
Opened file.
"""
raise NotImplementedError
Expand All @@ -398,14 +398,14 @@
"""
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:

Check warning on line 401 in minimalkv/_key_value_store.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/_key_value_store.py#L401

Added line #L401 was not covered by tests
"""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

Check warning on line 2 in minimalkv/_mixins.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/_mixins.py#L2

Added line #L2 was not covered by tests

from minimalkv._constants import FOREVER, NOT_SET, VALID_KEY_RE_EXTENDED

Expand Down Expand Up @@ -161,7 +161,7 @@
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 @@
----------
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 @@
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

Check warning on line 1 in minimalkv/cache.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/cache.py#L1

Added line #L1 was not covered by tests

from minimalkv._key_value_store import KeyValueStore
from minimalkv.decorator import StoreDecorator
Expand Down Expand Up @@ -81,7 +81,7 @@
# 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:

Check warning on line 84 in minimalkv/cache.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/cache.py#L84

Added line #L84 was not covered by tests
"""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 @@
----------
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 @@
# if an IOError occured, file pointer may be dirty - cannot proceed
# safely

def open(self, key: str) -> IO:
def open(self, key: str) -> BinaryIO:

Check warning on line 117 in minimalkv/cache.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/cache.py#L117

Added line #L117 was not covered by tests
"""Open record at key.

If a cache miss occurs, the value is retrieved, stored in the cache,
Expand All @@ -133,7 +133,7 @@

Returns
-------
file: file-like
file: BinaryIO
Read-only file-like object for reading data at key.

"""
Expand Down Expand Up @@ -205,7 +205,7 @@
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:

Check warning on line 208 in minimalkv/cache.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/cache.py#L208

Added line #L208 was not covered by tests
"""Store contents of file at key.

Will store the value in the backing store. Afterwards delete the (original)
Expand All @@ -215,7 +215,7 @@
----------
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

Check warning on line 4 in minimalkv/db/mongo.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/db/mongo.py#L4

Added line #L4 was not covered by tests

from bson.binary import Binary

Expand Down Expand Up @@ -37,7 +37,7 @@
except StopIteration as e:
raise KeyError(key) from e

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:

Check warning on line 40 in minimalkv/db/mongo.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/db/mongo.py#L40

Added line #L40 was not covered by tests
return BytesIO(self._get(key))

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

def _put_file(self, key: str, file: IO) -> str:
def _put_file(self, key: str, file: BinaryIO) -> str:

Check warning on line 49 in minimalkv/db/mongo.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/db/mongo.py#L49

Added line #L49 was not covered by tests
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

Check warning on line 2 in minimalkv/db/sql.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/db/sql.py#L2

Added line #L2 was not covered by tests

from sqlalchemy import Column, LargeBinary, String, Table, exists, select
from sqlalchemy.orm import Session
Expand Down Expand Up @@ -39,7 +39,7 @@

return rv

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:

Check warning on line 42 in minimalkv/db/sql.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/db/sql.py#L42

Added line #L42 was not covered by tests
return BytesIO(self._get(key))

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

def _put_file(self, key: str, file: IO) -> str:
def _put_file(self, key: str, file: BinaryIO) -> str:

Check warning on line 82 in minimalkv/db/sql.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/db/sql.py#L82

Added line #L82 was not covered by tests
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

Check warning on line 5 in minimalkv/fs.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/fs.py#L5

Added line #L5 was not covered by tests

from minimalkv._key_value_store import KeyValueStore
from minimalkv._mixins import CopyMixin, UrlMixin
Expand Down Expand Up @@ -76,7 +76,7 @@
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:

Check warning on line 79 in minimalkv/fs.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/fs.py#L79

Added line #L79 was not covered by tests
try:
f = open(self._build_filename(key), "rb")
return f
Expand Down Expand Up @@ -109,7 +109,7 @@
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:

Check warning on line 112 in minimalkv/fs.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/fs.py#L112

Added line #L112 was not covered by tests
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

Check warning on line 3 in minimalkv/fsspecstore.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/fsspecstore.py#L3

Added line #L3 was not covered by tests

from minimalkv.net._net_common import LAZY_PROPERTY_ATTR_PREFIX, lazy_property

Expand Down Expand Up @@ -189,21 +189,21 @@
except FileNotFoundError:
pass

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:

Check warning on line 192 in minimalkv/fsspecstore.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/fsspecstore.py#L192

Added line #L192 was not covered by tests
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:

Check warning on line 199 in minimalkv/fsspecstore.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/fsspecstore.py#L199

Added line #L199 was not covered by tests
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:

Check warning on line 206 in minimalkv/fsspecstore.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/fsspecstore.py#L206

Added line #L206 was not covered by tests
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

Check warning on line 4 in minimalkv/git.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/git.py#L4

Added line #L4 was not covered by tests

from dulwich.objects import Blob, Commit, Tree
from dulwich.repo import Repo
Expand Down Expand Up @@ -197,10 +197,10 @@
if o.path.decode("ascii").startswith(prefix):
yield o.path.decode("ascii")

def _open(self, key: str) -> IO:
def _open(self, key: str) -> BinaryIO:

Check warning on line 200 in minimalkv/git.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/git.py#L200

Added line #L200 was not covered by tests
return BytesIO(self._get(key))

def _put_file(self, key: str, file: IO) -> str:
def _put_file(self, key: str, file: BinaryIO) -> str:

Check warning on line 203 in minimalkv/git.py

View check run for this annotation

Codecov / codecov/patch

minimalkv/git.py#L203

Added line #L203 was not covered by tests
# 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
Loading