hdx-python-api 6.7.5__py3-none-any.whl → 6.7.6__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.
hdx/api/_version.py CHANGED
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
18
18
  commit_id: str | None
19
19
  __commit_id__: str | None
20
20
 
21
- __version__ = version = '6.7.5'
22
- __version_tuple__ = version_tuple = (6, 7, 5)
21
+ __version__ = version = '6.7.6'
22
+ __version_tuple__ = version_tuple = (6, 7, 6)
23
23
 
24
24
  __commit_id__ = commit_id = None
hdx/data/resource.py CHANGED
@@ -4,13 +4,15 @@ import json
4
4
  import logging
5
5
  from collections.abc import Iterator, Sequence
6
6
  from datetime import datetime
7
- from pathlib import Path
7
+ from pathlib import Path, PureWindowsPath
8
8
  from typing import Any, Optional
9
+ from urllib.parse import urlsplit
9
10
 
10
11
  from hdx.utilities.dateparse import now_utc, now_utc_notz, parse_date
11
12
  from hdx.utilities.downloader import Download
12
13
  from hdx.utilities.file_hashing import get_size_and_hash
13
14
  from hdx.utilities.retriever import Retrieve
15
+ from hdx.utilities.session import get_session
14
16
  from hdx.utilities.uuid import is_valid_uuid
15
17
 
16
18
  import hdx.api.utilities.url_utils
@@ -755,6 +757,22 @@ class Resource(HDXObject):
755
757
  logger.debug(result)
756
758
  return resources
757
759
 
760
+ @staticmethod
761
+ def _get_safe_filename(filename: str) -> str:
762
+ """Strip any directory components (/ or \\) from a filename built from
763
+ resource metadata so downloads cannot be written outside the folder
764
+
765
+ Args:
766
+ filename: Filename to make safe
767
+
768
+ Returns:
769
+ Filename without directory components
770
+ """
771
+ safe_filename = PureWindowsPath(filename).name
772
+ if safe_filename in ("", ".", ".."):
773
+ raise HDXError(f"Invalid filename {filename} for download!")
774
+ return safe_filename
775
+
758
776
  def _download_file(
759
777
  self,
760
778
  url: str,
@@ -773,7 +791,17 @@ class Resource(HDXObject):
773
791
  Returns:
774
792
  Path to downloaded file
775
793
  """
776
- session = hdx.api.utilities.url_utils.get_ckan_ready_session(self.configuration)
794
+ # Only send the HDX API key to the HDX site, not to external resource URLs
795
+ hdx_site_url = self.configuration.get_hdx_site_url()
796
+ if urlsplit(url)[:2] == urlsplit(hdx_site_url)[:2]:
797
+ session = hdx.api.utilities.url_utils.get_ckan_ready_session(
798
+ self.configuration
799
+ )
800
+ else:
801
+ session = get_session(
802
+ full_agent=self.configuration.get_user_agent(), use_env=False
803
+ )
804
+ filename = self._get_safe_filename(filename)
777
805
  with Download(session=session) as downloader:
778
806
  if retriever:
779
807
  downloader = retriever.clone(downloader)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: hdx-python-api
3
- Version: 6.7.5
3
+ Version: 6.7.6
4
4
  Summary: HDX Python API for interacting with the Humanitarian Data Exchange
5
5
  Project-URL: Homepage, https://github.com/OCHA-DAP/hdx-python-api
6
6
  Author: Michael Rans
@@ -28,8 +28,8 @@ Requires-Python: >=3.10
28
28
  Requires-Dist: ckanapi>=4.11
29
29
  Requires-Dist: defopt>=7.0.0
30
30
  Requires-Dist: email-validator
31
- Requires-Dist: hdx-python-country>=4.2.4
32
- Requires-Dist: hdx-python-utilities>=4.2.3
31
+ Requires-Dist: hdx-python-country>=4.2.5
32
+ Requires-Dist: hdx-python-utilities>=4.2.4
33
33
  Requires-Dist: makefun
34
34
  Requires-Dist: requests
35
35
  Provides-Extra: docs
@@ -1,5 +1,5 @@
1
1
  hdx/api/__init__.py,sha256=11ozJKiUsqDCZ3_mcAHhGYUyGK_Unl54djVSBBExFB4,59
2
- hdx/api/_version.py,sha256=COiFxfgYaSnJUzlrh0vxGDx6Wn8d0Gbr-1uNuoVlOao,520
2
+ hdx/api/_version.py,sha256=jJYFyK64B9LC3RE_aJfCOKY0MSwY3pwsvMideYtT6v4,520
3
3
  hdx/api/configuration.py,sha256=9WvuVI-3th2Dv70_t_4VoyrlJwB_9qUSN5_zUKbp4sQ,23805
4
4
  hdx/api/hdx_base_configuration.yaml,sha256=jj2t5b7NnI499rHVwOjj3Q5BAjE8i7NDGVKnWZ5P5-4,2222
5
5
  hdx/api/locations.py,sha256=bFl_cKz_AEQzkE_xWOkrRlUQ0GT2LMPSk-G_LsbV-Ns,4099
@@ -17,7 +17,7 @@ hdx/data/dataset.py,sha256=-GQO08PK8u19O0qRWttg3AuPskPCwjffcE2mPq-7dQE,105903
17
17
  hdx/data/hdxobject.py,sha256=EG5KkVpi-yF6m6HVF79EqDneq1W7mo5M-bPJHG79T0I,28383
18
18
  hdx/data/location.py,sha256=UJPGKc8euAYFJsUFkdQtViLUwqNWYOWwaLELApAMHk4,11168
19
19
  hdx/data/organization.py,sha256=IRen2F2NsXLC8r-PVTS9eQp7LTn_44OcDUa1S7SCKiQ,13140
20
- hdx/data/resource.py,sha256=QDQJp7XoCDn9DmyWPgA2YCn8Z4HGwezEkFyae_YlFKI,56507
20
+ hdx/data/resource.py,sha256=XgYDGD5mYc89uaiRi0oamVJ5FTFSOV6QO_Nx-9_rjpo,57601
21
21
  hdx/data/resource_matcher.py,sha256=Yzk9pWa4zk1p09m6zcZMj0XZf5Y16Pa3KToOdUt7rP8,4247
22
22
  hdx/data/resource_view.py,sha256=GcYkh0iDAe6_m2ErY_A6i3Z7ZFX949eGt1TLnLZv_8s,7346
23
23
  hdx/data/showcase.py,sha256=vnRZ45xd9Ct65eFQkWZtMIJPFifZfKOrH4obO5D5Pvc,14062
@@ -27,7 +27,7 @@ hdx/facades/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
27
27
  hdx/facades/infer_arguments.py,sha256=DvyDuozfOPmoaV7HjhQySojGIrMWW9nos31auQBVVl8,4032
28
28
  hdx/facades/keyword_arguments.py,sha256=aVapHCfqBO6H7xxBgTdK9ISdgrBFsNypPDWihfQ3edU,1125
29
29
  hdx/facades/simple.py,sha256=QWsBkaHG_UM6X3Zu2u3LWVOQzeR24q-EUClrfsL_mkM,1064
30
- hdx_python_api-6.7.5.dist-info/METADATA,sha256=C2mT_Q4SPSXjrlmzR_lHKSCdrO4YwxJPaMFHgsVcHHc,4946
31
- hdx_python_api-6.7.5.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
32
- hdx_python_api-6.7.5.dist-info/licenses/LICENSE,sha256=wc-4GpMn-ODs-U_bTe1YCiPVgvcjzrpYOx2wPuyAeII,1079
33
- hdx_python_api-6.7.5.dist-info/RECORD,,
30
+ hdx_python_api-6.7.6.dist-info/METADATA,sha256=_qNCZVzLKXdxqpPobApEMa5cuFJGeF0161_44-L01vM,4946
31
+ hdx_python_api-6.7.6.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
32
+ hdx_python_api-6.7.6.dist-info/licenses/LICENSE,sha256=wc-4GpMn-ODs-U_bTe1YCiPVgvcjzrpYOx2wPuyAeII,1079
33
+ hdx_python_api-6.7.6.dist-info/RECORD,,