Skip to content

Commit

Permalink
fix: prevent cruncher duplications with sets instead of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
anesson-cs committed Jul 24, 2024
1 parent 5502d18 commit 3f5d1a1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
20 changes: 4 additions & 16 deletions eodag/api/search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import logging
from collections import UserList
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Union

from shapely.geometry import GeometryCollection, shape

Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(
super(SearchResult, self).__init__(products)
self.number_matched = number_matched
self.search_kwargs: Optional[Dict[str, Any]] = None
self.crunchers: List[Crunch] = []
self.crunchers: Set[Crunch] = set()

def clear(self) -> None:
"""Clear search result"""
Expand All @@ -80,23 +80,11 @@ def crunch(self, cruncher: Crunch, **search_params: Any) -> SearchResult:
:returns: The result of the application of the crunching method to the EO products
:rtype: :class:`~eodag.api.search_result.SearchResult`
"""
for results_cruncher in self.crunchers:
if (
cruncher.__class__.__name__ == results_cruncher.__class__.__name__
and cruncher.config == results_cruncher.config
):
logger.info(
(
f"The cruncher '{cruncher.__class__.__name__}' has already been used "
"for these search results with the following parameter(s): "
f"{cruncher.config}. Please change parameters or use an other cruncher"
)
)
return self
crunched_results_list = cruncher.proceed(self.data, **search_params)
crunched_results = SearchResult(crunched_results_list)
crunched_results.search_kwargs = self.search_kwargs
crunched_results.crunchers = self.crunchers + [cruncher]
self.crunchers.add(cruncher)
crunched_results.crunchers = self.crunchers
return crunched_results

def filter_date(
Expand Down
11 changes: 11 additions & 0 deletions eodag/plugins/crunch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ def __repr__(self) -> str:
f"topic={self.__class__.mro()[-3].__name__})"
)

def __eq__(self, other: object) -> bool:
if not isinstance(other, Crunch):
raise NotImplementedError
return self.__class__.__name__ == other.__class__.__name__ and all(
self.config.__dict__.get(k) == v for k, v in other.config.__dict__.items()
)

def __hash__(self):
# necessary for instances to behave sanely in dicts and sets
return hash((str(self.config.__dict__),))

def proceed(
self, products: List[EOProduct], **search_params: Any
) -> List[EOProduct]:
Expand Down
4 changes: 2 additions & 2 deletions tests/units/test_download_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def test_plugins_download_base_download_all_exhaust_ok(
"geometry": None,
"cloudCover": 80,
}
search_result.crunchers = [FilterProperty({"cloudCover": 20, "operator": "lt"})]
search_result.crunchers = {FilterProperty({"cloudCover": 20, "operator": "lt"})}
# save a copy of the search result to check if it was called well below
tmp_search_result = copy(search_result)

Expand Down Expand Up @@ -294,7 +294,7 @@ def test_plugins_download_base_download_all_exhaust_ok(
# chat that crunch() method is called with search_iter_page() results and for the cruncher given
self.assertEqual(mock_crunch.call_count, 1)
self.assertTupleEqual(
(non_empty_do_search_results, search_result.crunchers[0]),
(non_empty_do_search_results, list(search_result.crunchers)[0]),
mock_crunch.call_args_list[0][0],
)
self.assertDictEqual(
Expand Down

0 comments on commit 3f5d1a1

Please sign in to comment.