77
88"""
99
10- from os .path import basename
1110from pathlib import Path
12- from shutil import rmtree
13- from tempfile import mkdtemp
11+ from tempfile import TemporaryDirectory
1412
1513from harmony_service_lib import BaseHarmonyAdapter
1614from harmony_service_lib .message import Source as HarmonySource
@@ -73,7 +71,8 @@ def get_asset_from_item(self, item: Item) -> Asset:
7371 This is used to select which asset is used by HyBIG to generate
7472 the browse image following these steps:
7573
76- 1. If found, return the first asset with 'visual' in any of the item's values' roles.
74+ 1. If found, return the first asset with 'visual' in any of the item's
75+ values' roles.
7776 2. If found, return the first asset that has 'data' in its item's values' roles.
7877 3. Raise a StopIteration error.
7978
@@ -93,61 +92,59 @@ def get_asset_from_item(self, item: Item) -> Asset:
9392
9493 def process_item (self , item : Item , source : HarmonySource ) -> Item :
9594 """Processes a single input STAC item."""
96- try :
97- working_directory = mkdtemp ()
98- results = item .clone ()
99- results .assets = {}
100-
101- asset = self .get_asset_from_item (item )
102-
103- color_palette = get_color_palette_from_item (item )
104-
105- # Download the input:
106- input_data_filename = download (
107- asset .href ,
108- working_directory ,
109- logger = self .logger ,
110- cfg = self .config ,
111- access_token = self .message .accessToken ,
112- )
95+ with TemporaryDirectory () as working_directory :
96+ try :
97+ results = item .clone ()
98+ results .assets = {}
99+
100+ asset = self .get_asset_from_item (item )
101+
102+ color_palette = get_color_palette_from_item (item )
103+
104+ # Download the input:
105+ input_data_filename = download (
106+ asset .href ,
107+ working_directory ,
108+ logger = self .logger ,
109+ cfg = self .config ,
110+ access_token = self .message .accessToken ,
111+ )
113112
114- # Create browse images.
115- image_file_list = create_browse_imagery (
116- self .message ,
117- input_data_filename ,
118- source ,
119- color_palette ,
120- logger = self .logger ,
121- )
113+ # Create browse images.
114+ image_file_list = create_browse_imagery (
115+ self .message ,
116+ input_data_filename ,
117+ source ,
118+ color_palette ,
119+ logger = self .logger ,
120+ )
122121
123- # image_file_list is a list of tuples (image, world, auxiliary)
124- # we need to stage them each individually, and then add their final
125- # locations to a list before creating the stac item.
126- item_assets = []
127-
128- for (
129- browse_image_name ,
130- world_file_name ,
131- aux_xml_file_name ,
132- ) in image_file_list :
133- # Stage the images:
134- browse_image_url = self .stage_output (browse_image_name , asset .href )
135- browse_aux_url = self .stage_output (aux_xml_file_name , asset .href )
136- world_file_url = self .stage_output (world_file_name , asset .href )
137- item_assets .append (('data' , browse_image_url , 'data' ))
138- item_assets .append (('metadata' , world_file_url , 'metadata' ))
139- item_assets .append (('auxiliary' , browse_aux_url , 'metadata' ))
140-
141- manifest_url = self .stage_manifest (image_file_list , asset .href )
142- item_assets .insert (0 , ('data' , manifest_url , 'metadata' ))
143-
144- return self .create_output_stac_item (item , item_assets )
145-
146- except Exception as exception :
147- self .logger .exception (exception )
148- raise HyBIGServiceError from exception
149- finally :
150- rmtree (working_directory )
122+ # image_file_list is a list of tuples (image, world, auxiliary)
123+ # we need to stage them each individually, and then add their final
124+ # locations to a list before creating the stac item.
125+ item_assets = []
126+
127+ for (
128+ browse_image_name ,
129+ world_file_name ,
130+ aux_xml_file_name ,
131+ ) in image_file_list :
132+ # Stage the images:
133+ browse_image_url = self .stage_output (browse_image_name , asset .href )
134+ browse_aux_url = self .stage_output (aux_xml_file_name , asset .href )
135+ world_file_url = self .stage_output (world_file_name , asset .href )
136+ item_assets .append (('data' , browse_image_url , 'data' ))
137+ item_assets .append (('metadata' , world_file_url , 'metadata' ))
138+ item_assets .append (('auxiliary' , browse_aux_url , 'metadata' ))
139+
140+ manifest_url = self .stage_manifest (image_file_list , asset .href )
141+ item_assets .insert (0 , ('data' , manifest_url , 'metadata' ))
142+
143+ return self .create_output_stac_item (item , item_assets )
144+
145+ except Exception as exception :
146+ self .logger .exception (exception )
147+ raise HyBIGServiceError (str (exception )) from exception
151148
152149 def stage_output (self , transformed_file : Path , input_file : str ) -> str :
153150 """Generate an output file name based on the input asset URL and the
@@ -174,7 +171,7 @@ def create_output_stac_item(
174171 """Create an output STAC item used to access the browse imagery and
175172 ESRI world file as staged in S3.
176173
177- asset_items are an array of tuples where the tuples should be: (name,
174+ item_assets is an array of tuples where the tuples should be: (name,
178175 url, role)
179176
180177 """
@@ -191,7 +188,7 @@ def create_output_stac_item(
191188
192189 output_stac_item .assets [asset_name ] = Asset (
193190 url ,
194- title = basename (url ),
191+ title = Path (url ). name ,
195192 media_type = get_file_mime_type (url ),
196193 roles = [role ],
197194 )
0 commit comments