Skip to content

Commit

Permalink
Added the input_source field to the Doi dataclass, and input_util.py …
Browse files Browse the repository at this point in the history
…to assign the field as it parsed DOI objects from input files

The action classes then provide this input source path to the transaction builder so only a single file is copied per transaction.
Previously, providing a directory with multiple input files resulted in an entire copy of the directory being made for each transaction.
  • Loading branch information
Scott Collins committed Nov 18, 2021
1 parent b8a2527 commit 4199fb9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
19 changes: 13 additions & 6 deletions src/pds_doi_service/core/actions/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,33 +267,40 @@ def run(self, **kwargs):
dois = self._complete_dois(dois)
dois = self._validate_dois(dois)

for doi in dois:
for input_doi in dois:
# Create a JSON format label to send to the service provider
io_doi_label = self._record_service.create_doi_record(doi, content_type=CONTENT_TYPE_JSON)
io_doi_label = self._record_service.create_doi_record(input_doi, content_type=CONTENT_TYPE_JSON)

# If the next step is to release, submit to the service provider and
# use the response label for the local transaction database entry
if not self._review:
# Determine the correct HTTP verb and URL for submission of this DOI
method, url = self._web_client.endpoint_for_doi(doi, self._name)
method, url = self._web_client.endpoint_for_doi(input_doi, self._name)

doi, o_doi_label = self._web_client.submit_content(
output_doi, o_doi_label = self._web_client.submit_content(
url=url, method=method, payload=io_doi_label, content_type=CONTENT_TYPE_JSON
)
# Otherwise, DOI object is ready to be logged
else:
output_doi = input_doi

# Otherwise, if the next step is review, the label we've already
# created has marked all the Doi's as being the "review" step
# so its ready to be submitted to the local transaction history
transaction = self.m_transaction_builder.prepare_transaction(
self._node, self._submitter, doi, input_path=self._input, output_content_type=CONTENT_TYPE_JSON
self._node,
self._submitter,
output_doi,
input_path=input_doi.input_source,
output_content_type=CONTENT_TYPE_JSON,
)

# Commit the transaction to the local database
transaction.log()

# Append the latest version of the Doi object to return
# as a label
output_dois.append(doi)
output_dois.append(output_doi)
# Propagate input format exceptions, force flag should not affect
# these being raised and certain callers (such as the API) look
# for this exception specifically
Expand Down
19 changes: 11 additions & 8 deletions src/pds_doi_service/core/actions/reserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,29 +245,32 @@ def run(self, **kwargs):
dois = self._complete_dois(dois)
dois = self._validate_dois(dois)

for doi in dois:
for input_doi in dois:
# Create the JSON request label to send
io_doi_label = self._record_service.create_doi_record(doi, content_type=CONTENT_TYPE_JSON)
io_doi_label = self._record_service.create_doi_record(input_doi, content_type=CONTENT_TYPE_JSON)

# Submit the Reserve request
# Determine the correct HTTP verb and URL for submission of this DOI
method, url = self._web_client.endpoint_for_doi(doi, self._name)
method, url = self._web_client.endpoint_for_doi(input_doi, self._name)

doi, o_doi_label = self._web_client.submit_content(
output_doi, o_doi_label = self._web_client.submit_content(
method=method, url=url, payload=io_doi_label, content_type=CONTENT_TYPE_JSON
)

# Log the inputs and outputs of this transaction
transaction = self.m_transaction_builder.prepare_transaction(
self._node, self._submitter, doi, input_path=self._input, output_content_type=CONTENT_TYPE_JSON
self._node,
self._submitter,
output_doi,
input_path=input_doi.input_source,
output_content_type=CONTENT_TYPE_JSON,
)

# Commit the transaction to the local database
transaction.log()

# Append the latest version of the Doi object to return
# as a label
output_dois.append(doi)
# Append the latest version of the Doi object to return as a label
output_dois.append(output_doi)

# Propagate input format exceptions, force flag should not affect
# these being raised and certain callers (such as the API) look
Expand Down
2 changes: 1 addition & 1 deletion src/pds_doi_service/core/actions/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def run(self, **kwargs):

for doi in dois:
transaction = self.m_transaction_builder.prepare_transaction(
self._node, self._submitter, doi, input_path=self._input, output_content_type=CONTENT_TYPE_JSON
self._node, self._submitter, doi, input_path=doi.input_source, output_content_type=CONTENT_TYPE_JSON
)

transaction.log()
Expand Down
4 changes: 1 addition & 3 deletions src/pds_doi_service/core/entities/doi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class DoiStatus(str, Enum):
An error has occurred with the DOI submission.
Unknown -
Default starting state for DOI transactions.
Reserve_not_submitted -
DOI reserve request in local database, but not published/released.
Used for testing of the reserve action.
Reserved -
DOI reserve request submitted, but not yet published/released.
Draft -
Expand Down Expand Up @@ -127,3 +124,4 @@ class Doi:
date_record_added: Optional[datetime] = None
date_record_updated: Optional[datetime] = None
event: Optional[DoiEvent] = None
input_source: Optional[str] = None
20 changes: 19 additions & 1 deletion src/pds_doi_service/core/input/input_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ def _read_from_path(self, path):
Returns
-------
dois : list[doi]
The list of Doi objects parsed from the provided path.
Raises
-------
InputFormatException
If an error is encountered while reading a local file.
Expand All @@ -526,6 +531,12 @@ def _read_from_path(self, path):

logger.error(msg)
raise InputFormatException(msg)

# Make a note of where we can find the original input file that
# resulted in these DOI's so we can save it to the transaction
# history later on
for doi in dois:
doi.input_source = path
else:
logger.info("File %s has unsupported extension, ignoring", path)
else:
Expand Down Expand Up @@ -580,7 +591,14 @@ def _read_from_remote(self, input_url):
temp_file.write(response.content)
temp_file.seek(0)

return self._read_from_path(temp_file.name)
dois = self._read_from_path(temp_file.name)

# Update input source to point to original URL, as the temp file paths
# assigned by _read_from_path no longer exist
for doi in dois:
doi.input_source = input_url

return dois

def parse_dois_from_input_file(self, input_file):
"""
Expand Down

0 comments on commit 4199fb9

Please sign in to comment.