14
14
15
15
16
16
class CatalogType (str , Enum ):
17
- def __str__ (self ):
17
+ def __str__ (self ) -> str :
18
18
return str (self .value )
19
19
20
20
SELF_CONTAINED = 'SELF_CONTAINED'
@@ -137,7 +137,7 @@ def __init__(self,
137
137
if href is not None :
138
138
self .set_self_href (href )
139
139
140
- self .catalog_type = catalog_type
140
+ self .catalog_type : CatalogType = catalog_type
141
141
142
142
self ._resolved_objects .cache (self )
143
143
@@ -156,7 +156,7 @@ def is_relative(self) -> bool:
156
156
def add_child (self ,
157
157
child : "Catalog" ,
158
158
title : Optional [str ] = None ,
159
- strategy : Optional [HrefLayoutStrategy ] = None ):
159
+ strategy : Optional [HrefLayoutStrategy ] = None ) -> None :
160
160
"""Adds a link to a child :class:`~pystac.Catalog` or :class:`~pystac.Collection`.
161
161
This method will set the child's parent to this object, and its root to
162
162
this Catalog's root.
@@ -268,15 +268,15 @@ def get_children(self) -> Iterable["Catalog"]:
268
268
"""
269
269
return map (lambda x : cast (ps .Catalog , x ), self .get_stac_objects ('child' ))
270
270
271
- def get_child_links (self ):
271
+ def get_child_links (self ) -> List [ Link ] :
272
272
"""Return all child links of this catalog.
273
273
274
274
Return:
275
275
List[Link]: List of links of this catalog with ``rel == 'child'``
276
276
"""
277
277
return self .get_links ('child' )
278
278
279
- def clear_children (self ) -> "Catalog" :
279
+ def clear_children (self ) -> None :
280
280
"""Removes all children from this catalog.
281
281
282
282
Return:
@@ -285,7 +285,6 @@ def clear_children(self) -> "Catalog":
285
285
child_ids = [child .id for child in self .get_children ()]
286
286
for child_id in child_ids :
287
287
self .remove_child (child_id )
288
- return self
289
288
290
289
def remove_child (self , child_id : str ) -> None :
291
290
"""Removes an child from this catalog.
@@ -336,7 +335,7 @@ def get_items(self) -> Iterable["ItemType"]:
336
335
"""
337
336
return map (lambda x : cast (ps .Item , x ), self .get_stac_objects ('item' ))
338
337
339
- def clear_items (self ) -> "Catalog" :
338
+ def clear_items (self ) -> None :
340
339
"""Removes all items from this catalog.
341
340
342
341
Return:
@@ -349,7 +348,6 @@ def clear_items(self) -> "Catalog":
349
348
item .set_root (None )
350
349
351
350
self .links = [link for link in self .links if link .rel != 'item' ]
352
- return self
353
351
354
352
def remove_item (self , item_id : str ) -> None :
355
353
"""Removes an item from this catalog.
@@ -396,7 +394,7 @@ def get_item_links(self) -> List[Link]:
396
394
def to_dict (self , include_self_link : bool = True ) -> Dict [str , Any ]:
397
395
links = self .links
398
396
if not include_self_link :
399
- links = filter ( lambda l : l .rel != 'self' , links )
397
+ links = [ x for x in links if x .rel != 'self' ]
400
398
401
399
d : Dict [str , Any ] = {
402
400
'id' : self .id ,
@@ -438,15 +436,15 @@ def clone(self) -> "Catalog":
438
436
439
437
return clone
440
438
441
- def make_all_asset_hrefs_relative (self ):
439
+ def make_all_asset_hrefs_relative (self ) -> None :
442
440
"""Makes all the HREFs of assets belonging to items in this catalog
443
441
and all children to be relative, recursively.
444
442
"""
445
443
for _ , _ , items in self .walk ():
446
444
for item in items :
447
445
item .make_asset_hrefs_relative ()
448
446
449
- def make_all_asset_hrefs_absolute (self ):
447
+ def make_all_asset_hrefs_absolute (self ) -> None :
450
448
"""Makes all the HREFs of assets belonging to items in this catalog
451
449
and all children to be absolute, recursively.
452
450
"""
@@ -457,7 +455,7 @@ def make_all_asset_hrefs_absolute(self):
457
455
def normalize_and_save (self ,
458
456
root_href : str ,
459
457
catalog_type : Optional [CatalogType ] = None ,
460
- strategy : Optional [HrefLayoutStrategy ] = None ):
458
+ strategy : Optional [HrefLayoutStrategy ] = None ) -> None :
461
459
"""Normalizes link HREFs to the given root_href, and saves the catalog.
462
460
463
461
This is a convenience method that simply calls :func:`Catalog.normalize_hrefs
@@ -476,7 +474,9 @@ def normalize_and_save(self,
476
474
self .normalize_hrefs (root_href , strategy = strategy )
477
475
self .save (catalog_type )
478
476
479
- def normalize_hrefs (self , root_href : str , strategy : Optional [HrefLayoutStrategy ] = None ):
477
+ def normalize_hrefs (self ,
478
+ root_href : str ,
479
+ strategy : Optional [HrefLayoutStrategy ] = None ) -> None :
480
480
"""Normalize HREFs will regenerate all link HREFs based on
481
481
an absolute root_href and the canonical catalog layout as specified
482
482
in the STAC specification's best practices.
@@ -492,7 +492,9 @@ def normalize_hrefs(self, root_href: str, strategy: Optional[HrefLayoutStrategy]
492
492
`STAC best practices document <https://github.com/radiantearth/stac-spec/blob/v0.8.1/best-practices.md#catalog-layout>`_ for the canonical layout of a STAC.
493
493
""" # noqa E501
494
494
if strategy is None :
495
- strategy = BestPracticesLayoutStrategy ()
495
+ _strategy : HrefLayoutStrategy = BestPracticesLayoutStrategy ()
496
+ else :
497
+ _strategy = strategy
496
498
497
499
# Normalizing requires an absolute path
498
500
if not is_absolute_href (root_href ):
@@ -501,9 +503,9 @@ def normalize_hrefs(self, root_href: str, strategy: Optional[HrefLayoutStrategy]
501
503
def process_item (item : "ItemType" , _root_href : str ) -> Callable [[], None ]:
502
504
item .resolve_links ()
503
505
504
- new_self_href = strategy .get_href (item , _root_href )
506
+ new_self_href = _strategy .get_href (item , _root_href )
505
507
506
- def fn ():
508
+ def fn () -> None :
507
509
item .set_self_href (new_self_href )
508
510
509
511
return fn
@@ -514,7 +516,7 @@ def process_catalog(cat: Catalog, _root_href: str,
514
516
515
517
cat .resolve_links ()
516
518
517
- new_self_href = strategy .get_href (cat , _root_href , is_root )
519
+ new_self_href = _strategy .get_href (cat , _root_href , is_root )
518
520
new_root = os .path .dirname (new_self_href )
519
521
520
522
for item in cat .get_items ():
@@ -523,7 +525,7 @@ def process_catalog(cat: Catalog, _root_href: str,
523
525
for child in cat .get_children ():
524
526
setter_funcs .extend (process_catalog (child , new_root , is_root = False ))
525
527
526
- def fn ():
528
+ def fn () -> None :
527
529
cat .set_self_href (new_self_href )
528
530
529
531
setter_funcs .append (fn )
@@ -538,8 +540,6 @@ def fn():
538
540
for fn in setter_funcs :
539
541
fn ()
540
542
541
- return self
542
-
543
543
def generate_subcatalogs (self ,
544
544
template : str ,
545
545
defaults : Optional [Dict [str , Any ]] = None ,
@@ -602,9 +602,9 @@ def generate_subcatalogs(self,
602
602
curr_parent = subcat
603
603
604
604
# resolve collection link so when added back points to correct location
605
- link = item .get_single_link ('collection' )
606
- if link is not None :
607
- link .resolve_stac_object ()
605
+ col_link = item .get_single_link ('collection' )
606
+ if col_link is not None :
607
+ col_link .resolve_stac_object ()
608
608
609
609
curr_parent .add_item (item )
610
610
@@ -613,7 +613,7 @@ def generate_subcatalogs(self,
613
613
614
614
return result
615
615
616
- def save (self , catalog_type : CatalogType = None ) -> None :
616
+ def save (self , catalog_type : Optional [ CatalogType ] = None ) -> None :
617
617
"""Save this catalog and all it's children/item to files determined by the object's
618
618
self link HREF.
619
619
@@ -652,14 +652,16 @@ def save(self, catalog_type: CatalogType = None) -> None:
652
652
653
653
include_self_link = False
654
654
# include a self link if this is the root catalog or if ABSOLUTE_PUBLISHED catalog
655
- if ((self .get_self_href () == self .get_root_link ().get_absolute_href ()
656
- and root .catalog_type != CatalogType .SELF_CONTAINED )
657
- or root .catalog_type == CatalogType .ABSOLUTE_PUBLISHED ):
655
+ if root .catalog_type == CatalogType .ABSOLUTE_PUBLISHED :
658
656
include_self_link = True
657
+ elif root .catalog_type != CatalogType .SELF_CONTAINED :
658
+ root_link = self .get_root_link ()
659
+ if root_link and root_link .get_absolute_href () == self .get_self_href ():
660
+ include_self_link = True
659
661
660
662
self .save_object (include_self_link = include_self_link )
661
-
662
- self .catalog_type = catalog_type
663
+ if catalog_type is not None :
664
+ self .catalog_type = catalog_type
663
665
664
666
def walk (self ) -> Iterable [Tuple ["Catalog" , Iterable ["Catalog" ], Iterable ["ItemType" ]]]:
665
667
"""Walks through children and items of catalogs.
@@ -702,7 +704,9 @@ def validate_all(self) -> None:
702
704
def _object_links (self ) -> List [str ]:
703
705
return ['child' , 'item' ] + (ps .STAC_EXTENSIONS .get_extended_object_links (self ))
704
706
705
- def map_items (self , item_mapper : Callable [["ItemType" ], Union ["ItemType" , List ["ItemType" ]]]):
707
+ def map_items (
708
+ self , item_mapper : Callable [["ItemType" ], Union ["ItemType" ,
709
+ List ["ItemType" ]]]) -> "Catalog" :
706
710
"""Creates a copy of a catalog, with each item passed through the
707
711
item_mapper function.
708
712
@@ -718,7 +722,7 @@ def map_items(self, item_mapper: Callable[["ItemType"], Union["ItemType", List["
718
722
719
723
new_cat = cast (Catalog , self .full_copy ())
720
724
721
- def process_catalog (catalog : Catalog ):
725
+ def process_catalog (catalog : Catalog ) -> None :
722
726
for child in catalog .get_children ():
723
727
process_catalog (child )
724
728
@@ -742,9 +746,10 @@ def process_catalog(catalog: Catalog):
742
746
process_catalog (new_cat )
743
747
return new_cat
744
748
745
- def map_assets (self , asset_mapper : Callable [[str , "AssetType" ],
746
- Union ["AssetType" , Tuple [str , "AssetType" ],
747
- Dict [str , "AssetType" ]]]):
749
+ def map_assets (
750
+ self , asset_mapper : Callable [[str , "AssetType" ], Union ["AssetType" , Tuple [str , "AssetType" ],
751
+ Dict [str , "AssetType" ]]]
752
+ ) -> "Catalog" :
748
753
"""Creates a copy of a catalog, with each Asset for each Item passed
749
754
through the asset_mapper function.
750
755
@@ -758,7 +763,7 @@ def map_assets(self, asset_mapper: Callable[[str, "AssetType"],
758
763
Catalog: A full copy of this catalog, with assets manipulated according
759
764
to the asset_mapper function.
760
765
"""
761
- def apply_asset_mapper (tup : Tuple [str , "AssetType" ]):
766
+ def apply_asset_mapper (tup : Tuple [str , "AssetType" ]) -> List [ Tuple [ str , ps . Asset ]] :
762
767
k , v = tup
763
768
result = asset_mapper (k , v )
764
769
if result is None :
@@ -773,7 +778,7 @@ def apply_asset_mapper(tup: Tuple[str, "AssetType"]):
773
778
raise Exception ('asset_mapper must return a non-empty list' )
774
779
return assets
775
780
776
- def item_mapper (item : ps .Item ):
781
+ def item_mapper (item : ps .Item ) -> ps . Item :
777
782
new_assets = [
778
783
x for result in map (apply_asset_mapper , item .assets .items ()) for x in result
779
784
]
@@ -782,7 +787,7 @@ def item_mapper(item: ps.Item):
782
787
783
788
return self .map_items (item_mapper )
784
789
785
- def describe (self , include_hrefs : bool = False , _indent : int = 0 ):
790
+ def describe (self , include_hrefs : bool = False , _indent : int = 0 ) -> None :
786
791
"""Prints out information about this Catalog and all contained
787
792
STACObjects.
788
793
0 commit comments