From 83112668622049ad14a0623ecb2bde42eb47c8be Mon Sep 17 00:00:00 2001 From: Jason Raitz Date: Thu, 30 Apr 2026 14:22:05 -0400 Subject: [PATCH] updated write_search_results_to_file - adds an optional output_dir parameter to overide the default --- CHANGELOG.md | 6 ++++++ README.md | 7 +++++++ tind_client/client.py | 10 ++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 847470b..7a34b7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.2.3] + +### Added +- parameter to client.write_search_results_to_file() to specify an output directory for the file, with fallback to default_storage_dir if not provided +- also uses PATH + ## [0.2.2] ### Changed diff --git a/README.md b/README.md index 5926422..2753778 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,13 @@ pymarc_results = client.search("collection:'Disabled Students Program Photos'", # search Tind with a query and write results to an XML file in the default storage directory records_written = client.write_search_results_to_file("Old Emperor Norton", "full_norton_results.xml") + +# write search results to a specific directory +records_written = client.write_search_results_to_file( + "Old Emperor Norton", + "full_norton_results.xml", + output_dir="/path/to/some/directory" +) ``` ## Running tests diff --git a/tind_client/client.py b/tind_client/client.py index f044520..77b9883 100644 --- a/tind_client/client.py +++ b/tind_client/client.py @@ -178,12 +178,14 @@ def search(self, query: str, result_format: str = "xml") -> list[Any]: return recs def write_search_results_to_file( - self, query: str = "", output_file_name: str = "tind.xml" + self, query: str = "", output_file_name: str = "tind.xml", output_dir: str = "" ) -> int: """Search TIND and stream results to an XML file. :param str query: A TIND search query string. :param str output_file_name: filename for the output XML file. + :param str output_dir: Directory in which to save the file. + Falls back to ``default_storage_dir`` when empty. :returns int: The number of records written to the file. """ @@ -192,9 +194,9 @@ def write_search_results_to_file( return 0 recs_written = 0 - output_path = os.path.join(self.default_storage_dir, output_file_name) + output_path = Path(output_dir or self.default_storage_dir) / output_file_name try: - with open(output_path, "w", encoding="utf-8") as f: + with output_path.open("w", encoding="utf-8") as f: f.write(f'\n\n') for record in self._iter_xml_records(query): record_xml = E.tostring(record, encoding="unicode") @@ -206,7 +208,7 @@ def write_search_results_to_file( raise TINDError(f"Matched {total_hits} tind ids, but API did not return any.") f.write("\n") except Exception: - Path(output_path).unlink(missing_ok=True) + output_path.unlink(missing_ok=True) raise if recs_written != total_hits: