11import logging
2+ import warnings
3+ from typing import Union , cast
24
3- from pystac import Asset , Item
5+ from pystac import Asset , Collection , Item
46from pystac .utils import is_absolute_href , make_relative_href
5- from stactools .core .copy import move_asset_file_to_item
7+ from stactools .core .copy import move_asset_file
68
79logger = logging .getLogger (__name__ )
810
911
10- def add_asset_to_item (
11- item : Item ,
12+ def add_asset (
13+ owner : Union [ Collection , Item ] ,
1214 key : str ,
1315 asset : Asset ,
1416 move_assets : bool = False ,
1517 ignore_conflicts : bool = False ,
16- ) -> Item :
17- """Adds an asset to an item.
18+ ) -> Union [ Collection , Item ] :
19+ """Adds an asset to an item or collection .
1820
1921 Args:
20- item (Item): The PySTAC Item to which the asset will be added.
22+ owner (Item or Collection): The PySTAC Item or Collecitonto which the asset
23+ will be added.
2124 key (str): The unique key of the asset.
2225 asset (Asset): The PySTAC Asset to add.
23- move_assets (bool): If True, move the asset file alongside the target item .
26+ move_assets (bool): If True, move the asset file alongside the target owner .
2427 ignore_conflicts (bool): If True, asset with the same key will not be added,
2528 and asset file that would overwrite an existing file will not be moved.
2629 If False, either of these situations will throw an error.
2730
2831 Returns:
29- Item : Returns an updated Item with the added Asset.
30- This operation mutates the Item .
32+ owner : Returns an updated Item or Collection with the added Asset.
33+ This operation mutates the owner .
3134 """
32- item_href = item .get_self_href ()
35+ owner_href = owner .get_self_href ()
3336 asset_href = asset .get_absolute_href ()
34- if key in item .assets :
37+ if key in owner .assets :
3538 if not ignore_conflicts :
3639 raise Exception (
37- f"Target item { item } already has an asset with key { key } , "
40+ f"Target { owner } already has an asset with key { key } , "
3841 "cannot add asset in from {asset_href}"
3942 )
4043 else :
@@ -43,23 +46,54 @@ def add_asset_to_item(
4346 f"Asset { asset } must have an href to be added. The href "
4447 "value should be an absolute path or URL."
4548 )
46- if not item_href and move_assets :
49+ if not owner_href and move_assets :
50+ raise ValueError (f"Target { owner } must have an href to move an asset to it" )
51+ if not owner_href and not is_absolute_href (asset .href ):
4752 raise ValueError (
48- f"Target Item { item } must have an href to move an asset to the item"
49- )
50- if not item_href and not is_absolute_href (asset .href ):
51- raise ValueError (
52- f"Target Item { item } must have an href to add "
53+ f"Target { owner } must have an href to add "
5354 "an asset with a relative href"
5455 )
5556 if move_assets :
56- new_asset_href = move_asset_file_to_item (
57- item , asset_href , ignore_conflicts = ignore_conflicts
57+ new_asset_href = move_asset_file (
58+ owner , asset_href , ignore_conflicts = ignore_conflicts
5859 )
5960 else :
60- if not is_absolute_href (asset .href ) and item_href is not None :
61- asset_href = make_relative_href (asset_href , item_href )
61+ if not is_absolute_href (asset .href ) and owner_href is not None :
62+ asset_href = make_relative_href (asset_href , owner_href )
6263 new_asset_href = asset_href
6364 asset .href = new_asset_href
64- item .add_asset (key , asset )
65- return item
65+ owner .add_asset (key , asset )
66+ return owner
67+
68+
69+ def add_asset_to_item (
70+ item : Item ,
71+ key : str ,
72+ asset : Asset ,
73+ move_assets : bool = False ,
74+ ignore_conflicts : bool = False ,
75+ ) -> Item :
76+ """Adds an asset to an item.
77+
78+ Args:
79+ item (Item): The PySTAC Item to which the asset will be added.
80+ key (str): The unique key of the asset.
81+ asset (Asset): The PySTAC Asset to add.
82+ move_assets (bool): If True, move the asset file alongside the target item.
83+ ignore_conflicts (bool): If True, asset with the same key will not be added,
84+ and asset file that would overwrite an existing file will not be moved.
85+ If False, either of these situations will throw an error.
86+
87+ Returns:
88+ Item: Returns an updated Item with the added Asset.
89+ This operation mutates the Item.
90+ """
91+ warnings .warn (
92+ "'add_asset_to_item' is deprecated. Use 'add_asset' instead" , DeprecationWarning
93+ )
94+ return cast (
95+ Item ,
96+ add_asset (
97+ item , key , asset , move_assets = move_assets , ignore_conflicts = ignore_conflicts
98+ ),
99+ )
0 commit comments