python-filewrap 0.0.1__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.
- LICENSE +21 -0
- filewrap/__init__.py +253 -0
- filewrap/py.typed +0 -0
- python_filewrap-0.0.1.dist-info/LICENSE +21 -0
- python_filewrap-0.0.1.dist-info/METADATA +42 -0
- python_filewrap-0.0.1.dist-info/RECORD +7 -0
- python_filewrap-0.0.1.dist-info/WHEEL +4 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
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.
|
filewrap/__init__.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
+
__version__ = (0, 0, 1)
|
|
6
|
+
__all__ = [
|
|
7
|
+
"SupportsRead", "SupportsWrite", "bio_chunk_iter", "bio_chunk_async_iter",
|
|
8
|
+
"bio_skip_iter", "bio_skip_async_iter", "bio_skip_bytes", "bio_skip_bytes_async"
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
from asyncio import to_thread
|
|
12
|
+
from functools import update_wrapper
|
|
13
|
+
from inspect import isawaitable, iscoroutinefunction
|
|
14
|
+
from collections.abc import AsyncIterator, Callable, Iterator
|
|
15
|
+
from shutil import COPY_BUFSIZE # type: ignore
|
|
16
|
+
from typing import Any, Protocol, TypeVar
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
_T_co = TypeVar("_T_co", covariant=True)
|
|
20
|
+
_T_contra = TypeVar("_T_contra", contravariant=True)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class SupportsRead(Protocol[_T_co]):
|
|
24
|
+
def read(self, __length: int = ...) -> _T_co: ...
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class SupportsWrite(Protocol[_T_contra]):
|
|
28
|
+
def write(self, __s: _T_contra) -> object: ...
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def ensure_async(func, /):
|
|
32
|
+
if iscoroutinefunction(func):
|
|
33
|
+
return func
|
|
34
|
+
async def wrapper(*args, **kwds):
|
|
35
|
+
ret = to_thread(func, *args, **kwds)
|
|
36
|
+
if isawaitable(ret):
|
|
37
|
+
ret = await ret
|
|
38
|
+
return update_wrapper(wrapper, func)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def bio_chunk_iter(
|
|
42
|
+
bio: SupportsRead[bytes],
|
|
43
|
+
/,
|
|
44
|
+
size: int = -1,
|
|
45
|
+
chunksize: int = COPY_BUFSIZE,
|
|
46
|
+
callback: None | Callable[[int], Any] = None,
|
|
47
|
+
) -> Iterator[bytes]:
|
|
48
|
+
read = bio.read
|
|
49
|
+
if not callable(callback):
|
|
50
|
+
callback = None
|
|
51
|
+
if size > 0:
|
|
52
|
+
while size:
|
|
53
|
+
readsize = min(chunksize, size)
|
|
54
|
+
chunk = read(readsize)
|
|
55
|
+
length = len(chunk)
|
|
56
|
+
if callback:
|
|
57
|
+
callback(length)
|
|
58
|
+
yield chunk
|
|
59
|
+
if length < readsize:
|
|
60
|
+
break
|
|
61
|
+
size -= readsize
|
|
62
|
+
elif size < 0:
|
|
63
|
+
while (chunk := read(chunksize)):
|
|
64
|
+
if callback:
|
|
65
|
+
callback(len(chunk))
|
|
66
|
+
yield chunk
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
async def bio_chunk_async_iter(
|
|
70
|
+
bio: SupportsRead[bytes],
|
|
71
|
+
/,
|
|
72
|
+
size: int = -1,
|
|
73
|
+
chunksize: int = COPY_BUFSIZE,
|
|
74
|
+
callback: None | Callable[[int], Any] = None,
|
|
75
|
+
) -> AsyncIterator[bytes]:
|
|
76
|
+
read = ensure_async(bio.read)
|
|
77
|
+
callback = ensure_async(callback) if callable(callback) else None
|
|
78
|
+
if size > 0:
|
|
79
|
+
while size:
|
|
80
|
+
readsize = min(chunksize, size)
|
|
81
|
+
chunk = await read(readsize)
|
|
82
|
+
length = len(chunk)
|
|
83
|
+
if callback:
|
|
84
|
+
await callback(length)
|
|
85
|
+
yield chunk
|
|
86
|
+
if length < readsize:
|
|
87
|
+
break
|
|
88
|
+
size -= readsize
|
|
89
|
+
elif size < 0:
|
|
90
|
+
while (chunk := (await read(chunksize))):
|
|
91
|
+
if callback:
|
|
92
|
+
await callback(len(chunk))
|
|
93
|
+
yield chunk
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def bio_skip_iter(
|
|
97
|
+
bio: SupportsRead[bytes],
|
|
98
|
+
/,
|
|
99
|
+
size: int = -1,
|
|
100
|
+
chunksize: int = COPY_BUFSIZE,
|
|
101
|
+
callback: None | Callable[[int], Any] = None,
|
|
102
|
+
) -> Iterator[int]:
|
|
103
|
+
if size == 0:
|
|
104
|
+
return
|
|
105
|
+
if not callable(callback):
|
|
106
|
+
callback = None
|
|
107
|
+
try:
|
|
108
|
+
seek = getattr(bio, "seek")
|
|
109
|
+
curpos = seek(0, 1)
|
|
110
|
+
if size > 0:
|
|
111
|
+
length = seek(size, 1) - curpos
|
|
112
|
+
else:
|
|
113
|
+
length = seek(0, 2) - curpos
|
|
114
|
+
except Exception:
|
|
115
|
+
if chunksize <= 0:
|
|
116
|
+
chunksize = COPY_BUFSIZE
|
|
117
|
+
if hasattr(bio, "readinto"):
|
|
118
|
+
readinto = bio.readinto
|
|
119
|
+
buf = bytearray(chunksize)
|
|
120
|
+
if size > 0:
|
|
121
|
+
while size >= chunksize:
|
|
122
|
+
length = readinto(buf)
|
|
123
|
+
if callback:
|
|
124
|
+
callback(length)
|
|
125
|
+
yield length
|
|
126
|
+
if length < chunksize:
|
|
127
|
+
break
|
|
128
|
+
size -= chunksize
|
|
129
|
+
else:
|
|
130
|
+
if size:
|
|
131
|
+
del buf[size:]
|
|
132
|
+
length = readinto(buf)
|
|
133
|
+
if callback:
|
|
134
|
+
callback(length)
|
|
135
|
+
yield length
|
|
136
|
+
else:
|
|
137
|
+
while (length := readinto(buf)):
|
|
138
|
+
if callback:
|
|
139
|
+
callback(length)
|
|
140
|
+
yield length
|
|
141
|
+
else:
|
|
142
|
+
read = bio.read
|
|
143
|
+
if size > 0:
|
|
144
|
+
while size:
|
|
145
|
+
readsize = min(chunksize, size)
|
|
146
|
+
length = len(read(readsize))
|
|
147
|
+
if callback:
|
|
148
|
+
callback(length)
|
|
149
|
+
yield length
|
|
150
|
+
if length < readsize:
|
|
151
|
+
break
|
|
152
|
+
size -= readsize
|
|
153
|
+
else:
|
|
154
|
+
while (length := len(read(chunksize))):
|
|
155
|
+
if callback:
|
|
156
|
+
callback(length)
|
|
157
|
+
yield length
|
|
158
|
+
else:
|
|
159
|
+
if callback:
|
|
160
|
+
callback(length)
|
|
161
|
+
yield length
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
async def bio_skip_async_iter(
|
|
165
|
+
bio: SupportsRead[bytes],
|
|
166
|
+
/,
|
|
167
|
+
size: int = -1,
|
|
168
|
+
chunksize: int = COPY_BUFSIZE,
|
|
169
|
+
callback: None | Callable[[int], Any] = None,
|
|
170
|
+
) -> AsyncIterator[int]:
|
|
171
|
+
if size == 0:
|
|
172
|
+
return
|
|
173
|
+
callback = ensure_async(callback) if callable(callback) else None
|
|
174
|
+
try:
|
|
175
|
+
seek = ensure_async(getattr(bio, "seek"))
|
|
176
|
+
curpos = await seek(0, 1)
|
|
177
|
+
if size > 0:
|
|
178
|
+
length = (await seek(size, 1)) - curpos
|
|
179
|
+
else:
|
|
180
|
+
length = (await seek(0, 2)) - curpos
|
|
181
|
+
except Exception:
|
|
182
|
+
if chunksize <= 0:
|
|
183
|
+
chunksize = COPY_BUFSIZE
|
|
184
|
+
if hasattr(bio, "readinto"):
|
|
185
|
+
readinto = ensure_async(bio.readinto)
|
|
186
|
+
buf = bytearray(chunksize)
|
|
187
|
+
if size > 0:
|
|
188
|
+
while size >= chunksize:
|
|
189
|
+
length = await readinto(buf)
|
|
190
|
+
if callback:
|
|
191
|
+
await callback(length)
|
|
192
|
+
yield length
|
|
193
|
+
if length < chunksize:
|
|
194
|
+
break
|
|
195
|
+
size -= chunksize
|
|
196
|
+
else:
|
|
197
|
+
if size:
|
|
198
|
+
del buf[size:]
|
|
199
|
+
length = await readinto(buf)
|
|
200
|
+
if callback:
|
|
201
|
+
await callback(length)
|
|
202
|
+
yield length
|
|
203
|
+
else:
|
|
204
|
+
while (length := (await readinto(buf))):
|
|
205
|
+
if callback:
|
|
206
|
+
await callback(length)
|
|
207
|
+
yield length
|
|
208
|
+
else:
|
|
209
|
+
read = ensure_async(bio.read)
|
|
210
|
+
if size > 0:
|
|
211
|
+
while size:
|
|
212
|
+
readsize = min(chunksize, size)
|
|
213
|
+
length = len(await read(readsize))
|
|
214
|
+
if callback:
|
|
215
|
+
await callback(length)
|
|
216
|
+
yield length
|
|
217
|
+
if length < readsize:
|
|
218
|
+
break
|
|
219
|
+
size -= readsize
|
|
220
|
+
else:
|
|
221
|
+
while (length := len(await read(chunksize))):
|
|
222
|
+
if callback:
|
|
223
|
+
await callback(length)
|
|
224
|
+
yield length
|
|
225
|
+
else:
|
|
226
|
+
if callback:
|
|
227
|
+
await callback(length)
|
|
228
|
+
yield length
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def bio_skip_bytes(
|
|
232
|
+
bio: SupportsRead[bytes],
|
|
233
|
+
/,
|
|
234
|
+
size: int = -1,
|
|
235
|
+
chunksize: int = COPY_BUFSIZE,
|
|
236
|
+
callback: None | Callable[[int], Any] = None,
|
|
237
|
+
) -> SupportsRead[bytes]:
|
|
238
|
+
for _ in bio_skip_iter(bio, size, chunksize, callback=callback):
|
|
239
|
+
pass
|
|
240
|
+
return bio
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
async def bio_skip_bytes_async(
|
|
244
|
+
bio: SupportsRead[bytes],
|
|
245
|
+
/,
|
|
246
|
+
size: int = -1,
|
|
247
|
+
chunksize: int = COPY_BUFSIZE,
|
|
248
|
+
callback: None | Callable[[int], Any] = None,
|
|
249
|
+
) -> SupportsRead[bytes]:
|
|
250
|
+
async for _ in bio_skip_async_iter(bio, size, chunksize, callback=callback):
|
|
251
|
+
pass
|
|
252
|
+
return bio
|
|
253
|
+
|
filewrap/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
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.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: python-filewrap
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python file wrappers.
|
|
5
|
+
Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-filewrap
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: file,wrapper
|
|
8
|
+
Author: ChenyangGao
|
|
9
|
+
Author-email: wosiwujm@gmail.com
|
|
10
|
+
Requires-Python: >=3.10,<4.0
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Topic :: Software Development
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-filewrap
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Python file wrappers.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
You can install from [pypi](https://pypi.org/project/python-filewrap/)
|
|
32
|
+
|
|
33
|
+
```console
|
|
34
|
+
pip install -U python-filewrap
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import filewrap
|
|
41
|
+
```
|
|
42
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
2
|
+
filewrap/__init__.py,sha256=v2fyorGxZAUFOtafLNuslJXNnJ2V0Fjh8DxGN44PliQ,7621
|
|
3
|
+
filewrap/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
python_filewrap-0.0.1.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
5
|
+
python_filewrap-0.0.1.dist-info/METADATA,sha256=XyBEK0H_XINPYTQSyHRVgcw74Z0CBURuZr9JeeEt6UM,1329
|
|
6
|
+
python_filewrap-0.0.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
|
7
|
+
python_filewrap-0.0.1.dist-info/RECORD,,
|