Compare commits
2 Commits
f84478a7a4
...
7e808509cc
| Author | SHA256 | Date | |
|---|---|---|---|
| 7e808509cc | |||
| 2bbab96ca7 |
@@ -65,12 +65,12 @@ class APIHandler:
|
|||||||
case 404:
|
case 404:
|
||||||
# Lapalissian:
|
# Lapalissian:
|
||||||
raise ConnectionError(
|
raise ConnectionError(
|
||||||
f"404: Not Found. This means there's no resource with this elabid (wrong elabid?) on your eLabFTW (wrong endpoint?)."
|
"404: Not Found. This means there's no resource with this elabid (wrong elabid?) on your eLabFTW (wrong endpoint?)."
|
||||||
)
|
)
|
||||||
case 400:
|
case 400:
|
||||||
# I genuinely have no idea:
|
# I genuinely have no idea:
|
||||||
raise ConnectionError(
|
raise ConnectionError(
|
||||||
f"400: Bad Request. This means the API endpoint you tried to reach is invalid. Did you tamper with the source code? If not, contact the developer."
|
"400: Bad Request. This means the API endpoint you tried to reach is invalid. Did you tamper with the source code? If not, contact the developer."
|
||||||
)
|
)
|
||||||
case _:
|
case _:
|
||||||
# For some fucking reason, this is the only error I actually get from the API...
|
# For some fucking reason, this is the only error I actually get from the API...
|
||||||
|
|||||||
@@ -331,6 +331,14 @@ class Target(Material):
|
|||||||
self.description = material_data.get("body") or ""
|
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__":
|
if __name__ == "__main__":
|
||||||
# head = APIHandler("MyApiKey-123456789abcdef")
|
# head = APIHandler("MyApiKey-123456789abcdef")
|
||||||
# print(f"Example header:\n\t{head.header}\n")
|
# print(f"Example header:\n\t{head.header}\n")
|
||||||
|
|||||||
29
src/main.py
29
src/main.py
@@ -1,6 +1,8 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os, json, requests, h5py
|
import os, json, requests, h5py
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
# import dotenv
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
from APIHandler import APIHandler
|
from APIHandler import APIHandler
|
||||||
from classes import *
|
from classes import *
|
||||||
@@ -85,6 +87,27 @@ def call_layers_from_list(elabid_list):
|
|||||||
return list_of_layers # list of Layer-class objects
|
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):
|
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.
|
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)
|
data = handler.get_entry_from_elabid(elabid)
|
||||||
sample = Entrypoint(data)
|
sample = Entrypoint(data)
|
||||||
sample_name = sample.name.strip().replace(" ", "_")
|
sample_name = sample.name.strip().replace(" ", "_")
|
||||||
|
operative_unit = "Napoli"
|
||||||
if sample.proposal:
|
if sample.proposal:
|
||||||
sample_proposal = sample.proposal.strip().replace(" ", "_")
|
sample_proposal = call_proposal_from_elabid(sample.proposal)
|
||||||
else:
|
else:
|
||||||
sample_proposal = None
|
sample_proposal = None
|
||||||
substrate_object = chain_entrypoint_to_batch(sample) # Substrate-class object
|
substrate_object = chain_entrypoint_to_batch(sample) # Substrate-class object
|
||||||
@@ -889,7 +913,8 @@ if __name__ == "__main__":
|
|||||||
fn_base = (
|
fn_base = (
|
||||||
"nffa-di_"
|
"nffa-di_"
|
||||||
+ (f"{sample_proposal}_" if sample_proposal else "")
|
+ (f"{sample_proposal}_" if sample_proposal else "")
|
||||||
+ "Napoli_"
|
+ operative_unit
|
||||||
|
+ "_"
|
||||||
+ sample_name
|
+ sample_name
|
||||||
)
|
)
|
||||||
with open(f"output/{fn_base}.json", "w") as f:
|
with open(f"output/{fn_base}.json", "w") as f:
|
||||||
|
|||||||
Reference in New Issue
Block a user