python-filewrap 0.0.1__py3-none-any.whl → 0.0.2.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.
filewrap/__init__.py CHANGED
@@ -2,13 +2,14 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 1)
5
+ __version__ = (0, 0, 2)
6
6
  __all__ = [
7
7
  "SupportsRead", "SupportsWrite", "bio_chunk_iter", "bio_chunk_async_iter",
8
8
  "bio_skip_iter", "bio_skip_async_iter", "bio_skip_bytes", "bio_skip_bytes_async"
9
9
  ]
10
10
 
11
11
  from asyncio import to_thread
12
+ from collections.abc import Awaitable
12
13
  from functools import update_wrapper
13
14
  from inspect import isawaitable, iscoroutinefunction
14
15
  from collections.abc import AsyncIterator, Callable, Iterator
@@ -35,17 +36,21 @@ def ensure_async(func, /):
35
36
  ret = to_thread(func, *args, **kwds)
36
37
  if isawaitable(ret):
37
38
  ret = await ret
39
+ return ret
38
40
  return update_wrapper(wrapper, func)
39
41
 
40
42
 
41
43
  def bio_chunk_iter(
42
- bio: SupportsRead[bytes],
44
+ bio: SupportsRead[bytes] | Callable[[int], bytes],
43
45
  /,
44
46
  size: int = -1,
45
47
  chunksize: int = COPY_BUFSIZE,
46
48
  callback: None | Callable[[int], Any] = None,
47
49
  ) -> Iterator[bytes]:
48
- read = bio.read
50
+ if callable(bio):
51
+ read = bio
52
+ else:
53
+ read = bio.read
49
54
  if not callable(callback):
50
55
  callback = None
51
56
  if size > 0:
@@ -67,13 +72,16 @@ def bio_chunk_iter(
67
72
 
68
73
 
69
74
  async def bio_chunk_async_iter(
70
- bio: SupportsRead[bytes],
75
+ bio: SupportsRead[bytes] | Callable[[int], bytes | Awaitable[bytes]],
71
76
  /,
72
77
  size: int = -1,
73
78
  chunksize: int = COPY_BUFSIZE,
74
79
  callback: None | Callable[[int], Any] = None,
75
80
  ) -> AsyncIterator[bytes]:
76
- read = ensure_async(bio.read)
81
+ if callable(bio):
82
+ read = ensure_async(bio)
83
+ else:
84
+ read = ensure_async(bio.read)
77
85
  callback = ensure_async(callback) if callable(callback) else None
78
86
  if size > 0:
79
87
  while size:
@@ -94,7 +102,7 @@ async def bio_chunk_async_iter(
94
102
 
95
103
 
96
104
  def bio_skip_iter(
97
- bio: SupportsRead[bytes],
105
+ bio: SupportsRead[bytes] | Callable[[int], bytes],
98
106
  /,
99
107
  size: int = -1,
100
108
  chunksize: int = COPY_BUFSIZE,
@@ -114,7 +122,9 @@ def bio_skip_iter(
114
122
  except Exception:
115
123
  if chunksize <= 0:
116
124
  chunksize = COPY_BUFSIZE
117
- if hasattr(bio, "readinto"):
125
+ if callable(bio):
126
+ read = bio
127
+ elif hasattr(bio, "readinto"):
118
128
  readinto = bio.readinto
119
129
  buf = bytearray(chunksize)
120
130
  if size > 0:
@@ -138,23 +148,24 @@ def bio_skip_iter(
138
148
  if callback:
139
149
  callback(length)
140
150
  yield length
151
+ return
141
152
  else:
142
153
  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
154
+ if size > 0:
155
+ while size:
156
+ readsize = min(chunksize, size)
157
+ length = len(read(readsize))
158
+ if callback:
159
+ callback(length)
160
+ yield length
161
+ if length < readsize:
162
+ break
163
+ size -= readsize
164
+ else:
165
+ while (length := len(read(chunksize))):
166
+ if callback:
167
+ callback(length)
168
+ yield length
158
169
  else:
159
170
  if callback:
160
171
  callback(length)
@@ -162,7 +173,7 @@ def bio_skip_iter(
162
173
 
163
174
 
164
175
  async def bio_skip_async_iter(
165
- bio: SupportsRead[bytes],
176
+ bio: SupportsRead[bytes] | Callable[[int], bytes | Awaitable[bytes]],
166
177
  /,
167
178
  size: int = -1,
168
179
  chunksize: int = COPY_BUFSIZE,
@@ -181,7 +192,9 @@ async def bio_skip_async_iter(
181
192
  except Exception:
182
193
  if chunksize <= 0:
183
194
  chunksize = COPY_BUFSIZE
184
- if hasattr(bio, "readinto"):
195
+ if callable(bio):
196
+ read = ensure_async(bio)
197
+ elif hasattr(bio, "readinto"):
185
198
  readinto = ensure_async(bio.readinto)
186
199
  buf = bytearray(chunksize)
187
200
  if size > 0:
@@ -207,21 +220,21 @@ async def bio_skip_async_iter(
207
220
  yield length
208
221
  else:
209
222
  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
223
+ if size > 0:
224
+ while size:
225
+ readsize = min(chunksize, size)
226
+ length = len(await read(readsize))
227
+ if callback:
228
+ await callback(length)
229
+ yield length
230
+ if length < readsize:
231
+ break
232
+ size -= readsize
233
+ else:
234
+ while (length := len(await read(chunksize))):
235
+ if callback:
236
+ await callback(length)
237
+ yield length
225
238
  else:
226
239
  if callback:
227
240
  await callback(length)
@@ -229,25 +242,23 @@ async def bio_skip_async_iter(
229
242
 
230
243
 
231
244
  def bio_skip_bytes(
232
- bio: SupportsRead[bytes],
245
+ bio: SupportsRead[bytes] | Callable[[int], bytes],
233
246
  /,
234
247
  size: int = -1,
235
248
  chunksize: int = COPY_BUFSIZE,
236
249
  callback: None | Callable[[int], Any] = None,
237
- ) -> SupportsRead[bytes]:
250
+ ):
238
251
  for _ in bio_skip_iter(bio, size, chunksize, callback=callback):
239
252
  pass
240
- return bio
241
253
 
242
254
 
243
255
  async def bio_skip_bytes_async(
244
- bio: SupportsRead[bytes],
256
+ bio: SupportsRead[bytes] | Callable[[int], bytes | Awaitable[bytes]],
245
257
  /,
246
258
  size: int = -1,
247
259
  chunksize: int = COPY_BUFSIZE,
248
260
  callback: None | Callable[[int], Any] = None,
249
- ) -> SupportsRead[bytes]:
261
+ ):
250
262
  async for _ in bio_skip_async_iter(bio, size, chunksize, callback=callback):
251
263
  pass
252
- return bio
253
264
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-filewrap
3
- Version: 0.0.1
3
+ Version: 0.0.2.1
4
4
  Summary: Python file wrappers.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-filewrap
6
6
  License: MIT
@@ -0,0 +1,7 @@
1
+ LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
+ filewrap/__init__.py,sha256=l_Q8a1s5NvAs8UCcYm5vU-CNBWRG3HZwgI2_p9gKX00,7948
3
+ filewrap/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ python_filewrap-0.0.2.1.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
+ python_filewrap-0.0.2.1.dist-info/METADATA,sha256=ajb4Ejcp7sbKejMJLlBjV-GxQuvIz3OG3G1n8gkfr9g,1331
6
+ python_filewrap-0.0.2.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
+ python_filewrap-0.0.2.1.dist-info/RECORD,,
@@ -1,7 +0,0 @@
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,,