@@ -11,24 +11,25 @@ class Catalog(Thing):
1111 def __init__ (self , data , root = None , ** kwargs ):
1212 """ Initialize a catalog with a catalog file """
1313 super (Catalog , self ).__init__ (data , ** kwargs )
14- self ._root = root
1514
1615 @property
1716 def stac_version (self ):
1817 """ Get the STAC version of this catalog """
19- return self .data ['stac_version' ]
18+ return self ._data ['stac_version' ]
2019
2120 @property
2221 def description (self ):
2322 """ Get catalog description """
24- return self .data .get ('description' , '' )
23+ return self ._data .get ('description' , '' )
2524
2625 @classmethod
27- def create (cls , id = 'stac-catalog' , description = 'A STAC Catalog' , root = None , ** kwargs ):
26+ def create (cls , id = 'stac-catalog' , title = 'A STAC Catalog' ,
27+ description = 'A STAC Catalog' , root = None , ** kwargs ):
2828 """ Create new catalog """
2929 kwargs .update ({
3030 'id' : id ,
3131 'stac_version' : STAC_VERSION ,
32+ 'title' : title ,
3233 'description' : description ,
3334 'links' : []
3435 })
@@ -45,78 +46,49 @@ def catalogs(self):
4546 for cat in self .children ():
4647 for subcat in cat .children ():
4748 yield subcat
48- # Python 2
49- for x in subcat .catalogs ():
50- yield x
51- # Python 3.3+
52- # yield from subcat.catalogs()
49+ yield from subcat .catalogs ()
5350 yield cat
5451
5552 def collections (self ):
5653 """ Recursively get all collections within this Catalog """
5754 for cat in self .children ():
58- if 'extent' in cat .data .keys ():
55+ if 'extent' in cat ._data .keys ():
5956 yield Collection .open (cat .filename )
6057 # TODO - keep going? if other Collections can appear below a Collection
6158 else :
62- # Python 2
63- for x in cat .collections ():
64- yield x
65- # Python 3.3+
66- # yield from cat.collections()
59+ yield from cat .collections ()
6760
6861 def items (self ):
6962 """ Recursively get all items within this Catalog """
7063 for item in self .links ('item' ):
7164 yield Item .open (item )
7265 for child in self .children ():
73- # Python 2
74- for x in child .items ():
75- yield x
76- # Python 3.3+
77- # yield from child.items()
66+ yield from child .items ()
7867
79- def add_catalog (self , catalog ):
68+ def add_catalog (self , catalog , basename = 'catalog' ):
8069 """ Add a catalog to this catalog """
8170 if self .filename is None :
8271 raise STACError ('Save catalog before adding sub-catalogs' )
8372 # add new catalog child link
84- child_link = '%s/catalog .json' % catalog .id
73+ child_link = '%s/%s .json' % ( catalog .id , basename )
8574 child_fname = os .path .join (self .path , child_link )
8675 child_path = os .path .dirname (child_fname )
87- root_link = self .links ('root' )[0 ]
76+ root_links = self .links ('root' )
77+ root_link = root_links [0 ] if len (root_links ) > 0 else self .filename
8878 root_path = os .path .dirname (root_link )
8979 self .add_link ('child' , child_link )
9080 self .save ()
9181 # strip self, parent, child links from catalog and add new links
9282 catalog .clean_hierarchy ()
93- catalog .add_link ('self' , os .path .join (self .endpoint (), os .path .relpath (child_fname , root_path )))
9483 catalog .add_link ('root' , os .path .relpath (root_link , child_path ))
9584 catalog .add_link ('parent' , os .path .relpath (self .filename , child_path ))
9685 # create catalog file
97- catalog .save_as ( child_fname )
86+ catalog .save ( filename = child_fname )
9887 return self
9988
100- def endpoint (self ):
101- """ Get endpoint URL to the root catalog """
102- return os .path .dirname (self .root ().links ('self' )[0 ])
103-
104- def publish (self , endpoint , root = None ):
105- """ Update all self links throughout catalog to use new endpoint """
106- # we don't use the catalogs and items functions as we'd have to go
107- # through the tree twice, once for catalogs and once for items
108- # update myself
109- if root is None :
110- root = self .filename
111- super (Catalog , self ).publish (endpoint , root = root )
112- # update direct items
113- for link in self .links ('item' ):
114- item = Item .open (link )
115- item .publish (endpoint , root = root )
116- # follow children
117- for cat in self .children ():
118- cat .publish (endpoint , root = root )
119-
89+ def add_collection (self , catalog , basename = 'collection' ):
90+ """ Add a collection to this catalog """
91+ return self .add_catalog (catalog , basename = basename )
12092
12193
12294# import and end of module prevents problems with circular dependencies.
0 commit comments