diff --git a/eoreader/products/product.py b/eoreader/products/product.py index 10962a99..99b9668e 100644 --- a/eoreader/products/product.py +++ b/eoreader/products/product.py @@ -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 diff --git a/eoreader/reader.py b/eoreader/reader.py index 259b503d..7223b445 100644 --- a/eoreader/reader.py +++ b/eoreader/reader.py @@ -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 @@ -504,7 +503,6 @@ def _compile_(regex_str: str): return comp - @contextmanager def open( self, product_path: AnyPathStrType, @@ -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