@@ -332,37 +332,43 @@ def check_neither_url_filetoupload(self) -> None:
332332 if self ._file_to_upload is None and "url" not in self .data :
333333 raise HDXError ("Either a url or a file to upload must be supplied!" )
334334
335- def set_types (self , data : Optional [Dict ] = None ) -> None :
336- """Add resource_type and url_type if not supplied based on url or file to
337- upload. Correct the file type.
335+ def correct_format (self , data : Dict = None ) -> None :
336+ """Correct the format of the file
338337
339338 Args:
340- data (Dict): Resource data. Defaults to None (self.data)
339+ data (Dict): Resource data.
341340
342341 Returns:
343342 None
344343 """
345- if data is None :
346- data = self .data
347- if self ._file_to_upload is None :
348- if "url" in data :
349- data ["resource_type" ] = "api"
350- data ["url_type" ] = "api"
351- else :
352- data ["resource_type" ] = "file.upload"
353- data ["url_type" ] = "upload"
354- if "tracking_summary" in data :
355- del data ["tracking_summary" ]
356344 file_format = data .get ("format" )
357- if file_format is not None :
358- file_format = self .get_mapped_format (
359- file_format , configuration = self .configuration
345+ if file_format is None :
346+ return
347+ file_format = self .get_mapped_format (
348+ file_format , configuration = self .configuration
349+ )
350+ if not file_format :
351+ raise HDXError (
352+ f"Supplied file format { file_format } is invalid and could not be mapped to a known type!"
360353 )
361- if not file_format :
362- raise HDXError (
363- f"Supplied file type { file_format } is invalid and could not be mapped to a known type!"
364- )
365- data ["format" ] = file_format
354+ data ["format" ] = file_format
355+
356+ def set_types (self ) -> None :
357+ """Add resource_type and url_type if not supplied based on url or file to
358+ upload.
359+
360+ Returns:
361+ None
362+ """
363+ if self ._file_to_upload is None :
364+ if "url" in self .data :
365+ self .data ["resource_type" ] = "api"
366+ self .data ["url_type" ] = "api"
367+ else :
368+ self .data ["resource_type" ] = "file.upload"
369+ self .data ["url_type" ] = "upload"
370+ if "tracking_summary" in self .data :
371+ del self .data ["tracking_summary" ]
366372
367373 def check_required_fields (self , ignore_fields : ListTuple [str ] = tuple ()) -> None :
368374 """Check that metadata for resource is complete. The parameter ignore_fields
@@ -417,21 +423,29 @@ def _resource_merge_hdx_update(
417423 else :
418424 # update file if size or hash has changed
419425 files ["upload" ] = self ._file_to_upload
426+ self ._old_data ["resource_type" ] = "file.upload"
427+ self ._old_data ["url_type" ] = "upload"
428+ if "tracking_summary" in self ._old_data :
429+ del self ._old_data ["tracking_summary" ]
420430 self ._old_data ["size" ] = size
421431 self ._old_data ["hash" ] = hash
422432 status = 2
423433 self ._url_backup = None
424- elif data_updated :
425- # Should not output timezone info here
426- self ._old_data ["last_modified" ] = now_utc_notz ().isoformat (
427- timespec = "microseconds"
428- )
429- self ._data_updated = False
430- status = 0
431434 else :
432- status = 1
435+ if "url" in self ._old_data :
436+ self ._old_data ["resource_type" ] = "api"
437+ self ._old_data ["url_type" ] = "api"
438+ if data_updated :
439+ # Should not output timezone info here
440+ self ._old_data ["last_modified" ] = now_utc_notz ().isoformat (
441+ timespec = "microseconds"
442+ )
443+ self ._data_updated = False
444+ status = 0
445+ else :
446+ status = 1
433447
434- self .set_types (self ._old_data )
448+ self .correct_format (self ._old_data )
435449 # old_data will be merged into data in the next step
436450 self ._merge_hdx_update ("resource" , "id" , files , True , ** kwargs )
437451 return status
@@ -498,6 +512,7 @@ def create_in_hdx(self, **kwargs: Any) -> int:
498512 return self ._resource_merge_hdx_update (** kwargs )
499513
500514 self .set_types ()
515+ self .correct_format (self .data )
501516 if "ignore_check" not in kwargs : # allow ignoring of field checks
502517 self .check_required_fields ()
503518 files = {}
0 commit comments