Skip to content

Commit

Permalink
nhg
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-braun committed Feb 21, 2025
1 parent 4fe6749 commit b9b00e8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 59 deletions.
10 changes: 8 additions & 2 deletions eoreader/products/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,17 @@ def __init__(
# This is to avoid a meaningless tmp folder (tmp_None) if the output is given directly in the init of the product
self._move_tmp_process(f"tmp_{self.condensed_name}")

def __enter__(self):
return self

def __exit__(self, *args, **kwargs):
self.close()

def __del__(self):
"""Cleaning up _tmp directory"""
self.delete()
self.close()

def delete(self):
def close(self):
self.clear()

# -- Remove temp folders
Expand Down
106 changes: 49 additions & 57 deletions eoreader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import importlib
import logging
import re
from contextlib import contextmanager
from enum import unique
from typing import Union
from zipfile import BadZipFile
Expand Down Expand Up @@ -504,7 +503,6 @@ def _compile_(regex_str: str):

return comp

@contextmanager
def open(
self,
product_path: AnyPathStrType,
Expand Down Expand Up @@ -579,65 +577,59 @@ def open(
Product: EOReader's product
"""
prod = None
try:
# If a URL is given, it must point to a URL translatable to a STAC Item
if validators.url(product_path):
if PYSTAC_INSTALLED:
try:
product_path = Item.from_file(product_path)
is_stac = True
except Exception as exc:
raise InvalidProductError(
f"Cannot convert your URL ({product_path}) to a STAC Item."
) from exc
else:
raise ModuleNotFoundError(
"You should install 'pystac' to use STAC Products."
)
# Check path (first check URL as they are also strings)
elif path.is_path(product_path):
is_stac = False
else:
# Check STAC Item
if PYSTAC_INSTALLED:
is_stac = isinstance(product_path, pystac.Item)
else:
is_stac = False

if is_stac:
prod = self._open_stac_item(
product_path, output_path, remove_tmp, **kwargs
)
# If a URL is given, it must point to a URL translatable to a STAC Item
if validators.url(product_path):
if PYSTAC_INSTALLED:
try:
product_path = Item.from_file(product_path)
is_stac = True
except Exception as exc:
raise InvalidProductError(
f"Cannot convert your URL ({product_path}) to a STAC Item."
) from exc
else:
# If not an Item, it should be a path to somewhere
prod = self._open_path(
product_path,
archive_path,
output_path,
method,
remove_tmp,
custom,
constellation,
**kwargs,
raise ModuleNotFoundError(
"You should install 'pystac' to use STAC Products."
)
# Check path (first check URL as they are also strings)
elif path.is_path(product_path):
is_stac = False
else:
# Check STAC Item
if PYSTAC_INSTALLED:
is_stac = isinstance(product_path, pystac.Item)
else:
is_stac = False

if not prod:
LOGGER.warning(
f"There is no existing products in EOReader corresponding to {product_path}."
)
LOGGER.info(
"Your given path may not be a satellite image. If it is, maybe the product isn't handled by EOReader. "
"If you are sure this product is handled, it is either corrupted or you may need to go deeper in the filetree to find the correct path to give."
)
LOGGER.debug(
"Please look at what folder you should give to EOReader by accessing the documentation: "
"https://eoreader.readthedocs.io/latest/main_features.html#recognized-paths"
)
yield prod
finally:
if prod is not None:
LOGGER.debug(f"Closing {prod.condensed_name}")
prod.delete()
if is_stac:
prod = self._open_stac_item(product_path, output_path, remove_tmp, **kwargs)
else:
# If not an Item, it should be a path to somewhere
prod = self._open_path(
product_path,
archive_path,
output_path,
method,
remove_tmp,
custom,
constellation,
**kwargs,
)

if not prod:
LOGGER.warning(
f"There is no existing products in EOReader corresponding to {product_path}."
)
LOGGER.info(
"Your given path may not be a satellite image. If it is, maybe the product isn't handled by EOReader. "
"If you are sure this product is handled, it is either corrupted or you may need to go deeper in the filetree to find the correct path to give."
)
LOGGER.debug(
"Please look at what folder you should give to EOReader by accessing the documentation: "
"https://eoreader.readthedocs.io/latest/main_features.html#recognized-paths"
)
return prod

def _open_stac_item(
self, item: Item, output_path: AnyPathStrType, remove_tmp: bool, **kwargs
Expand Down

0 comments on commit b9b00e8

Please sign in to comment.