Skip to content

Commit a3c9b7d

Browse files
committed
#20 - refactor code
1 parent a5e1069 commit a3c9b7d

File tree

1 file changed

+30
-38
lines changed

1 file changed

+30
-38
lines changed

src/dwcahandler/dwca/dwca_factory.py

+30-38
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ def list_class_rowtypes() :
2727
print(f"{name}: {member.value}")
2828

2929
@staticmethod
30-
def get_contents_from_file_names(files: list) -> (dict[MetaElementTypes, str], dict[MetaElementTypes, str]):
30+
def get_contents_from_file_names(files: list, csv_encoding: CSVEncoding,
31+
content_keys: dict[MetaElementTypes, list] = None, zf: ZipFile = None) \
32+
-> (ContentData, list[ContentData]):
3133
"""Find the core content and extension contents from a list of file paths.
3234
Core content will always be event if present, otherwise, occurrence content
3335
3436
:param files: list of files
37+
:param csv_encoding: delimiter for txt file. Default is comma delimiter txt files if not supplied
38+
:param content_keys: optional dictionary of MetaElementTypes and key list
39+
for eg. {MetaElementTypes.OCCURRENCE, ["occurrenceID"]}
40+
:param zf: Zipfile pointer if using
3541
:return dict of core content type and file name and dict containing ext content type and file name
3642
"""
3743
def derive_type(file_list: list) -> dict[str, MetaElementTypes]:
@@ -51,9 +57,23 @@ def derive_type(file_list: list) -> dict[str, MetaElementTypes]:
5157
core_filename = next(iter(core_file))
5258
core_type = core_file[core_filename]
5359
ext_files = {k: v for k, v in contents.items() if v != core_type}
54-
return core_file, ext_files
5560

56-
return None
61+
core_data = [core_filename] if not zf else io.TextIOWrapper(zf.open(core_filename), encoding="utf-8")
62+
core_content = ContentData(data=core_data,
63+
type=core_type, csv_encoding=csv_encoding,
64+
keys=get_keys(class_type=core_type,
65+
override_content_keys=content_keys))
66+
ext_content = []
67+
for ext_file, ext_type in ext_files.items():
68+
ext_data = [ext_file] if not zf else io.TextIOWrapper(zf.open(ext_file), encoding="utf-8")
69+
ext_content.append(ContentData(data=ext_data,
70+
type=ext_type, csv_encoding=csv_encoding,
71+
keys=get_keys(class_type=ext_type,
72+
override_content_keys=content_keys)))
73+
return core_content, ext_content
74+
else:
75+
raise ValueError("The core content cannot be determined. Please check filenames against the class type. "
76+
"Use list_class_rowtypes to print the class types. ")
5777

5878
"""Perform various DwCA operations"""
5979
@staticmethod
@@ -71,23 +91,9 @@ def create_dwca_from_file_list(files: list, output_dwca: Union[str, BytesIO],
7191
:param content_keys: optional dictionary of MetaElementTypes and key list
7292
for eg. {MetaElementTypes.OCCURRENCE, ["occurrenceID"]}
7393
"""
74-
core_content, ext_content_list = DwcaHandler.get_contents_from_file_names(files)
75-
if core_content:
76-
core_filename = next(iter(core_content))
77-
core_type = core_content[core_filename]
78-
79-
core_content = ContentData(data=[core_filename], type=core_type, csv_encoding=csv_encoding,
80-
keys=get_keys(class_type=core_type, override_content_keys=content_keys))
81-
ext_content = []
82-
for ext_file, ext_type in ext_content_list.items():
83-
ext_content.append(ContentData(data=[ext_file],
84-
type=ext_type, csv_encoding=csv_encoding,
85-
keys=get_keys(class_type=ext_type,
86-
override_content_keys=content_keys)))
87-
DwcaHandler.create_dwca(core_csv=core_content, ext_csv_list=ext_content, output_dwca=output_dwca,
88-
eml_content=eml_content)
89-
else:
90-
raise ValueError("The core content cannot be determined. Please check filename in zip file")
94+
core_content, ext_content_list = DwcaHandler.get_contents_from_file_names(files=files, csv_encoding=csv_encoding)
95+
DwcaHandler.create_dwca(core_csv=core_content, ext_csv_list=ext_content_list, output_dwca=output_dwca,
96+
eml_content=eml_content)
9197

9298
@staticmethod
9399
def create_dwca_from_zip_content(zip_file: str, output_dwca: Union[str, BytesIO],
@@ -107,24 +113,10 @@ def create_dwca_from_zip_content(zip_file: str, output_dwca: Union[str, BytesIO]
107113
"""
108114
with ZipFile(zip_file, 'r') as zf:
109115
files = zf.namelist()
110-
core_content, ext_content_list = DwcaHandler.get_contents_from_file_names(files)
111-
if core_content:
112-
core_filename = next(iter(core_content))
113-
core_type = core_content[core_filename]
114-
core_content = ContentData(data=io.TextIOWrapper(zf.open(core_filename), encoding="utf-8"),
115-
type=core_type, csv_encoding=csv_encoding,
116-
keys=get_keys(class_type=core_type,
117-
override_content_keys=content_keys))
118-
ext_content = []
119-
for ext_file, ext_type in ext_content_list.items():
120-
ext_content.append(ContentData(data=io.TextIOWrapper(zf.open(ext_file), encoding="utf-8"),
121-
type=ext_type, csv_encoding=csv_encoding,
122-
keys=get_keys(class_type=ext_type,
123-
override_content_keys=content_keys)))
124-
DwcaHandler.create_dwca(core_csv=core_content, ext_csv_list=ext_content, output_dwca=output_dwca,
125-
eml_content=eml_content)
126-
else:
127-
raise ValueError("The core content cannot be determined. Please check filename in zip file")
116+
core_content, ext_content_list = DwcaHandler.get_contents_from_file_names(files=files, csv_encoding=csv_encoding, zf=zf)
117+
DwcaHandler.create_dwca(core_csv=core_content, ext_csv_list=ext_content_list, output_dwca=output_dwca,
118+
eml_content=eml_content)
119+
zf.close()
128120

129121
@staticmethod
130122
def create_dwca(core_csv: ContentData,

0 commit comments

Comments
 (0)