Skip to content

Commit

Permalink
Updates based on discussion in PR #107
Browse files Browse the repository at this point in the history
* Rename _write_output_files > transform_and_write_output_files
* Rename write_timdex_records_to_json_file > _write_timdex_records_to_json_file
* Rename write_deleted_records_to_txt_file > _write_deleted_records_to_txt_file
* Add docstrings
* Add abstractmethod decorator to get_main_titles
  • Loading branch information
ehanson8 committed Dec 5, 2023
1 parent 7519db8 commit c378408
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
8 changes: 4 additions & 4 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ def test_xmltransformer_iterates_successfully_if_get_optional_fields_returns_non
assert len(output_records.deleted_records) == 1


def test_xmltransformer__write_output_files_writes_timdex_records_and_deleted_files(
def test_xmltransformer_transform_and_write_output_files_writes_output_files(
tmp_path, oai_pmh_records
):
output_file = str(tmp_path / "output_file.json")
transformer = XmlTransformer("cool-repo", oai_pmh_records)
transformer._write_output_files(output_file)
transformer.transform_and_write_output_files(output_file)
output_files = list(tmp_path.iterdir())
assert len(output_files) == 2
assert output_files[0].name == "output_file.json"
assert output_files[1].name == "output_file.txt"


def test_xmltransformer__write_output_files_no_deleted_records_file_if_not_needed(
def test_xmltransformer_transform_and_write_output_files_no_txt_file_if_not_needed(
tmp_path,
):
output_file = str(tmp_path / "output_file.json")
datacite_records = XmlTransformer.parse_source_file(
"tests/fixtures/datacite/datacite_records.xml"
)
transformer = XmlTransformer("cool-repo", datacite_records)
transformer._write_output_files(output_file)
transformer.transform_and_write_output_files(output_file)
assert len(list(tmp_path.iterdir())) == 1
assert next(tmp_path.iterdir()).name == "output_file.json"

Expand Down
2 changes: 1 addition & 1 deletion transmogrifier/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def main(source, input_file, output_file, verbose):
logger.info("Running transform for source %s", source)

transformer = Transformer.load(source, input_file)
transformer._write_output_files(output_file)
transformer.transform_and_write_output_files(output_file)
logger.info(
(
"Completed transform, total records processed: %d, "
Expand Down
31 changes: 22 additions & 9 deletions transmogrifier/sources/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ def __next__(self) -> TimdexRecord:
continue

@final
def _write_output_files(self, output_file: str) -> None:
self.write_timdex_records_to_json_file(output_file)
def transform_and_write_output_files(self, output_file: str) -> None:
"""Iterates through source records to transform and write to output files.
Args:
output_file: The name of the output files.
"""
self._write_timdex_records_to_json_file(output_file)
if self.processed_record_count == 0:
raise ValueError(
"No records processed from input file, needs investigation"
Expand All @@ -80,16 +85,17 @@ def _write_output_files(self, output_file: str) -> None:
deleted_output_file = output_file.replace("index", "delete").replace(
"json", "txt"
)
self.write_deleted_records_to_txt_file(deleted_records, deleted_output_file)
self._write_deleted_records_to_txt_file(
deleted_records, deleted_output_file
)

@final
def write_timdex_records_to_json_file(self, output_file: str) -> int:
def _write_timdex_records_to_json_file(self, output_file: str) -> int:
"""
Write TIMDEX records to JSON file.
Args:
output_file: The JSON file used for writing TIMDEX records.
"""
count = 0
try:
Expand Down Expand Up @@ -121,10 +127,16 @@ def write_timdex_records_to_json_file(self, output_file: str) -> int:

@final
@staticmethod
def write_deleted_records_to_txt_file(
deleted_records: list[str], output_file_path: str
def _write_deleted_records_to_txt_file(
deleted_records: list[str], output_file: str
):
with open(output_file_path, "w") as file:
"""Write deleted records to the specified text file.
Args:
deleted_records: The deleted records to write to file.
output_file: The text file used for writing deleted records.
"""
with open(output_file, "w") as file:
for record_id in deleted_records:
file.write(f"{record_id}\n")

Expand Down Expand Up @@ -232,6 +244,7 @@ def get_required_fields(self, source_record: JSON | Tag) -> dict:
pass

@classmethod
@abstractmethod
def get_main_titles(cls, source_record: JSON | Tag) -> list[str]:
"""
Retrieve main title(s) from an source record.
Expand All @@ -241,7 +254,7 @@ def get_main_titles(cls, source_record: JSON | Tag) -> list[str]:
Args:
source_record: A single source record.
"""
return []
pass

@classmethod
@abstractmethod
Expand Down

0 comments on commit c378408

Please sign in to comment.