diff --git a/src/classes.py b/src/classes.py index 813a7fa..c3d41f9 100644 --- a/src/classes.py +++ b/src/classes.py @@ -331,6 +331,14 @@ class Target(Material): self.description = material_data.get("body") or "" +class Proposal: + def __init__(self, proposal_data): + if "Proposal " in proposal_data["title"]: + self.name = proposal_data["title"].replace("Proposal ", "") + else: + self.name = proposal_data["title"] + + if __name__ == "__main__": # head = APIHandler("MyApiKey-123456789abcdef") # print(f"Example header:\n\t{head.header}\n") diff --git a/src/main.py b/src/main.py index 0cbc2a3..c94d8c0 100755 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 import os, json, requests, h5py import numpy as np + +# import dotenv from getpass import getpass from APIHandler import APIHandler from classes import * @@ -85,6 +87,27 @@ def call_layers_from_list(elabid_list): return list_of_layers # list of Layer-class objects +def call_proposal_from_elabid(elabid): + try: + proposal_data = APIHandler(api_key).get_entry_from_elabid( + elabid, entryType="items" + ) + proposal_category = proposal_data.get("category_title") + # TO-DO: correct this typo on elabftw: Subtrate → Substrate. + if ( + "Proposal" not in proposal_category + ): # to avoid that same old problem with trailing spaces + print(f"Category of the resource: {proposal_category}.") + raise ValueError( + f"The referenced resource (elabid = {elabid}) is not a proposal." + ) + else: + proposal = Proposal(proposal_data) + except ConnectionError as e: + raise ConnectionError(e) + return proposal.name # String + + def chain_entrypoint_to_batch(sample_object): """ Takes an Entrypoint-class object, looks at its .batch_elabid attribute and returns a Material-class object containing data on the substrate batch associated to the starting sample. @@ -877,8 +900,9 @@ if __name__ == "__main__": data = handler.get_entry_from_elabid(elabid) sample = Entrypoint(data) sample_name = sample.name.strip().replace(" ", "_") + operative_unit = "Napoli" if sample.proposal: - sample_proposal = sample.proposal.strip().replace(" ", "_") + sample_proposal = call_proposal_from_elabid(sample.proposal) else: sample_proposal = None substrate_object = chain_entrypoint_to_batch(sample) # Substrate-class object @@ -889,7 +913,8 @@ if __name__ == "__main__": fn_base = ( "nffa-di_" + (f"{sample_proposal}_" if sample_proposal else "") - + "Napoli_" + + operative_unit + + "_" + sample_name ) with open(f"output/{fn_base}.json", "w") as f: