Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

## [Unreleased - target 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
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions tind_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i understand this (based on getting up to speed on in slack) but i think adding output_dir honestly makes this more confusing. imo, output_file_name should just take a str | os.PathLike-typed value. but shrug if we're thinking about refactoring this soon anyway...

) -> 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.
"""

Expand All @@ -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'<?xml version="1.0" encoding="UTF-8"?>\n<collection xmlns="{NS}">\n')
for record in self._iter_xml_records(query):
record_xml = E.tostring(record, encoding="unicode")
Expand All @@ -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("</collection>\n")
except Exception:
Path(output_path).unlink(missing_ok=True)
output_path.unlink(missing_ok=True)
raise

if recs_written != total_hits:
Expand Down