Skip to content

Commit 02f46b2

Browse files
committed
refactor: support ZipFile in read_manifest_from_zip
1 parent 19af277 commit 02f46b2

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

questionpy_server/utils/manifest.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import asyncio
66
from asyncio import to_thread
7+
from contextlib import ExitStack
78
from pathlib import Path
89
from typing import IO, Annotated, Any
910
from zipfile import BadZipFile, ZipFile
@@ -48,9 +49,14 @@ def _read_manifest_from_path_sync(manifest_path: Path) -> Manifest:
4849
raise ManifestError(msg) from e
4950

5051

51-
def _read_manifest_from_zip_sync(package_path: Path) -> Manifest:
52+
def _read_manifest_from_zip_sync(package: Path | ZipFile) -> Manifest:
5253
try:
53-
with ZipFile(package_path) as zip_file, zip_file.open(f"{DIST_DIR}/{MANIFEST_FILENAME}") as manifest_file:
54+
with ExitStack() as stack:
55+
if isinstance(package, Path):
56+
package = stack.enter_context(ZipFile(package))
57+
58+
manifest_file = stack.enter_context(package.open(f"{DIST_DIR}/{MANIFEST_FILENAME}"))
59+
5460
return _read_manifest_from_file_sync(manifest_file)
5561
except BadZipFile as e:
5662
msg = f"Could not read manifest from package: {e}"
@@ -61,13 +67,13 @@ def _read_manifest_from_zip_sync(package_path: Path) -> Manifest:
6167
raise ManifestError(msg) from e
6268

6369

64-
async def read_manifest_from_zip(package_path: Path) -> Manifest:
70+
async def read_manifest_from_zip(package: Path | ZipFile) -> Manifest:
6571
"""Reads the manifest from a zipped package.
6672
6773
Raises:
68-
ManifestError: if the manifest could not be read, is too large, or is invalid
74+
ManifestError: if the manifest could not be read, it is too large or is invalid
6975
"""
70-
return await asyncio.to_thread(_read_manifest_from_zip_sync, package_path)
76+
return await asyncio.to_thread(_read_manifest_from_zip_sync, package)
7177

7278

7379
async def read_manifest_from_location(location: PackageLocation) -> Manifest:

0 commit comments

Comments
 (0)