python-httpfile 0.0.1.2__py3-none-any.whl → 0.0.1.3__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.
httpfile/__init__.py CHANGED
@@ -19,12 +19,9 @@ from typing import Any, BinaryIO, IO, Optional, Protocol, Self, TypeVar
19
19
  from types import MappingProxyType
20
20
  from warnings import warn
21
21
 
22
- from aiohttp import ClientSession
23
22
  from filewrap import bio_skip_iter
24
23
  from http_response import get_filename, get_length, get_range, get_total_length, is_chunked, is_range_request
25
- from iterutils import through
26
24
  from property import funcproperty
27
- from requests import Session
28
25
  from urlopen import urlopen
29
26
 
30
27
 
@@ -107,6 +104,18 @@ class HTTPFileReader(RawIOBase, BinaryIO):
107
104
  headers["Range"] = f"bytes={start}-"
108
105
  elif start < 0:
109
106
  headers["Range"] = f"bytes={start}"
107
+ if callable(url):
108
+ geturl = url
109
+ def url():
110
+ url = geturl()
111
+ headers_extra = getattr(url, "headers")
112
+ if headers_extra:
113
+ headers.update(headers_extra)
114
+ return url
115
+ elif hasattr(url, "headers"):
116
+ headers_extra = getattr(url, "headers")
117
+ if headers_extra:
118
+ headers.update(headers_extra)
110
119
  response = urlopen(url() if callable(url) else url, headers=headers)
111
120
  if start:
112
121
  rng = get_range(response)
@@ -294,7 +303,8 @@ class HTTPFileReader(RawIOBase, BinaryIO):
294
303
  if old_pos == pos:
295
304
  return pos
296
305
  if pos > old_pos and pos - old_pos <= self.seek_threshold:
297
- through(bio_skip_iter(self, pos - old_pos))
306
+ for _ in bio_skip_iter(self, pos - old_pos):
307
+ pass
298
308
  else:
299
309
  self.reconnect(pos)
300
310
  return pos
@@ -368,42 +378,63 @@ class HTTPFileReader(RawIOBase, BinaryIO):
368
378
  else:
369
379
  return buffer
370
380
 
381
+ try:
382
+ from requests import Session
383
+
384
+ class RequestsFileReader(HTTPFileReader):
385
+
386
+ def __init__(
387
+ self,
388
+ /,
389
+ url: str | Callable[[], str],
390
+ headers: Optional[Mapping] = None,
391
+ start: int = 0,
392
+ seek_threshold: int = 1 << 20,
393
+ urlopen: Callable = Session().get,
394
+ ):
395
+ def urlopen_wrapper(url: str, headers: Optional[Mapping] = headers):
396
+ resp = urlopen(url, headers=headers, stream=True)
397
+ resp.raise_for_status()
398
+ return resp
399
+ super().__init__(
400
+ url,
401
+ headers=headers,
402
+ start=start,
403
+ seek_threshold=seek_threshold,
404
+ urlopen=urlopen_wrapper,
405
+ )
371
406
 
372
- class RequestsFileReader(HTTPFileReader):
407
+ def _add_start(self, delta: int, /):
408
+ pass
373
409
 
374
- def __init__(
375
- self,
376
- /,
377
- url: str | Callable[[], str],
378
- headers: Optional[Mapping] = None,
379
- start: int = 0,
380
- seek_threshold: int = 1 << 20,
381
- urlopen: Callable = Session().get,
382
- ):
383
- def urlopen_wrapper(url: str, headers: Optional[Mapping] = headers):
384
- resp = urlopen(url, headers=headers, stream=True)
385
- resp.raise_for_status()
386
- return resp
387
- super().__init__(
388
- url,
389
- headers=headers,
390
- start=start,
391
- seek_threshold=seek_threshold,
392
- urlopen=urlopen_wrapper,
393
- )
410
+ @funcproperty
411
+ def file(self, /) -> BinaryIO:
412
+ return self.response.raw
394
413
 
395
- def _add_start(self, delta: int, /):
396
- pass
414
+ def tell(self, /) -> int:
415
+ start = self.start
416
+ if start >= self.length:
417
+ return start
418
+ return start + self.file.tell()
419
+ __all__.append("RequestsFileReader")
420
+ except ImportError:
421
+ pass
397
422
 
398
- @funcproperty
399
- def file(self, /) -> BinaryIO:
400
- return self.response.raw
401
423
 
402
- def tell(self, /) -> int:
403
- start = self.start
404
- if start >= self.length:
405
- return start
406
- return start + self.file.tell()
424
+ try:
425
+ from aiohttp import ClientSession
426
+
427
+ ...
428
+ except ImportError:
429
+ pass
430
+
431
+
432
+ try:
433
+ from httpx import Client, AsyncClient
434
+
435
+ ...
436
+ except ImportError:
437
+ pass
407
438
 
408
439
 
409
440
  # TODO: 支持异步文件,使用 aiohttp,参考 aiofiles 的接口实现
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-httpfile
3
- Version: 0.0.1.2
3
+ Version: 0.0.1.3
4
4
  Summary: Python httpfile classes.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-httpfile
6
6
  License: MIT
@@ -21,13 +21,10 @@ Classifier: Programming Language :: Python :: 3 :: Only
21
21
  Classifier: Topic :: Software Development
22
22
  Classifier: Topic :: Software Development :: Libraries
23
23
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
- Requires-Dist: aiohttp
25
24
  Requires-Dist: http_response
26
25
  Requires-Dist: python-filewrap
27
- Requires-Dist: python-iterutils
28
26
  Requires-Dist: python-property
29
27
  Requires-Dist: python-urlopen
30
- Requires-Dist: requests
31
28
  Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-httpfile
32
29
  Description-Content-Type: text/markdown
33
30
 
@@ -0,0 +1,7 @@
1
+ LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
+ httpfile/__init__.py,sha256=HB4QguYNM1Jri_2ZIkERW9LN6-BXS4ZnLKf7F06aQkg,13090
3
+ httpfile/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ python_httpfile-0.0.1.3.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
+ python_httpfile-0.0.1.3.dist-info/METADATA,sha256=2OM3725fbgLkcH7ZgWXXI0uRwAY1H1RdYag7Ng5KRR4,1463
6
+ python_httpfile-0.0.1.3.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
+ python_httpfile-0.0.1.3.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- httpfile/__init__.py,sha256=QMeil_PlioH8MCXWfK4Pty5hJEPFryrkjXTAy_t2anE,12317
3
- httpfile/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_httpfile-0.0.1.2.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_httpfile-0.0.1.2.dist-info/METADATA,sha256=mRCnDifm57cPKclZ4pF9B7g2Xc1naxBCfJzeU0sZF3Q,1542
6
- python_httpfile-0.0.1.2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
- python_httpfile-0.0.1.2.dist-info/RECORD,,