zipindex 1.0.0__py3-none-any.whl
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.
- zipindex/__init__.py +2 -0
- zipindex/indexer.py +110 -0
- zipindex-1.0.0.dist-info/METADATA +58 -0
- zipindex-1.0.0.dist-info/RECORD +6 -0
- zipindex-1.0.0.dist-info/WHEEL +4 -0
- zipindex-1.0.0.dist-info/licenses/LICENSE +21 -0
zipindex/__init__.py
ADDED
zipindex/indexer.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Simple addition to zipfile options that allows for intuitive control over
|
|
4
|
+
zipfile internal uses
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import zipfile
|
|
9
|
+
from typing import Self, Iterable
|
|
10
|
+
import os
|
|
11
|
+
from functools import partial
|
|
12
|
+
import re
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def member_endswith(member: zipfile.ZipInfo,
|
|
16
|
+
match_key: str) -> bool:
|
|
17
|
+
return member.filename.endswith(match_key)
|
|
18
|
+
|
|
19
|
+
class ZipIndex:
|
|
20
|
+
|
|
21
|
+
def __repr__(self) -> str:
|
|
22
|
+
return (f"ZipIndex(zipfile_path={self.zipfile_path}, "
|
|
23
|
+
f"member_name={self.member_name})")
|
|
24
|
+
|
|
25
|
+
def __init__(self,
|
|
26
|
+
zipfile_path: str,
|
|
27
|
+
member_name: str) -> Self:
|
|
28
|
+
|
|
29
|
+
self.zipfile_path = zipfile_path
|
|
30
|
+
self.member_name = member_name
|
|
31
|
+
|
|
32
|
+
def open(self, mode:str = 'r') -> os.PathLike:
|
|
33
|
+
self.zf = zipfile.ZipFile(self.zipfile_path, mode)
|
|
34
|
+
matched_file = max(self.zf.filelist,
|
|
35
|
+
key=partial(member_endswith,
|
|
36
|
+
match_key=self.member_name))
|
|
37
|
+
self.fid = self.zf.open(matched_file, mode)
|
|
38
|
+
return self.fid
|
|
39
|
+
|
|
40
|
+
def suffix(self):
|
|
41
|
+
m = re.match(r"^.*(\.\w+)$", self.member_name)
|
|
42
|
+
if m:
|
|
43
|
+
return m.group(1)
|
|
44
|
+
return ""
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def __enter__(self) -> os.PathLike:
|
|
48
|
+
return self.open()
|
|
49
|
+
|
|
50
|
+
def __exit__(self, etype, e, tb) -> None:
|
|
51
|
+
self.fid.close()
|
|
52
|
+
self.zf.close()
|
|
53
|
+
return None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def factory(cls, zipfile_path: str) -> dict[str, Self]:
|
|
58
|
+
with zipfile.ZipFile(zipfile_path, 'r') as zf:
|
|
59
|
+
output = {
|
|
60
|
+
member.filename : cls(zipfile_path,
|
|
61
|
+
member.filename.split("/")[-1])
|
|
62
|
+
for member in zf.filelist
|
|
63
|
+
if not member.filename.endswith("/")}
|
|
64
|
+
return output
|
|
65
|
+
|
|
66
|
+
@classmethod
|
|
67
|
+
def categorical_factory(cls,
|
|
68
|
+
zipfile_path: str,
|
|
69
|
+
categories: dict[str, Iterable[dict[int, bytes]]]) -> dict[str, tuple[Self]]:
|
|
70
|
+
"""
|
|
71
|
+
Provides a factory for categorizing internal files in a zipfile.ZipFile
|
|
72
|
+
Must match all conditions of at least one category to be included in
|
|
73
|
+
the output.
|
|
74
|
+
|
|
75
|
+
Parameters
|
|
76
|
+
----------
|
|
77
|
+
zipfile_path : str
|
|
78
|
+
Path to the zipfile.
|
|
79
|
+
categories : dict[str, Iterable[dict[int, bytes]]]
|
|
80
|
+
Dictionary definition of bytes-wise matching for type detection.
|
|
81
|
+
Ex: {"empty" : [{0: b""}]}
|
|
82
|
+
Returns
|
|
83
|
+
-------
|
|
84
|
+
dict[str, tuple[Self]]
|
|
85
|
+
{category : tuple[ZipIndex]}.
|
|
86
|
+
|
|
87
|
+
"""
|
|
88
|
+
assert categories
|
|
89
|
+
output = {cat: [] for cat in categories}
|
|
90
|
+
with zipfile.ZipFile(zipfile_path, 'r') as zf:
|
|
91
|
+
for category, definition in categories.items():
|
|
92
|
+
for member in zf.filelist:
|
|
93
|
+
if member.filename.endswith("/"): continue
|
|
94
|
+
with zf.open(member, 'r') as fid:
|
|
95
|
+
matched = []
|
|
96
|
+
for position, condition in definition.items():
|
|
97
|
+
fid.seek(position, 0)
|
|
98
|
+
if len(condition) == 0:
|
|
99
|
+
b = fid.read()
|
|
100
|
+
else:
|
|
101
|
+
b = fid.read(len(condition))
|
|
102
|
+
matched.append((b == condition))
|
|
103
|
+
if all(matched):
|
|
104
|
+
output[category].append(cls(zipfile_path,
|
|
105
|
+
member.filename.split("/")[-1]))
|
|
106
|
+
return output
|
|
107
|
+
|
|
108
|
+
idx = ZipIndex.categorical_factory(zipfile_path='C:/dev/data.zip',
|
|
109
|
+
categories={"empty": {0: b""},
|
|
110
|
+
"bin" : {0: b"BIN"}})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: zipindex
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Simple indexing for working with files inside zipfiles
|
|
5
|
+
Project-URL: homepage, https://www.github.com/ResoluteVinculum/ZipIndex
|
|
6
|
+
Project-URL: issues, https://www.github.com/ResoluteVinculum/ZipIndex/issues
|
|
7
|
+
Author-email: Trent Oberlander <trentoberlander@gmail.com>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Trent Oberlander
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Keywords: files
|
|
31
|
+
Classifier: Operating System :: OS Independent
|
|
32
|
+
Classifier: Programming Language :: Python :: 3
|
|
33
|
+
Requires-Python: >=3.11
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# ZipIndex
|
|
37
|
+
|
|
38
|
+
Simple, pure-python object package for dealing with many files inside of
|
|
39
|
+
zipfiles.
|
|
40
|
+
|
|
41
|
+
# Example Usage
|
|
42
|
+
```python
|
|
43
|
+
from zipindex import ZipIndex
|
|
44
|
+
|
|
45
|
+
# Single Instance
|
|
46
|
+
inst = ZipIndex(zipfile_path="...", member_name="...")
|
|
47
|
+
|
|
48
|
+
# Simple Factory
|
|
49
|
+
members: dict[str, ZipIndex] = ZipIndex.factory(zipfile_path="...")
|
|
50
|
+
|
|
51
|
+
# Categorical Factory
|
|
52
|
+
categories: dict[str, ZipIndex] = ZipIndex.categorical_factory(zipfile_path="...", categories={"empty" : {0 : b""}})
|
|
53
|
+
|
|
54
|
+
# Use
|
|
55
|
+
with inst.open() as fid:
|
|
56
|
+
data = fid.read()
|
|
57
|
+
|
|
58
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
zipindex/__init__.py,sha256=za1If6M-9AVycP5yPdiUiDSKELA6nx_aZs_KdndJ7F4,40
|
|
2
|
+
zipindex/indexer.py,sha256=YgrYkDYlIPA_CnA4TdLkHUcGedytwOOPRwRxd4A2i6o,3941
|
|
3
|
+
zipindex-1.0.0.dist-info/METADATA,sha256=ieswrPWwsX2MS-4iQSqHUqLK7csD0fBmm3WVi5v2B20,2274
|
|
4
|
+
zipindex-1.0.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
5
|
+
zipindex-1.0.0.dist-info/licenses/LICENSE,sha256=lKS5iiOkJreeA4cZQSpQ2mlWqz0k29HaYoWDt7c7xgE,1094
|
|
6
|
+
zipindex-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Trent Oberlander
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|