rtzip 0.4.0__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: rtzip
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Requires-Dist: obstruct==0.5.2
5
5
  Requires-Dist: pyaes>=1.6.1
6
6
  Requires-Python: >=3.12
@@ -4,7 +4,7 @@ requires = ["uv_build < 0.13"]
4
4
 
5
5
  [project]
6
6
  name = "rtzip"
7
- version = "0.4.0"
7
+ version = "0.5.0"
8
8
  requires-python = ">=3.12"
9
9
  dependencies = [
10
10
  "obstruct==0.5.2",
@@ -6,7 +6,7 @@ requires = [
6
6
 
7
7
  [project]
8
8
  name = "rtzip"
9
- version = "0.4.0"
9
+ version = "0.5.0"
10
10
  requires-python = ">=3.12"
11
11
  dependencies = [
12
12
  "obstruct==0.5.2",
@@ -0,0 +1,75 @@
1
+ from typing import AsyncGenerator, Any, Protocol, runtime_checkable
2
+
3
+ from .errors import UnsupportedAlgorithmError
4
+ from .models import CDEntry, LocalFileHeader
5
+
6
+ type IndexAcceptedTypes = bytes | bytearray | memoryview
7
+
8
+
9
+ @runtime_checkable
10
+ class AlgorithmHandler(Protocol):
11
+ def __call__(
12
+ self,
13
+ raw_data: AsyncGenerator[IndexAcceptedTypes, None],
14
+ entry: CDEntry,
15
+ header: LocalFileHeader,
16
+ context: dict[str, Any],
17
+ /
18
+ ) -> AsyncGenerator[IndexAcceptedTypes, None]:
19
+ """
20
+ 压缩算法处理器
21
+
22
+ :param raw_data: 原始数据流(异步生成器)
23
+ :param entry: 当前文件的 CDEntry 对象
24
+ :param header: 当前文件的 LocalFileHeader 对象(已经过文件描述符修补)
25
+ :param context: 上下文信息
26
+ :return:
27
+ """
28
+ ...
29
+
30
+
31
+ algorithm_mapping: dict[int, AlgorithmHandler] = {}
32
+
33
+
34
+ def algorithm_handler(algorithm: int, override=False):
35
+ """
36
+ 压缩流包装器注册装饰器
37
+
38
+ :param algorithm: 压缩算法 ID
39
+ :param override: 如果你希望覆盖一个已注册的包装器,请设置为 True,否则将会阻止可能的覆盖行为
40
+ :return:
41
+ """
42
+ if not override and algorithm in algorithm_mapping:
43
+ raise ValueError(f"Algorithm {algorithm} is already registered")
44
+
45
+ def decorator(func: AlgorithmHandler):
46
+ algorithm_mapping[algorithm] = func
47
+ return func
48
+
49
+ return decorator
50
+
51
+
52
+ def get_algorithm_handler(algorithm: int) -> AlgorithmHandler:
53
+ """
54
+ 获取包装器函数
55
+
56
+ :param algorithm: 压缩算法 ID
57
+ :return: AlgorithmHandler 可调用对象,接受一个异步生成器,返回一个异步生成器
58
+ """
59
+ try:
60
+ return algorithm_mapping[algorithm]
61
+ except KeyError:
62
+ raise UnsupportedAlgorithmError(algorithm)
63
+
64
+
65
+ def get_algorithm_handler_safe(algorithm: int) -> AlgorithmHandler | None:
66
+ return algorithm_mapping.get(algorithm)
67
+
68
+ def is_algorithm_handlier_available(algorithm: int) -> bool:
69
+ """
70
+ 判断某个压缩算法是否已有注册的包装器
71
+
72
+ :param algorithm: 压缩算法 ID
73
+ :return:
74
+ """
75
+ return algorithm in algorithm_mapping
@@ -1,9 +1,9 @@
1
1
  import struct
2
2
  from collections.abc import Buffer
3
3
  from fnmatch import fnmatch
4
- from typing import AsyncGenerator
4
+ from typing import AsyncGenerator, Any
5
5
 
6
- from .algorithm_register import is_algorithm_available, get_algorithm_handler
6
+ from .algorithm_register import is_algorithm_handlier_available, get_algorithm_handler
7
7
  from .const import *
8
8
  from .errors import UnsupportedAlgorithmError
9
9
  from .models import EOCD, Zip64EOCD, CDEntry, LocalFileHeader
@@ -66,11 +66,11 @@ class RemoteZip:
66
66
  return await self.source.get_total_size()
67
67
 
68
68
  __slots__ = [
69
- 'source', 'zip_info', 'files', 'file_mapping', 'cached_headers', 'is_zip64',
69
+ 'source', 'zip_info', 'files', 'file_mapping', 'cached_headers', 'is_zip64', 'context',
70
70
  '_namelist'
71
71
  ]
72
72
 
73
- def __init__(self, source: DataSource):
73
+ def __init__(self, source: DataSource, **kwargs):
74
74
  self.source = source
75
75
 
76
76
  self.zip_info: EOCD | Zip64EOCD | None = None
@@ -80,8 +80,15 @@ class RemoteZip:
80
80
  self.cached_headers: dict[bytes, LocalFileHeader] = {}
81
81
 
82
82
  self.is_zip64 = False
83
+ self.context = kwargs
83
84
  self._namelist = None
84
85
 
86
+ def _merge_context(self, overrides: dict[str, Any]):
87
+ return {
88
+ **self.context,
89
+ **overrides
90
+ }
91
+
85
92
  def namelist(self):
86
93
  if self._namelist is None:
87
94
  if self.files is None:
@@ -342,9 +349,11 @@ class RemoteZip:
342
349
  raise FileNotFoundError(filename)
343
350
 
344
351
  async def _stream_single_file(self, filename: bytes, kwargs):
352
+ kwargs = self._merge_context(kwargs)
353
+
345
354
  entry = self.find_file_entry(filename)
346
355
 
347
- if not is_algorithm_available(entry.algorithm):
356
+ if not is_algorithm_handlier_available(entry.algorithm):
348
357
  raise UnsupportedAlgorithmError(entry.algorithm)
349
358
 
350
359
  header, buffer = await self.fetch_file_header(entry=entry)
@@ -396,6 +405,7 @@ class RemoteZip:
396
405
 
397
406
 
398
407
  async def resolve(self, filename: bytes, max_resolve=24, **kwargs) -> bytes:
408
+ kwargs = self._merge_context(kwargs)
399
409
  entry = self.find_file_entry(filename)
400
410
  target = entry.filename
401
411
 
@@ -440,8 +450,8 @@ class RemoteZip:
440
450
  filename=normpath(path)
441
451
  )
442
452
 
443
- async def read_text(self, path: bytes, encoding='utf-8', errors='strict') -> str:
444
- return (await self.read(path)).decode(encoding, errors=errors)
453
+ async def read_text(self, path: bytes, encoding='utf-8', errors='strict', **kwargs) -> str:
454
+ return (await self.read(path, **kwargs)).decode(encoding, errors=errors)
445
455
 
446
456
  def rglob(self, pattern):
447
457
  for file in self._namelist:
@@ -6,7 +6,7 @@ from typing import Protocol
6
6
  from obstruct import Struct, u16, u8
7
7
 
8
8
  from .errors import UnsupportedAlgorithmError, WrongPasswordError
9
- from .algorithm_register import algorithm_handler, is_algorithm_available, get_algorithm_handler
9
+ from .algorithm_register import algorithm_handler, is_algorithm_handlier_available, get_algorithm_handler
10
10
 
11
11
 
12
12
  @dataclasses.dataclass(slots=True)
@@ -278,7 +278,7 @@ def zip_aes_wrapper(raw_data, entry, header, ctx):
278
278
  # 获取解密后的压缩格式
279
279
  algorithm = aes_extra.compression
280
280
 
281
- if not is_algorithm_available(algorithm):
281
+ if not is_algorithm_handlier_available(algorithm):
282
282
  raise UnsupportedAlgorithmError(algorithm)
283
283
 
284
284
  return get_algorithm_handler(algorithm)(
@@ -290,4 +290,4 @@ def zip_aes_wrapper(raw_data, entry, header, ctx):
290
290
  def install(pbkdf2_hmac: PBKDF2_HMAC_FunctionType, aes_cipher: type[AESDecryptor]):
291
291
  pbkdf2_hmac_implement(pbkdf2_hmac)
292
292
  aes_cipher_implement(aes_cipher)
293
- algorithm_handler(0x0063)(zip_aes_wrapper) if not is_algorithm_available(0x0063) else None
293
+ algorithm_handler(0x0063)(zip_aes_wrapper) if not is_algorithm_handlier_available(0x0063) else None
@@ -1,46 +0,0 @@
1
- from collections.abc import Buffer
2
- from typing import Callable, AsyncGenerator, Any
3
-
4
- from .models import CDEntry, LocalFileHeader
5
-
6
- AlgorithmHandler = Callable[[AsyncGenerator[Buffer, None], CDEntry, LocalFileHeader, dict[str, Any]], AsyncGenerator[bytes | bytearray | memoryview, None]]
7
-
8
- algorithm_mapping: dict[int, AlgorithmHandler] = {}
9
-
10
-
11
- def algorithm_handler(algorithm: int, override=False):
12
- """
13
- 压缩流包装器注册装饰器
14
-
15
- :param algorithm: 压缩算法 ID
16
- :param override: 如果你希望覆盖一个已注册的包装器,请设置为 True,否则将会阻止可能的覆盖行为
17
- :return:
18
- """
19
- if not override and algorithm in algorithm_mapping:
20
- raise ValueError(f"Algorithm {algorithm} is already registered")
21
-
22
- def decorator(func: AlgorithmHandler):
23
- algorithm_mapping[algorithm] = func
24
- return func
25
-
26
- return decorator
27
-
28
-
29
- def get_algorithm_handler(algorithm: int) -> AlgorithmHandler:
30
- """
31
- 获取包装器函数
32
-
33
- :param algorithm: 压缩算法 ID
34
- :return: AlgorithmHandler 可调用对象,接受一个异步生成器,返回一个异步生成器
35
- """
36
- return algorithm_mapping[algorithm]
37
-
38
-
39
- def is_algorithm_available(algorithm: int) -> bool:
40
- """
41
- 判断某个压缩算法是否已有注册的包装器
42
-
43
- :param algorithm: 压缩算法 ID
44
- :return:
45
- """
46
- return algorithm in algorithm_mapping
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes