python-filewrap 0.0.7.2__py3-none-any.whl → 0.0.8__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,7 +2,7 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 7)
5
+ __version__ = (0, 0, 8)
6
6
  __all__ = [
7
7
  "SupportsRead", "SupportsWrite", "SupportsSeek",
8
8
  "bio_chunk_iter", "bio_chunk_async_iter",
@@ -45,7 +45,7 @@ class SupportsWrite(Protocol[_T_contra]):
45
45
 
46
46
 
47
47
  @runtime_checkable
48
- class SupportsSeek(Protocol[_T_contra]):
48
+ class SupportsSeek(Protocol):
49
49
  def seek(self, /, __offset: int, __whence: int = 0) -> int: ...
50
50
 
51
51
 
@@ -54,30 +54,60 @@ def bio_chunk_iter(
54
54
  /,
55
55
  size: int = -1,
56
56
  chunksize: int = COPY_BUFSIZE,
57
+ can_buffer: bool = False,
57
58
  callback: None | Callable[[int], Any] = None,
58
59
  ) -> Iterator[Buffer]:
60
+ use_readinto = False
59
61
  if callable(bio):
60
62
  read = bio
63
+ elif can_buffer and hasattr(bio, "readinto"):
64
+ readinto = bio.readinto
65
+ use_readinto = True
61
66
  else:
62
67
  read = bio.read
63
68
  if not callable(callback):
64
69
  callback = None
65
- if size > 0:
66
- while size:
67
- readsize = min(chunksize, size)
68
- chunk = read(readsize)
69
- length = len(chunk)
70
- if callback:
71
- callback(length)
72
- yield chunk
73
- if length < readsize:
74
- break
75
- size -= readsize
76
- elif size < 0:
77
- while (chunk := read(chunksize)):
78
- if callback:
79
- callback(len(chunk))
80
- yield chunk
70
+ if use_readinto:
71
+ buf = bytearray(chunksize)
72
+ if size > 0:
73
+ while size:
74
+ if size < chunksize:
75
+ del buf[size:]
76
+ length = readinto(buf)
77
+ if callback:
78
+ callback(length)
79
+ if length < len(buf):
80
+ del buf[length:]
81
+ yield buf
82
+ break
83
+ yield buf
84
+ size -= length
85
+ else:
86
+ while (length := readinto(buf)):
87
+ if callback:
88
+ callback(length)
89
+ if length < chunksize:
90
+ del buf[length:]
91
+ yield buf
92
+ break
93
+ yield buf
94
+ else:
95
+ if size > 0:
96
+ while size:
97
+ readsize = min(chunksize, size)
98
+ chunk = read(readsize)
99
+ length = len(chunk)
100
+ if callback:
101
+ callback(length)
102
+ yield chunk
103
+ if length < readsize:
104
+ break
105
+ size -= length
106
+ elif size < 0:
107
+ while (chunk := read(chunksize)):
108
+ if callback:
109
+ callback(len(chunk))
110
+ yield chunk
81
111
 
82
112
 
83
113
  async def bio_chunk_async_iter(
@@ -85,29 +115,59 @@ async def bio_chunk_async_iter(
85
115
  /,
86
116
  size: int = -1,
87
117
  chunksize: int = COPY_BUFSIZE,
118
+ can_buffer: bool = False,
88
119
  callback: None | Callable[[int], Any] = None,
89
120
  ) -> AsyncIterator[Buffer]:
121
+ use_readinto = False
90
122
  if callable(bio):
91
123
  read = ensure_async(bio)
124
+ elif can_buffer and hasattr(bio, "readinto"):
125
+ readinto = ensure_async(bio.readinto)
126
+ use_readinto = True
92
127
  else:
93
128
  read = ensure_async(bio.read)
94
129
  callback = ensure_async(callback) if callable(callback) else None
95
- if size > 0:
96
- while size:
97
- readsize = min(chunksize, size)
98
- chunk = await read(readsize)
99
- length = len(chunk)
100
- if callback:
101
- await callback(length)
102
- yield chunk
103
- if length < readsize:
104
- break
105
- size -= readsize
106
- elif size < 0:
107
- while (chunk := (await read(chunksize))):
108
- if callback:
109
- await callback(len(chunk))
110
- yield chunk
130
+ if use_readinto:
131
+ buf = bytearray(chunksize)
132
+ if size > 0:
133
+ while size:
134
+ if size < chunksize:
135
+ del buf[size:]
136
+ length = await readinto(buf)
137
+ if callback:
138
+ await callback(length)
139
+ if length < len(buf):
140
+ del buf[length:]
141
+ yield buf
142
+ break
143
+ yield buf
144
+ size -= length
145
+ else:
146
+ while (length := (await readinto(buf))):
147
+ if callback:
148
+ await callback(length)
149
+ if length < chunksize:
150
+ del buf[length:]
151
+ yield buf
152
+ break
153
+ yield buf
154
+ else:
155
+ if size > 0:
156
+ while size:
157
+ readsize = min(chunksize, size)
158
+ chunk = await read(readsize)
159
+ length = len(chunk)
160
+ if callback:
161
+ await callback(length)
162
+ yield chunk
163
+ if length < readsize:
164
+ break
165
+ size -= readsize
166
+ elif size < 0:
167
+ while (chunk := (await read(chunksize))):
168
+ if callback:
169
+ await callback(len(chunk))
170
+ yield chunk
111
171
 
112
172
 
113
173
  def bio_skip_iter(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-filewrap
3
- Version: 0.0.7.2
3
+ Version: 0.0.8
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=kgvpxjHpP0ZfyZsxofnSEjYXClNVhbsQARewbytl01Q,18778
3
+ filewrap/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ python_filewrap-0.0.8.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
+ python_filewrap-0.0.8.dist-info/METADATA,sha256=Eb2gtpXftkLvFpwHFIlq8QOw1qHwbWBu0mWxhdEPfP4,1362
6
+ python_filewrap-0.0.8.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
+ python_filewrap-0.0.8.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- filewrap/__init__.py,sha256=6XD5ftZ6z5WDo6RDOpGONYlEp7rzBYbqulXVm9z-HnA,16773
3
- filewrap/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_filewrap-0.0.7.2.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_filewrap-0.0.7.2.dist-info/METADATA,sha256=6yuS1eI1vZTXQUNqg4ysI4B1L0qfOqeJJuCFzJvH9ZQ,1364
6
- python_filewrap-0.0.7.2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
- python_filewrap-0.0.7.2.dist-info/RECORD,,