-
Notifications
You must be signed in to change notification settings - Fork 5
use py7zr instead of pylzma #8
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| [mypy] | ||
| warn_unused_configs = True | ||
|
|
||
| [mypy-py7zlib] | ||
| [mypy-py7zr] | ||
| ignore_missing_imports = True |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,71 @@ | ||
| from io import BytesIO | ||
| from typing import Optional | ||
| from py7zlib import Archive7z, ArchiveError | ||
| from typing import Optional, Dict | ||
| import threading | ||
| import py7zr | ||
|
|
||
| NAPI_ARCHIVE_PASSWORD = "iBlm8NTigvru0Jr0" | ||
|
|
||
|
|
||
| def un7zip_api_response(content_7z: bytes) -> Optional[bytes]: | ||
| class InMemoryIO(py7zr.io.Py7zIO): | ||
| def __init__(self, fname: str): | ||
| self.fname = fname | ||
| self._buf = bytearray() | ||
| self._length = 0 | ||
| self._lock = threading.Lock() | ||
|
|
||
| def write(self, data: bytes) -> None: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. linter complains about returning type I think there's a bit more work to make this change working with tests passing etc, i.e.
The implementation itself makes sense, so there's just some boilerplate needed around python tooling. I may help you with it or base off of your changes and do it on my own but again, my spare time is super limited so can't promise doing it any time soon
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I mentioned when reporting the issue, I am not a Python expert. I admit that I did not perform exhaustive testing. I built a .whl package based on my commits and installed it locally using vevn. I have been using it for a few days instead of the old version, and everything seems to be working fine. |
||
| with self._lock: | ||
| self._buf.extend(data) | ||
| self._length += len(data) | ||
|
|
||
| def read(self, size: Optional[int] = None) -> bytes: | ||
| return b"" | ||
|
|
||
| def seek(self, offset: int, whence: int = 0) -> int: | ||
| return offset | ||
|
|
||
| def flush(self) -> None: | ||
| pass | ||
|
|
||
| def size(self) -> int: | ||
| return self._length | ||
|
|
||
| def getvalue(self) -> bytes: | ||
| with self._lock: | ||
| return bytes(self._buf) | ||
|
|
||
|
|
||
| class InMemoryFactory(py7zr.io.WriterFactory): | ||
| def __init__(self, target_filename: Optional[str] = None): | ||
| self.products: Dict[str, InMemoryIO] = {} | ||
| self.target_filename = target_filename | ||
|
|
||
| def create(self, filename: str) -> py7zr.io.Py7zIO: | ||
| if self.target_filename is not None and filename != self.target_filename: | ||
| product = InMemoryIO(filename) | ||
| else: | ||
| product = InMemoryIO(filename) | ||
| self.products[filename] = product | ||
| return product | ||
|
|
||
|
|
||
| def un7zip_api_response(content_7z: bytes, target_filename: Optional[str] = None) -> Optional[bytes]: | ||
| try: | ||
| buffer = BytesIO(content_7z) | ||
| archive = Archive7z(buffer, password=NAPI_ARCHIVE_PASSWORD) | ||
| return archive.getmember(0).read() | ||
| except ArchiveError: | ||
| with py7zr.SevenZipFile(buffer, mode="r", password=NAPI_ARCHIVE_PASSWORD) as archive: | ||
| factory = InMemoryFactory(target_filename=target_filename) | ||
| archive.extractall(factory=factory) | ||
|
|
||
| if target_filename: | ||
| product = factory.products.get(target_filename) | ||
| return product.getvalue() if product else None | ||
|
|
||
| if not factory.products: | ||
| return None | ||
| first_product = next(iter(factory.products.values())) | ||
| return first_product.getvalue() | ||
|
|
||
| except (py7zr.exceptions.Bad7zFile, | ||
| py7zr.exceptions.PasswordRequired, | ||
| py7zr.exceptions.UnsupportedCompressionMethodError): | ||
| return None | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [tool.poetry] | ||
| name = "napi-py" | ||
| version = "1.2.4" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if dropping the support is required, I think it should be 1.3.0
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that makes sense, and you're right. |
||
| version = "1.2.5" | ||
| description = "CLI tool for downloading subtitles from napiprojekt.pl" | ||
| authors = ["emkor93 <emkor93@gmail.com>"] | ||
| license = "GPL-3.0 License" | ||
|
|
@@ -11,11 +11,11 @@ keywords = ["napiprojekt", "subs", "subtitles", "movie", "film"] | |
| packages = [{ include = "napi" }] | ||
|
|
||
| [tool.poetry.dependencies] | ||
| python = "^3.7" | ||
| pylzma = "^0.5.0" | ||
| python = "^3.9" | ||
| py7zr = "^1.0.0" | ||
| chardet = "^5.2.0" | ||
|
|
||
| [tool.poetry.dev-dependencies] | ||
| [tool.poetry.group.dev.dependencies] | ||
| mypy = "^1.4.0" | ||
| pytest = "^7.4.4" | ||
| black = "^22.3.0" | ||
|
|
@@ -29,4 +29,4 @@ build-backend = "poetry.core.masonry.api" | |
|
|
||
| [tool.black] | ||
| line-length = 120 | ||
| target-version = ['py37', 'py38', 'py39', 'py310'] | ||
| target-version = ['py39', 'py310'] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it needed to drop support for 3.7 and 3.8?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid so, because for py7zr version 1.0.0, the minimum required version is Python 3.9, and the maximum is Python 3.13 (planned support for 3.14).