Skip to content

Commit 4c685fa

Browse files
committed
updated write_search_results_to_file
- adds an optional output_dir parameter to overide the default
1 parent 6757e7b commit 4c685fa

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased - target 0.2.3]
9+
10+
### Added
11+
- 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
12+
- also uses PATH
13+
814
## [0.2.2]
915

1016
### Changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ pymarc_results = client.search("collection:'Disabled Students Program Photos'",
8181

8282
# search Tind with a query and write results to an XML file in the default storage directory
8383
records_written = client.write_search_results_to_file("Old Emperor Norton", "full_norton_results.xml")
84+
85+
# write search results to a specific directory
86+
records_written = client.write_search_results_to_file(
87+
"Old Emperor Norton",
88+
"full_norton_results.xml",
89+
output_dir="/path/to/some/directory"
90+
)
8491
```
8592

8693
## Running tests

tind_client/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,14 @@ def search(self, query: str, result_format: str = "xml") -> list[Any]:
178178
return recs
179179

180180
def write_search_results_to_file(
181-
self, query: str = "", output_file_name: str = "tind.xml"
181+
self, query: str = "", output_file_name: str = "tind.xml", output_dir: str = ""
182182
) -> int:
183183
"""Search TIND and stream results to an XML file.
184184
185185
:param str query: A TIND search query string.
186186
:param str output_file_name: filename for the output XML file.
187+
:param str output_dir: Directory in which to save the file.
188+
Falls back to ``default_storage_dir`` when empty.
187189
:returns int: The number of records written to the file.
188190
"""
189191

@@ -192,9 +194,9 @@ def write_search_results_to_file(
192194
return 0
193195

194196
recs_written = 0
195-
output_path = os.path.join(self.default_storage_dir, output_file_name)
197+
output_path = Path(output_dir or self.default_storage_dir) / output_file_name
196198
try:
197-
with open(output_path, "w", encoding="utf-8") as f:
199+
with output_path.open("w", encoding="utf-8") as f:
198200
f.write(f'<?xml version="1.0" encoding="UTF-8"?>\n<collection xmlns="{NS}">\n')
199201
for record in self._iter_xml_records(query):
200202
record_xml = E.tostring(record, encoding="unicode")
@@ -206,7 +208,7 @@ def write_search_results_to_file(
206208
raise TINDError(f"Matched {total_hits} tind ids, but API did not return any.")
207209
f.write("</collection>\n")
208210
except Exception:
209-
Path(output_path).unlink(missing_ok=True)
211+
output_path.unlink(missing_ok=True)
210212
raise
211213

212214
if recs_written != total_hits:

0 commit comments

Comments
 (0)