29
29
identify_stac_object ,
30
30
migrate_to_latest ,
31
31
)
32
- from pystac .utils import is_absolute_href , make_absolute_href
32
+ from pystac .utils import is_absolute_href , make_absolute_href , make_relative_href
33
33
34
34
if TYPE_CHECKING :
35
35
from pystac .item import Asset as Asset_Type , Item as Item_Type
@@ -705,16 +705,22 @@ def generate_subcatalogs(
705
705
706
706
return result
707
707
708
- def save (self , catalog_type : Optional [CatalogType ] = None ) -> None :
708
+ def save (
709
+ self ,
710
+ catalog_type : Optional [CatalogType ] = None ,
711
+ dest_href : Optional [str ] = None ,
712
+ ) -> None :
709
713
"""Save this catalog and all it's children/item to files determined by the object's
710
- self link HREF.
714
+ self link HREF or a specified path .
711
715
712
716
Args:
713
717
catalog_type : The catalog type that dictates the structure of
714
718
the catalog to save. Use a member of :class:`~pystac.CatalogType`.
715
719
If not supplied, the catalog_type of this catalog will be used.
716
720
If that attribute is not set, an exception will be raised.
717
-
721
+ dest_href : The location where the catalog is to be saved.
722
+ If not supplied, the catalog's self link HREF is used to determine
723
+ the location of the catalog file and children's files.
718
724
Note:
719
725
If the catalog type is ``CatalogType.ABSOLUTE_PUBLISHED``,
720
726
all self links will be included, and hierarchical links be absolute URLs.
@@ -724,6 +730,7 @@ def save(self, catalog_type: Optional[CatalogType] = None) -> None:
724
730
If the catalog type is ``CatalogType.SELF_CONTAINED``, no self links will
725
731
be included and hierarchical links will be relative URLs.
726
732
"""
733
+
727
734
root = self .get_root ()
728
735
if root is None :
729
736
raise Exception ("There is no root catalog" )
@@ -735,13 +742,27 @@ def save(self, catalog_type: Optional[CatalogType] = None) -> None:
735
742
736
743
for child_link in self .get_child_links ():
737
744
if child_link .is_resolved ():
738
- cast (Catalog , child_link .target ).save ()
745
+ child = cast (Catalog , child_link .target )
746
+ if dest_href is not None :
747
+ rel_href = make_relative_href (child .self_href , self .self_href )
748
+ child_dest_href = make_absolute_href (
749
+ rel_href , dest_href , start_is_dir = True
750
+ )
751
+ child .save (dest_href = child_dest_href )
752
+ else :
753
+ child .save ()
739
754
740
755
for item_link in self .get_item_links ():
741
756
if item_link .is_resolved ():
742
- cast (pystac .Item , item_link .target ).save_object (
743
- include_self_link = items_include_self_link
744
- )
757
+ item = cast (pystac .Item , item_link .target )
758
+ if dest_href is not None :
759
+ rel_href = make_relative_href (item .self_href , self .self_href )
760
+ item_dest_href = make_absolute_href (
761
+ rel_href , dest_href , start_is_dir = True
762
+ )
763
+ item .save_object (include_self_link = True , dest_href = item_dest_href )
764
+ else :
765
+ item .save_object (include_self_link = items_include_self_link )
745
766
746
767
include_self_link = False
747
768
# include a self link if this is the root catalog
@@ -753,7 +774,15 @@ def save(self, catalog_type: Optional[CatalogType] = None) -> None:
753
774
if root_link and root_link .get_absolute_href () == self .get_self_href ():
754
775
include_self_link = True
755
776
756
- self .save_object (include_self_link = include_self_link )
777
+ catalog_dest_href = None
778
+ if dest_href is not None :
779
+ rel_href = make_relative_href (self .self_href , self .self_href )
780
+ catalog_dest_href = make_absolute_href (
781
+ rel_href , dest_href , start_is_dir = True
782
+ )
783
+ self .save_object (
784
+ include_self_link = include_self_link , dest_href = catalog_dest_href
785
+ )
757
786
if catalog_type is not None :
758
787
self .catalog_type = catalog_type
759
788
0 commit comments