|
| 1 | +from elinkapi import Elink, ForbiddenException, NotFoundException, WorkflowStatus, Identifier |
| 2 | +import argparse, sys |
| 3 | +from config import TARGET, TOKEN |
| 4 | + |
| 5 | +def audit(logs): |
| 6 | + """ |
| 7 | + Print out details from the audit logs. |
| 8 | + """ |
| 9 | + for log in logs: |
| 10 | + print (f"- {log.type} on {log.audit_date.strftime('%Y-%m-%d %H:%M:%S')}, state {log.status}") |
| 11 | + for message in log.messages: |
| 12 | + print (f" * {message}") |
| 13 | + |
| 14 | +def print_person(p): |
| 15 | + """ |
| 16 | + Print details on a particular Person record. |
| 17 | + """ |
| 18 | + print (" * {0}, {1} {2} {3}".format(p.last_name, |
| 19 | + p.first_name, |
| 20 | + p.middle_name if p.middle_name else "", |
| 21 | + p.contributor_type if p.contributor_type else "")) |
| 22 | + |
| 23 | +def print_organization(o): |
| 24 | + """ |
| 25 | + Print organization details. |
| 26 | + """ |
| 27 | + print (" * {0} {1}".format(o.name, |
| 28 | + o.contributor_type if o.contributor_type else "")) |
| 29 | + |
| 30 | +def info(argv): |
| 31 | + """ |
| 32 | + Retrieve information on a particular OSTI ID if you have access to it. |
| 33 | + """ |
| 34 | + parser = argparse.ArgumentParser("details", description="Find record information at OSTI.") |
| 35 | + parser.add_argument("-i", "--id", help="OSTI ID to find.", type=int, required=True) |
| 36 | + args = parser.parse_args() |
| 37 | + |
| 38 | + # make a link to the API |
| 39 | + api = Elink(target = TARGET, token=TOKEN) |
| 40 | + |
| 41 | + # look for it |
| 42 | + try: |
| 43 | + record = api.get_single_record(args.id) |
| 44 | + |
| 45 | + print (f"Details for record OSTI ID {record.osti_id}") |
| 46 | + print (f"Title: {record.title}") |
| 47 | + print (f"Product type: {record.product_type}") |
| 48 | + |
| 49 | + if record.doi: |
| 50 | + print (f"DOI {record.doi}") |
| 51 | + |
| 52 | + print ("") |
| 53 | + |
| 54 | + if WorkflowStatus.Released.value==record.workflow_status: |
| 55 | + print ("Record RELEASED.") |
| 56 | + elif WorkflowStatus.Saved.value==record.workflow_status: |
| 57 | + print ("Record in SAVED state.") |
| 58 | + elif WorkflowStatus.FailedRelease.value==record.workflow_status or WorkflowStatus.FailedValidation.value==record.workflow_status: |
| 59 | + print ("Record in FAILED state, reasons:") |
| 60 | + |
| 61 | + audit(record.audit_logs) |
| 62 | + elif WorkflowStatus.Validated.value==record.workflow_status: |
| 63 | + print ("Record is PENDING in validated state, logs:") |
| 64 | + |
| 65 | + audit(record.audit_logs) |
| 66 | + else: |
| 67 | + print (f"Record status: {record.workflow_status}") |
| 68 | + |
| 69 | + print (f"Publication Date {record.publication_date}") |
| 70 | + |
| 71 | + print ("Current Revision Dates:") |
| 72 | + print (f" * Added {record.date_metadata_added}") |
| 73 | + print (f" * Updated {record.date_metadata_updated}") |
| 74 | + print (f" * First Submitted {record.date_submitted_to_osti_first}") |
| 75 | + print (f" * Last Submitted {record.date_submitted_to_osti_last}") |
| 76 | + print (f" * First Released {record.date_released_first}") |
| 77 | + print (f" * Last Released {record.date_released_last}") |
| 78 | + |
| 79 | + print (f"Description:\n{record.description}") |
| 80 | + |
| 81 | + print ("Persons:") |
| 82 | + |
| 83 | + print ("- AUTHORS") |
| 84 | + for person in list(filter(lambda x: x.type=="AUTHOR", record.persons)): |
| 85 | + print_person(person) |
| 86 | + |
| 87 | + print ("- CONTRIBUTORS") |
| 88 | + for person in list(filter(lambda c: c.type=="CONTRIBUTING", record.persons)): |
| 89 | + print_person(person) |
| 90 | + |
| 91 | + print ("Organizations:") |
| 92 | + print ("- SPONSORING") |
| 93 | + for organization in list(filter(lambda x: x.type=="SPONSOR", record.organizations)): |
| 94 | + print_organization(organization) |
| 95 | + |
| 96 | + print ("- RESEARCHING") |
| 97 | + for organization in list(filter(lambda x: x.type=="RESEARCHING", record.organizations)): |
| 98 | + print_organization(organization) |
| 99 | + print ("- CONTRIBUTING") |
| 100 | + for organization in list(filter(lambda x: x.type=="CONTRIBUTING", record.organizations)): |
| 101 | + print_organization(organization) |
| 102 | + |
| 103 | + print ("Identifiers:") |
| 104 | + for identifier in record.identifiers: |
| 105 | + print ("- {0} ({1})".format(identifier.value, Identifier.Type(identifier.type).name)) |
| 106 | + |
| 107 | + print ("\nMedia Information:") |
| 108 | + |
| 109 | + if not record.media: |
| 110 | + print ("- No media associated.") |
| 111 | + |
| 112 | + for media in record.media: |
| 113 | + print (f"- Media ID {media.media_id}, added {media.date_added}, updated {media.date_updated}. State {media.status}") |
| 114 | + print (" Files:") |
| 115 | + for file in media.files: |
| 116 | + print(f" * Media File ID {file.media_file_id} Status {file.status}") |
| 117 | + if file.processing_exceptions: |
| 118 | + print(f" - Exceptions: {file.processing_exceptions}") |
| 119 | + |
| 120 | + except ForbiddenException as e: |
| 121 | + print (f"Access denied to ID {args.id}") |
| 122 | + except NotFoundException as e: |
| 123 | + print (f"OSTI ID {args.id} was not found at OSTI.") |
| 124 | + |
| 125 | + |
| 126 | +# When called, run this procedure |
| 127 | +if __name__ == '__main__': |
| 128 | + info(sys.argv[1:]) |
0 commit comments