Skip to content

Commit e05d81c

Browse files
committed
#20 - Fix for default keys for core if not provided
1 parent 19319bb commit e05d81c

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/dwcahandler/dwca/core_dwca.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -770,18 +770,6 @@ def _combine_contents(self, contents: list, csv_encoding, use_chunking=False):
770770

771771
raise ValueError('content is empty')
772772

773-
def __check_csv_info_value(self, csv_info: CsvFileType, col: str):
774-
"""Look for a column in a CSV file
775-
776-
:param csv_info: The CSV file
777-
:param col: The column name
778-
:return: Either column information or False for not found
779-
"""
780-
csv_info_dict = asdict(csv_info)
781-
if col in csv_info_dict:
782-
return csv_info_dict[col]
783-
return False
784-
785773
def check_duplicates(self, content_keys_df, keys, error_file=None):
786774
"""Check a content frame for duplicate keys
787775
@@ -905,14 +893,31 @@ def extract_csv_content(self, csv_info: CsvFileType,
905893
:param core_ext_type: Whether this is a core or extension content frame
906894
:param build_coreid_for_ext: indicator to build id and core id to support dwca with extension
907895
"""
896+
def __get_default_core_key(core_sv_info: CsvFileType):
897+
"""Look for a column in a CSV file
898+
899+
:param csv_info: The CSV file
900+
:param col: The column name
901+
:return: Either column information or False for not found
902+
"""
903+
if not core_sv_info.keys or len(core_sv_info.keys) == 0:
904+
if core_sv_info.type == MetaElementTypes.EVENT:
905+
return ["eventID"]
906+
elif core_sv_info.type == MetaElementTypes.OCCURRENCE:
907+
return ["occurrenceID"]
908+
else:
909+
raise ValueError("Keys need to be set for core content")
910+
elif len(core_sv_info.keys) > 0:
911+
return core_sv_info.keys
912+
908913
if isinstance(csv_info.files, pd.DataFrame):
909-
csv_content = csv_info.files.copy(deep=True)
914+
csv_content = csv_info.files
910915
else:
911916
csv_content = self._combine_contents(csv_info.files, csv_info.csv_encoding)
912917

913918
# Use default occurrenceID if not provided
914919
if core_ext_type == CoreOrExtType.CORE:
915-
keys = csv_info.keys if self.__check_csv_info_value(csv_info, 'keys') else ['occurrenceID']
920+
keys = __get_default_core_key(csv_info)
916921
else:
917922
keys = self.core_content.keys
918923
core_id_field: str = ""

0 commit comments

Comments
 (0)