Skip to content

Commit

Permalink
refactor download() a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
valgur committed Aug 16, 2021
1 parent 80f504a commit 11f590c
Showing 1 changed file with 48 additions and 48 deletions.
96 changes: 48 additions & 48 deletions sentinelsat/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,58 +125,58 @@ def download(self, id, directory=".", *, stop_event=None):
LTAError
If the product has been archived and its retrieval failed.
"""
if not self.node_filter:
product_info = self.api.get_product_odata(id)
filename = self.api._get_filename(product_info)
path = Path(directory) / filename
product_info["path"] = str(path)
product_info["downloaded_bytes"] = 0

if path.exists():
# We assume that the product has been downloaded and is complete
return product_info
if self.node_filter:
return self._download_with_node_filter(id, directory, stop_event)

# An incomplete download triggers the retrieval from the LTA if the product is not online
if not self.api.is_online(id):
self.trigger_offline_retrieval(id)
raise LTATriggered(id)
product_info = self.api.get_product_odata(id)
filename = self.api._get_filename(product_info)
path = Path(directory) / filename
product_info["path"] = str(path)
product_info["downloaded_bytes"] = 0

self._download_outer(product_info, path, stop_event)
return product_info
else:
product_info = self.api.get_product_odata(id)
product_path = Path(directory) / (product_info["title"] + ".SAFE")
product_info["node_path"] = "./" + product_info["title"] + ".SAFE"
manifest_path = product_path / "manifest.safe"
if not manifest_path.exists() and self.trigger_offline_retrieval(id):
raise LTATriggered(id)

manifest_info, _ = self.api._get_manifest(product_info, manifest_path)
product_info["nodes"] = {
manifest_info["node_path"]: manifest_info,
}

node_infos = self._filter_nodes(manifest_path, product_info, self.node_filter)
product_info["nodes"].update(node_infos)

for node_info in node_infos.values():
if stop_event and stop_event.is_set():
raise concurrent.futures.CancelledError()
node_path = node_info["node_path"]
path = (product_path / node_path).resolve()
node_info["path"] = path
node_info["downloaded_bytes"] = 0

self.logger.debug("Downloading %s node to %s", id, path)
self.logger.debug("Node URL for %s: %s", id, node_info["url"])

if path.exists():
# We assume that the product node has been downloaded and is complete
continue
self._download_outer(node_info, path, stop_event)
if path.exists():
# We assume that the product has been downloaded and is complete
return product_info

def _download_outer(self, product_info: Dict[str, Any], path: Path, stop_event):
# An incomplete download triggers the retrieval from the LTA if the product is not online
if not self.api.is_online(id):
self.trigger_offline_retrieval(id)
raise LTATriggered(id)

self._download_common(product_info, path, stop_event)
return product_info

def _download_with_node_filter(self, id, directory, stop_event):
product_info = self.api.get_product_odata(id)
product_path = Path(directory) / (product_info["title"] + ".SAFE")
product_info["node_path"] = "./" + product_info["title"] + ".SAFE"
manifest_path = product_path / "manifest.safe"
if not manifest_path.exists() and self.trigger_offline_retrieval(id):
raise LTATriggered(id)
manifest_info, _ = self.api._get_manifest(product_info, manifest_path)
product_info["nodes"] = {
manifest_info["node_path"]: manifest_info,
}
node_infos = self._filter_nodes(manifest_path, product_info, self.node_filter)
product_info["nodes"].update(node_infos)
for node_info in node_infos.values():
if stop_event and stop_event.is_set():
raise concurrent.futures.CancelledError()
node_path = node_info["node_path"]
path = (product_path / node_path).resolve()
node_info["path"] = path
node_info["downloaded_bytes"] = 0

self.logger.debug("Downloading %s node to %s", id, path)
self.logger.debug("Node URL for %s: %s", id, node_info["url"])

if path.exists():
# We assume that the product node has been downloaded and is complete
continue
self._download_common(node_info, path, stop_event)
return product_info

def _download_common(self, product_info: Dict[str, Any], path: Path, stop_event):
# Use a temporary file for downloading
temp_path = path.with_name(path.name + ".incomplete")
skip_download = False
Expand Down

0 comments on commit 11f590c

Please sign in to comment.