Skip to content

Commit

Permalink
Fix all problems pointed out by ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasMarwitzQC committed Nov 6, 2023
1 parent 359a7cd commit fe83151
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 98 deletions.
2 changes: 1 addition & 1 deletion minimalkv/_boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ def _get_s3bucket(
if create_if_missing:
return s3con.create_bucket(bucket)
else:
raise OSError(f"Bucket {bucket} does not exist")
raise OSError(f"Bucket {bucket} does not exist") from ex
raise
6 changes: 2 additions & 4 deletions minimalkv/_get_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
def get_store_from_url(
url: str, store_cls: Optional[Type[KeyValueStore]] = None
) -> KeyValueStore:
"""
Take a URL and return a minimalkv store according to the parameters in the URL.
"""Take a URL and return a minimalkv store according to the parameters in the URL.
Parameters
----------
Expand Down Expand Up @@ -97,8 +96,7 @@ def get_store_from_url(


def _extract_wrappers(parsed_url: SplitResult) -> List[str]:
"""
Extract wrappers from a parsed URL.
"""Extract wrappers from a parsed URL.
Wrappers allow you to add additional functionality to a store, e.g. encryption.
They can be specified in two ways:
Expand Down
9 changes: 3 additions & 6 deletions minimalkv/_key_value_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@


class KeyValueStore:
"""
Class to access a key-value store.
"""Class to access a key-value store.
Supported keys are ascii-strings containing alphanumeric characters or symbols out
of ``minimalkv._constants.VALID_NON_NUM`` of length not greater than 250. Values
Expand Down Expand Up @@ -141,8 +140,7 @@ def iter_keys(self, prefix: str = "") -> Iterator[str]:
raise NotImplementedError

def iter_prefixes(self, delimiter: str, prefix: str = "") -> Iterator[str]:
"""
Iterate over unique prefixes in the store up to delimiter, starting with prefix.
"""Iterate over unique prefixes in the store up to delimiter, starting with prefix.
If ``prefix`` contains ``delimiter``, return the prefix up to the first
occurence of delimiter after the prefix.
Expand Down Expand Up @@ -468,8 +466,7 @@ def __exit__(
def _from_parsed_url(
cls, parsed_url: SplitResult, query: Dict[str, str]
) -> "KeyValueStore":
"""
Build a ``KeyValueStore`` from a parsed URL.
"""Build a ``KeyValueStore`` from a parsed URL.
To build a ``KeyValueStore`` from a URL, use :func:`get_store_from_url`.
Expand Down
2 changes: 1 addition & 1 deletion minimalkv/_store_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _create_store_gcs(store_type, params):
from minimalkv._hstores import HGoogleCloudStore
from minimalkv.net.gcstore import GoogleCloudStore

if type(params["credentials"]) is bytes:
if isinstance(params["credentials"], bytes):
account_info = json.loads(params["credentials"].decode())
params["credentials"] = Credentials.from_service_account_info(
account_info,
Expand Down
16 changes: 8 additions & 8 deletions minimalkv/_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ def url2dict(url: str, raise_on_extra_params: bool = False) -> Dict[str, Any]:
)

u = urisplit(url)
parsed = dict(
scheme=u.getscheme(),
host=u.gethost(),
port=u.getport(),
path=u.getpath(),
query=u.getquerydict(),
userinfo=u.getuserinfo(),
)
parsed = {
"scheme": u.getscheme(),
"host": u.gethost(),
"port": u.getport(),
"path": u.getpath(),
"query": u.getquerydict(),
"userinfo": u.getuserinfo(),
}
fragment = u.getfragment()

params = {"type": parsed["scheme"]}
Expand Down
2 changes: 1 addition & 1 deletion minimalkv/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def get_file(self, key, file): # noqa D
try:
f = open(file, "wb")
except OSError as e:
raise OSError(f"Error opening {file} for writing: {e!r}")
raise OSError(f"Error opening {file} for writing: {e!r}") from e

# file is open, now we call ourself again with a proper file
try:
Expand Down
4 changes: 2 additions & 2 deletions minimalkv/db/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def _get(self, key: str) -> bytes:
try:
item = next(self.db[self.collection].find({"_id": key}))
return pickle.loads(item["v"])
except StopIteration:
raise KeyError(key)
except StopIteration as e:
raise KeyError(key) from e

def _open(self, key: str) -> IO:
return BytesIO(self._get(key))
Expand Down
2 changes: 1 addition & 1 deletion minimalkv/db/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ def iter_keys(self, prefix: str = "") -> Iterator[str]: # noqa D
query = select(self.table.c.key)
if prefix != "":
query = query.where(self.table.c.key.like(prefix + "%"))
return map(lambda v: str(v[0]), session.execute(query))
return (str(v[0]) for v in session.execute(query))
3 changes: 1 addition & 2 deletions minimalkv/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ def copy(self, source: str, dest: str): # noqa D


class PrefixDecorator(KeyTransformingDecorator):
"""
Prefixes any key with a string before passing it on the decorated store.
"""Prefixes any key with a string before passing it on the decorated store.
Automatically strips the prefix upon key retrieval.
Expand Down
12 changes: 5 additions & 7 deletions minimalkv/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _open(self, key: str) -> IO:
return f
except OSError as e:
if 2 == e.errno:
raise KeyError(key)
raise KeyError(key) from e
else:
raise

Expand All @@ -97,7 +97,7 @@ def _copy(self, source: str, dest: str) -> str:
return dest
except OSError as e:
if 2 == e.errno:
raise KeyError(source)
raise KeyError(source) from e
else:
raise

Expand Down Expand Up @@ -155,7 +155,7 @@ def keys(self, prefix: str = "") -> List[str]:
"""
root = os.path.abspath(self.root)
result = []
for dp, dn, fn in os.walk(root):
for dp, _, fn in os.walk(root):
for f in fn:
key = os.path.join(dp, f)[len(root) + 1 :]
if key.startswith(prefix):
Expand All @@ -174,8 +174,7 @@ def iter_keys(self, prefix: str = "") -> Iterator[str]:
return iter(self.keys(prefix))

def iter_prefixes(self, delimiter: str, prefix: str = "") -> Iterator[str]:
"""
Iterate over unique prefixes in the store up to delimiter, starting with prefix.
"""Iterate over unique prefixes in the store up to delimiter, starting with prefix.
If ``prefix`` contains ``delimiter``, return the prefix up to the first
occurence of delimiter after the prefix.
Expand Down Expand Up @@ -224,8 +223,7 @@ def _iter_prefixes_efficient(


class WebFilesystemStore(FilesystemStore):
"""
FilesystemStore supporting generating URLS for web applications.
"""FilesystemStore supporting generating URLS for web applications.
The most common use is to make the ``root`` directory of the filesystem store
available through a webserver.
Expand Down
34 changes: 14 additions & 20 deletions minimalkv/fsspecstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class FSSpecStoreEntry(io.BufferedIOBase):
"""A file-like object for reading from an entry in an FSSpecStore."""

def __init__(self, file: "AbstractBufferedFile"):
"""
Initialize an FSSpecStoreEntry.
"""Initialize an FSSpecStoreEntry.
Parameters
----------
Expand All @@ -32,8 +31,7 @@ def __init__(self, file: "AbstractBufferedFile"):
self._file = file

def seek(self, loc: int, whence: int = 0) -> int:
"""
Set current file location.
"""Set current file location.
Parameters
----------
Expand All @@ -46,9 +44,9 @@ def seek(self, loc: int, whence: int = 0) -> int:
raise ValueError("I/O operation on closed file.")
try:
return self._file.seek(loc, whence)
except ValueError:
except ValueError as e:
# Map ValueError to IOError
raise OSError
raise OSError from e

def tell(self) -> int:
"""Return the current offset as int. Always >= 0."""
Expand Down Expand Up @@ -90,8 +88,7 @@ def __init__(
write_kwargs: Optional[dict] = None,
custom_fs: Optional["AbstractFileSystem"] = None,
):
"""
Initialize an FSSpecStore.
"""Initialize an FSSpecStore.
The underlying fsspec FileSystem is created when the store is used for the first time.
Expand Down Expand Up @@ -126,30 +123,30 @@ def _prefix_exists(self) -> Union[None, bool]:

@property
def mkdir_prefix(self):
"""
Whether to create the prefix if it does not exist.
"""Whether to create the prefix if it does not exist.
.. note:: Deprecated in 2.0.0.
"""
warnings.warn(
"The mkdir_prefix attribute is deprecated!"
"It will be renamed to _mkdir_prefix in the next release.",
DeprecationWarning,
stacklevel=2,
)

return self._mkdir_prefix

@property
def prefix(self):
"""
Get the prefix used on the ``fsspec`` ``FileSystem`` when storing keys.
"""Get the prefix used on the ``fsspec`` ``FileSystem`` when storing keys.
.. note:: Deprecated in 2.0.0.
"""
warnings.warn(
"The prefix attribute is deprecated!"
"It will be renamed to _prefix in the next release.",
DeprecationWarning,
stacklevel=2,
)

return self._prefix
Expand Down Expand Up @@ -184,10 +181,7 @@ def iter_keys(self, prefix: str = "") -> Iterator[str]:

all_files_and_dirs = self._fs.find(dir_prefix, prefix=file_prefix)

return map(
lambda k: k.replace(f"{self._prefix}", ""),
all_files_and_dirs,
)
return (k.replace(f"{self._prefix}", "") for k in all_files_and_dirs)

def _delete(self, key: str) -> None:
try:
Expand All @@ -198,16 +192,16 @@ def _delete(self, key: str) -> None:
def _open(self, key: str) -> IO:
try:
return self._fs.open(f"{self._prefix}{key}")
except FileNotFoundError:
raise KeyError(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:
try:
file.write(self._fs.cat_file(f"{self._prefix}{key}"))
return key
except FileNotFoundError:
raise KeyError(key)
except FileNotFoundError as e:
raise KeyError(key) from e

def _put_file(self, key: str, file: IO) -> str:
self._fs.pipe_file(f"{self._prefix}{key}", file.read(), **self._write_kwargs)
Expand Down
4 changes: 2 additions & 2 deletions minimalkv/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ def _get(self, key: str) -> bytes:
fn = self.subdir + "/" + key
_, blob_id = tree.lookup_path(self.repo.__getitem__, fn.encode("ascii"))
blob = self.repo[blob_id]
except KeyError:
raise KeyError(key)
except KeyError as e:
raise KeyError(key) from e

return blob.data

Expand Down
3 changes: 1 addition & 2 deletions minimalkv/idgen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
In cases where you want to generate IDs automatically, decorators are available.
"""In cases where you want to generate IDs automatically, decorators are available.
These should be the outermost decorators, as they change the
signature of some of the put methods slightly.
Expand Down
4 changes: 1 addition & 3 deletions minimalkv/memory/redisstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def keys(self, prefix: str = "") -> List[str]:
IOError
If there was an error accessing the store.
"""
return list(
map(lambda b: b.decode(), self.redis.keys(pattern=re.escape(prefix) + "*"))
)
return [b.decode() for b in self.redis.keys(pattern=re.escape(prefix) + "*")]

def iter_keys(self, prefix="") -> Iterator[str]:
"""Iterate over all keys in the store starting with prefix.
Expand Down
4 changes: 2 additions & 2 deletions minimalkv/net/_azurestore_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def map_azure_exceptions(key=None, error_codes_pass=()):
if error_code is not None and error_code in error_codes_pass:
return
if error_code == "BlobNotFound":
raise KeyError(key)
raise OSError(str(ex))
raise KeyError(key) from ex
raise OSError(str(ex)) from ex


class AzureBlockBlobStore(KeyValueStore): # noqa D
Expand Down
8 changes: 4 additions & 4 deletions minimalkv/net/_azurestore_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def map_azure_exceptions(key=None, exc_pass=()):
if ex.__class__.__name__ not in exc_pass:
s = str(ex)
if s.startswith("The specified container does not exist."):
raise OSError(s)
raise KeyError(key)
raise OSError(s) from ex
raise KeyError(key) from ex
except AzureHttpError as ex:
if ex.__class__.__name__ not in exc_pass:
raise OSError(str(ex))
raise OSError(str(ex)) from ex
except AzureException as ex:
if ex.__class__.__name__ not in exc_pass:
raise OSError(str(ex))
raise OSError(str(ex)) from ex


class AzureBlockBlobStore(KeyValueStore): # noqa D
Expand Down
Loading

0 comments on commit fe83151

Please sign in to comment.