|
9 | 9 | from enum import Enum
|
10 | 10 | from typing import (
|
11 | 11 | TYPE_CHECKING,
|
| 12 | + Callable, |
12 | 13 | Dict,
|
13 | 14 | Generator,
|
14 | 15 | Iterable,
|
15 | 16 | List,
|
16 | 17 | Optional,
|
| 18 | + Protocol, |
17 | 19 | Set,
|
18 | 20 | Type,
|
19 | 21 | TypeVar,
|
|
134 | 136 | LOGGER = logging.getLogger(__name__)
|
135 | 137 |
|
136 | 138 |
|
| 139 | +class IndexSearchRequestProvider(Protocol): |
| 140 | + def to_request(self) -> IndexSearchRequest: |
| 141 | + pass |
| 142 | + |
| 143 | + |
137 | 144 | class AssetClient:
|
138 | 145 | """
|
139 | 146 | This class can be used to retrieve information about assets. This class does not need to be instantiated
|
@@ -1861,6 +1868,45 @@ def get_hierarchy(
|
1861 | 1868 | )
|
1862 | 1869 | return CategoryHierarchy(top_level=top_categories, stub_dict=category_dict)
|
1863 | 1870 |
|
| 1871 | + def process_assets( |
| 1872 | + self, search: IndexSearchRequestProvider, func: Callable[[Asset], None] |
| 1873 | + ) -> int: |
| 1874 | + """ |
| 1875 | + Process assets matching a search query and apply a processing function to each unique asset. |
| 1876 | +
|
| 1877 | + This function iteratively searches for assets using the search provider and processes each |
| 1878 | + unique asset using the provided callable function. The uniqueness of assets is determined |
| 1879 | + based on their GUIDs. If new assets are found in subsequent iterations that haven't been |
| 1880 | + processed yet, the process continues until no more new assets are available to process. |
| 1881 | +
|
| 1882 | + Arguments: |
| 1883 | + search: IndexSearchRequestProvider |
| 1884 | + The search provider that generates search queries and contains the criteria for |
| 1885 | + searching the assets. |
| 1886 | + func: Callable[[Asset], None] |
| 1887 | + A callable function that receives each unique asset as its parameter and performs |
| 1888 | + the required operations on it. |
| 1889 | +
|
| 1890 | + Returns: |
| 1891 | + int: The total number of unique assets that have been processed. |
| 1892 | + """ |
| 1893 | + guids_processed: set[str] = set() |
| 1894 | + has_assets_to_process: bool = True |
| 1895 | + iteration_count = 0 |
| 1896 | + while has_assets_to_process: |
| 1897 | + iteration_count += 1 |
| 1898 | + has_assets_to_process = False |
| 1899 | + response = self.search(search.to_request()) |
| 1900 | + LOGGER.debug( |
| 1901 | + "Iteration %d found %d assets.", iteration_count, response.count |
| 1902 | + ) |
| 1903 | + for asset in response: |
| 1904 | + if asset.guid not in guids_processed: |
| 1905 | + guids_processed.add(asset.guid) |
| 1906 | + has_assets_to_process = True |
| 1907 | + func(asset) |
| 1908 | + return len(guids_processed) |
| 1909 | + |
1864 | 1910 |
|
1865 | 1911 | class SearchResults(ABC, Iterable):
|
1866 | 1912 | """
|
|
0 commit comments