11from abc import abstractmethod , ABC
22from collections import OrderedDict
3- import os
43from string import Formatter
54from typing import Any , Callable , Dict , List , Optional , TYPE_CHECKING , Union
65
76import pystac
7+ from pystac .utils import safe_urlparse , join_path_or_url , JoinType
88
99if TYPE_CHECKING :
1010 from pystac .stac_object import STACObject as STACObject_Type
@@ -374,36 +374,51 @@ def __init__(
374374 def get_catalog_href (
375375 self , cat : "Catalog_Type" , parent_dir : str , is_root : bool
376376 ) -> str :
377+ parsed_parent_dir = safe_urlparse (parent_dir )
378+ join_type = JoinType .from_parsed_uri (parsed_parent_dir )
379+
377380 if is_root or self .catalog_template is None :
378381 return self .fallback_strategy .get_catalog_href (cat , parent_dir , is_root )
379382 else :
380383 template_path = self .catalog_template .substitute (cat )
381384 if not template_path .endswith (".json" ):
382- template_path = os .path .join (template_path , cat .DEFAULT_FILE_NAME )
385+ template_path = join_path_or_url (
386+ join_type , template_path , cat .DEFAULT_FILE_NAME
387+ )
383388
384- return os . path . join ( parent_dir , template_path )
389+ return join_path_or_url ( join_type , parent_dir , template_path )
385390
386391 def get_collection_href (
387392 self , col : "Collection_Type" , parent_dir : str , is_root : bool
388393 ) -> str :
394+ parsed_parent_dir = safe_urlparse (parent_dir )
395+ join_type = JoinType .from_parsed_uri (parsed_parent_dir )
396+
389397 if is_root or self .collection_template is None :
390398 return self .fallback_strategy .get_collection_href (col , parent_dir , is_root )
391399 else :
392400 template_path = self .collection_template .substitute (col )
393401 if not template_path .endswith (".json" ):
394- template_path = os .path .join (template_path , col .DEFAULT_FILE_NAME )
402+ template_path = join_path_or_url (
403+ join_type , template_path , col .DEFAULT_FILE_NAME
404+ )
395405
396- return os . path . join ( parent_dir , template_path )
406+ return join_path_or_url ( join_type , parent_dir , template_path )
397407
398408 def get_item_href (self , item : "Item_Type" , parent_dir : str ) -> str :
409+ parsed_parent_dir = safe_urlparse (parent_dir )
410+ join_type = JoinType .from_parsed_uri (parsed_parent_dir )
411+
399412 if self .item_template is None :
400413 return self .fallback_strategy .get_item_href (item , parent_dir )
401414 else :
402415 template_path = self .item_template .substitute (item )
403416 if not template_path .endswith (".json" ):
404- template_path = os .path .join (template_path , "{}.json" .format (item .id ))
417+ template_path = join_path_or_url (
418+ join_type , template_path , "{}.json" .format (item .id )
419+ )
405420
406- return os . path . join ( parent_dir , template_path )
421+ return join_path_or_url ( join_type , parent_dir , template_path )
407422
408423
409424class BestPracticesLayoutStrategy (HrefLayoutStrategy ):
@@ -424,24 +439,33 @@ class BestPracticesLayoutStrategy(HrefLayoutStrategy):
424439 def get_catalog_href (
425440 self , cat : "Catalog_Type" , parent_dir : str , is_root : bool
426441 ) -> str :
442+ parsed_parent_dir = safe_urlparse (parent_dir )
443+ join_type = JoinType .from_parsed_uri (parsed_parent_dir )
444+
427445 if is_root :
428446 cat_root = parent_dir
429447 else :
430- cat_root = os . path . join ( parent_dir , "{}" .format (cat .id ))
448+ cat_root = join_path_or_url ( join_type , parent_dir , "{}" .format (cat .id ))
431449
432- return os . path . join ( cat_root , cat .DEFAULT_FILE_NAME )
450+ return join_path_or_url ( join_type , cat_root , cat .DEFAULT_FILE_NAME )
433451
434452 def get_collection_href (
435453 self , col : "Collection_Type" , parent_dir : str , is_root : bool
436454 ) -> str :
455+ parsed_parent_dir = safe_urlparse (parent_dir )
456+ join_type = JoinType .from_parsed_uri (parsed_parent_dir )
457+
437458 if is_root :
438459 col_root = parent_dir
439460 else :
440- col_root = os . path . join ( parent_dir , "{}" .format (col .id ))
461+ col_root = join_path_or_url ( join_type , parent_dir , "{}" .format (col .id ))
441462
442- return os . path . join ( col_root , col .DEFAULT_FILE_NAME )
463+ return join_path_or_url ( join_type , col_root , col .DEFAULT_FILE_NAME )
443464
444465 def get_item_href (self , item : "Item_Type" , parent_dir : str ) -> str :
445- item_root = os .path .join (parent_dir , "{}" .format (item .id ))
466+ parsed_parent_dir = safe_urlparse (parent_dir )
467+ join_type = JoinType .from_parsed_uri (parsed_parent_dir )
468+
469+ item_root = join_path_or_url (join_type , parent_dir , "{}" .format (item .id ))
446470
447- return os . path . join ( item_root , "{}.json" .format (item .id ))
471+ return join_path_or_url ( join_type , item_root , "{}.json" .format (item .id ))
0 commit comments