Skip to content

Commit

Permalink
Upgrade to Ruff 0.7 (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Oct 20, 2024
1 parent 1213805 commit f649457
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 47 deletions.
40 changes: 19 additions & 21 deletions betty/gramps/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import re
import tarfile
from collections import defaultdict
from contextlib import suppress
from contextlib import suppress, ExitStack
from dataclasses import dataclass
from enum import Enum
from logging import getLogger
from pathlib import Path
from typing import Iterable, Any, IO, cast, TYPE_CHECKING, TypeVar, Generic, final
from typing import Iterable, Any, cast, TYPE_CHECKING, TypeVar, Generic, final
from xml.etree import ElementTree

import aiofiles
Expand Down Expand Up @@ -251,26 +251,24 @@ async def load_gpkg(self, gpkg_path: Path) -> None:
:raises betty.gramps.error.GrampsError:
"""
gpkg_path = gpkg_path.resolve()
try:
tar_file: IO[bytes] = gzip.open(gpkg_path) # type: ignore[assignment]
except FileNotFoundError:
raise GrampsFileNotFound.new(gpkg_path) from None
else:
async with TemporaryDirectory() as cache_directory_path_str:
try:
tarfile.open(
fileobj=tar_file,
).extractall(cache_directory_path_str, filter="data")
except (OSError, tarfile.ReadError) as error:
raise UserFacingGrampsError(
_(
"Could not extract {file_path} as a gzipped tar file (*.tar.gz)."
).format(file_path=str(gpkg_path))
) from error
else:
await self.load_gramps(
Path(cache_directory_path_str) / "data.gramps"
with ExitStack() as stack:
try:
tar_file = stack.enter_context(
tarfile.open( # noqa SIM115
name=gpkg_path, mode="r:gz"
)
)
except FileNotFoundError:
raise GrampsFileNotFound.new(gpkg_path) from None
except (OSError, tarfile.ReadError) as error:
raise UserFacingGrampsError(
_(
"Could not extract {file_path} as a gzipped tar file (*.tar.gz)."
).format(file_path=str(gpkg_path))
) from error
async with TemporaryDirectory() as cache_directory_path_str:
tar_file.extractall(cache_directory_path_str, filter="data")
await self.load_gramps(Path(cache_directory_path_str) / "data.gramps")

async def load_xml(self, xml: str, gramps_tree_directory_path: Path) -> None:
"""
Expand Down
32 changes: 16 additions & 16 deletions betty/license/licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,23 @@ async def _load_licenses(self) -> None:
def _extract_licenses(
cls, spdx_licenses_data_path: Path, cache_directory_path: Path
):
tar_file = tarfile.open(spdx_licenses_data_path, "r:gz")
tar_file.extractall(
cache_directory_path,
members=[
tar_file.getmember(
f"license-list-data-{cls.SPDX_VERSION}/json/licenses.json"
),
*[
tar_info
for tar_info in tar_file.getmembers()
if tar_info.name.startswith(
f"license-list-data-{cls.SPDX_VERSION}/json/details/"
)
with tarfile.open(spdx_licenses_data_path, "r:gz") as tar_file:
tar_file.extractall(
cache_directory_path,
members=[
tar_file.getmember(
f"license-list-data-{cls.SPDX_VERSION}/json/licenses.json"
),
*[
tar_info
for tar_info in tar_file.getmembers()
if tar_info.name.startswith(
f"license-list-data-{cls.SPDX_VERSION}/json/details/"
)
],
],
],
filter="data",
)
filter="data",
)

async def _load_license(self, license_id: MachineName) -> type[License]:
await self._load_licenses()
Expand Down
5 changes: 2 additions & 3 deletions betty/test_utils/project/extension/demo/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ def demo_project_fetcher(binary_file_cache: BinaryFileCache, tmp_path: Path) ->
f.write(dumps(license_data))

spdx_tar_file_path = tmp_path / "spdx.tar.gz"
spdx_tar_file = tarfile.open(spdx_tar_file_path, "w:gz")
spdx_tar_file.add(spdx_directory_path, "/")
spdx_tar_file.close()
with tarfile.open(spdx_tar_file_path, "w:gz") as spdx_tar_file:
spdx_tar_file.add(spdx_directory_path, "/")
fetcher = StaticFetcher(
fetch_file_map={SpdxLicenseRepository.URL: spdx_tar_file_path}
)
Expand Down
10 changes: 4 additions & 6 deletions betty/tests/license/test_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ def sut_without_licenses(
with open(licenses_file_path, "w") as f:
f.write(dumps(licenses_data))
spdx_tar_file_path = tmp_path / "spdx.tar.gz"
spdx_tar_file = tarfile.open(spdx_tar_file_path, "w:gz")
spdx_tar_file.add(spdx_directory_path, "/")
spdx_tar_file.close()
with tarfile.open(spdx_tar_file_path, "w:gz") as spdx_tar_file:
spdx_tar_file.add(spdx_directory_path, "/")
fetcher = StaticFetcher(
fetch_file_map={SpdxLicenseRepository.URL: spdx_tar_file_path}
)
Expand Down Expand Up @@ -176,9 +175,8 @@ def sut_with_licenses(
with open(license_file_path, "w") as f:
f.write(dumps(license_data))
spdx_tar_file_path = tmp_path / "spdx.tar.gz"
spdx_tar_file = tarfile.open(spdx_tar_file_path, "w:gz")
spdx_tar_file.add(spdx_directory_path, "/")
spdx_tar_file.close()
with tarfile.open(spdx_tar_file_path, "w:gz") as spdx_tar_file:
spdx_tar_file.add(spdx_directory_path, "/")
fetcher = StaticFetcher(
fetch_file_map={SpdxLicenseRepository.URL: spdx_tar_file_path}
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ test = [
'pytest-aioresponses ~= 0.2',
'pytest-asyncio ~= 0.24',
'pytest-mock ~= 3.14',
'ruff ~= 0.6.4',
'ruff ~= 0.7',
'types-aiofiles ~= 24.1',
'types-babel ~= 2.11',
'types-click ~= 7.1',
Expand Down

0 comments on commit f649457

Please sign in to comment.