reykit 1.1.71__py3-none-any.whl → 1.1.73__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.
reykit/rnet.py CHANGED
@@ -410,7 +410,7 @@ def download(url: str, path: str | None = None) -> str:
410
410
  ----------
411
411
  url : Download URL.
412
412
  path : Save path.
413
- - `None`, File name is `download`: and automatic judge file type.
413
+ - `None`: File name is get from response, or is 'download' join automatic judge file type.
414
414
 
415
415
  Returns
416
416
  -------
@@ -421,20 +421,21 @@ def download(url: str, path: str | None = None) -> str:
421
421
  response = request(url)
422
422
  content = response.content
423
423
 
424
- # Judge file type and path.
424
+ # File name.
425
425
  if path is None:
426
+ file_name = None
426
427
  Content_disposition = response.headers.get('Content-Disposition', '')
427
428
  if 'filename' in Content_disposition:
428
- file_name = search(
429
+ file_name: str | None = search(
429
430
  'filename=[\'"]?([^\\s\'"]+)',
430
431
  Content_disposition
431
432
  )
432
- else:
433
- file_name = None
434
433
  if file_name is None:
435
434
  file_type_obj = filetype_guess(content)
436
- file_name = 'download.' + file_type_obj.EXTENSION
437
- file_name = file_name or 'download'
435
+ if file_type_obj is not None:
436
+ file_name = 'download.' + file_type_obj.EXTENSION
437
+ else:
438
+ file_name = 'download'
438
439
  path = os_abspath(file_name)
439
440
 
440
441
  # Save.
reykit/ros.py CHANGED
@@ -38,9 +38,10 @@ from os.path import (
38
38
  )
39
39
  from shutil import copy as shutil_copy
40
40
  from pathlib import Path
41
- from json import JSONDecodeError
42
- from tomllib import loads as tomllib_loads
43
41
  from hashlib import md5 as hashlib_md5
42
+ from tomllib import loads as tomllib_loads
43
+ from json import JSONDecodeError
44
+ from filetype import guess as filetype_guess
44
45
  from tempfile import TemporaryFile, TemporaryDirectory
45
46
 
46
47
  from .rbase import Base, throw
@@ -1980,10 +1981,10 @@ class FileCache(Base):
1980
1981
 
1981
1982
  Parameters
1982
1983
  ----------
1983
- md5 : File md5 value.
1984
+ md5 : File MD5 value.
1984
1985
  name : File name.
1985
- - `None`: Use md5 value.
1986
- copy : Do you want to copy file when exist md5 value file and not exist file name.
1986
+ - `None`: Use MD5 value.
1987
+ copy : Do you want to copy file when exist MD5 value file and not exist file name.
1987
1988
 
1988
1989
  Returns
1989
1990
  -------
@@ -2024,7 +2025,7 @@ class FileCache(Base):
2024
2025
  ----------
2025
2026
  source : Source file path or file data.
2026
2027
  name : File name.
2027
- - `None`: Use md5 value.
2028
+ - `None`: Use MD5 value.
2028
2029
  delete : When source is file path, whether delete original file.
2029
2030
 
2030
2031
  Returns
@@ -2069,6 +2070,50 @@ class FileCache(Base):
2069
2070
  return path
2070
2071
 
2071
2072
 
2073
+ def download(self, url: str, name: str | None = None) -> str:
2074
+ """
2075
+ Download file from URL.
2076
+
2077
+ Parameters
2078
+ ----------
2079
+ url : Download URL.
2080
+ name : File name.
2081
+ - `None`: Get from response, or is MD5 value join automatic judge file type.
2082
+
2083
+ Returns
2084
+ -------
2085
+ File absolute path.
2086
+ """
2087
+
2088
+ # Import.
2089
+ from .rnet import request
2090
+
2091
+ # Download.
2092
+ response = request(url)
2093
+ content = response.content
2094
+
2095
+ # File name.
2096
+ if name is None:
2097
+ Content_disposition = response.headers.get('Content-Disposition', '')
2098
+ if 'filename' in Content_disposition:
2099
+ name: str | None = search(
2100
+ 'filename=[\'"]?([^\\s\'"]+)',
2101
+ Content_disposition
2102
+ )
2103
+ if name is None:
2104
+ file_md5 = get_md5(content)
2105
+ file_type_obj = filetype_guess(content)
2106
+ if file_type_obj is not None:
2107
+ name = f'{file_md5}.{file_type_obj.EXTENSION}'
2108
+ else:
2109
+ name = file_md5
2110
+
2111
+ # Store.
2112
+ path = self.store(content, name)
2113
+
2114
+ return path
2115
+
2116
+
2072
2117
  def doc_to_docx(path: str, save_path: str | None = None) -> str:
2073
2118
  """
2074
2119
  Convert `DOC` file to `DOCX` file.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reykit
3
- Version: 1.1.71
3
+ Version: 1.1.73
4
4
  Summary: Kit method set.
5
5
  Project-URL: homepage, https://github.com/reyxbo/reykit/
6
6
  Author-email: Rey <reyxbo@163.com>
@@ -6,9 +6,9 @@ reykit/remail.py,sha256=l4HGKXdfHNBxyBT3YxeZyQhfecbElqTqSAGInwWhap8,6723
6
6
  reykit/rimage.py,sha256=lNN2iMpvSMqh-nPTpxrA9yHy43EA5WoYdxKYhqPwMgk,6154
7
7
  reykit/rlog.py,sha256=TRAWaVG9KTgzeNjN-FXkcvBmvq1IhICgawllQEGoUdg,25745
8
8
  reykit/rmonkey.py,sha256=Dj2GBzBDFXbo0Z-5f8Zep4dfbaIw1bo1FUmC31xvDuk,7929
9
- reykit/rnet.py,sha256=AfMUcAW26zNauW6PeS3GYkLJcJu_82g1tkZGnc--Qto,16848
9
+ reykit/rnet.py,sha256=rKCrIlQnU8yAEOIbnVz6NPKBOWb_RR2vFU1ibvNsxZo,16912
10
10
  reykit/rnum.py,sha256=PhG4V_BkVfCJUsbpMDN1umGZly1Hsus80TW8bpyBtyY,3653
11
- reykit/ros.py,sha256=Yi_mfYzVHwjjUZke0BNX7PKFLZpoT5NRyj5aDOSNQRU,46911
11
+ reykit/ros.py,sha256=ej3sQf5PZqUXWNIWFprV3TAiHffAQ51dSWf1DJUgzMw,48220
12
12
  reykit/rrand.py,sha256=4VwooITgox54_GonELcJfcIpStDi-UJchpnyWKnyeIA,8606
13
13
  reykit/rre.py,sha256=1qva7xatKVE9qC2j7IujjXSM59qxHWwTYpiizFFQ8Xo,6024
14
14
  reykit/rschedule.py,sha256=HuQRSNF6yd397P9knIX9-z8Ii1tUXFUL_-zxTIkxir0,14804
@@ -22,7 +22,7 @@ reykit/rwrap.py,sha256=FEmeK_fboJ-OyXeJf8bilc7U2ph8xIbZGNHb6fLCy2c,15063
22
22
  reykit/rzip.py,sha256=BGEONswuBZxQ-zcgd_xp2fcvYesC9AmKaaXWvnT3bTI,3456
23
23
  reykit/rdll/__init__.py,sha256=nLSb8onBm2ilyoxzpDzUeGfSCKwkLEesIhzK3LiJ8mk,701
24
24
  reykit/rdll/rdll_core.py,sha256=o6-rKcTQgxZQe0kD3GnwyNb3KL9IogzgCQNOmYLMm7A,5086
25
- reykit-1.1.71.dist-info/METADATA,sha256=Nb_DD9MgqjDfIjqFrHgXQZPbjO4r5MM5gMTs0TFpcVw,1872
26
- reykit-1.1.71.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
27
- reykit-1.1.71.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
28
- reykit-1.1.71.dist-info/RECORD,,
25
+ reykit-1.1.73.dist-info/METADATA,sha256=U59vJYIz0jm08eMamUgrwctejLSQyW6oIcCwFQ00oO4,1872
26
+ reykit-1.1.73.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
27
+ reykit-1.1.73.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
28
+ reykit-1.1.73.dist-info/RECORD,,