first attempt to refactor the test code into something more elegant
This commit is contained in:
87
src/main.py
87
src/main.py
@@ -1,46 +1,49 @@
|
||||
import os, json, requests
|
||||
from getpass import getpass
|
||||
from classes import Header
|
||||
from APIHandler import APIHandler
|
||||
from classes import *
|
||||
|
||||
def get_sample_layers_data(elabid):
|
||||
'''
|
||||
Return the following data from every eLabFTW experiment linked
|
||||
to a certain sample, identified by elabid.
|
||||
|
||||
- Title of the experiment
|
||||
- Category (should check it's "PLD Deposition")
|
||||
- Layer number - if present (PLD depositions)
|
||||
- Deposition time - returns error if not present
|
||||
- Repetition rate - returns error if not present
|
||||
'''
|
||||
# header = {
|
||||
# "Authorization": apikey,
|
||||
# "Content-Type": "application/json"
|
||||
# }
|
||||
sample_data = requests.get(
|
||||
headers = header,
|
||||
url = f"https://elabftw.fisica.unina.it/api/v2/items/{elabid}",
|
||||
verify=True
|
||||
).json()
|
||||
related_experiments = sample_data["related_experiments_links"]
|
||||
result = []
|
||||
for exp in related_experiments:
|
||||
experiment_data = requests.get(
|
||||
headers = header,
|
||||
url = f"https://elabftw.fisica.unina.it/api/v2/experiments/{exp.get("entityid")}",
|
||||
verify=True
|
||||
).json()
|
||||
extra = experiment_data["metadata_decoded"]["extra_fields"]
|
||||
result.append(
|
||||
{"title": exp.get("title"),
|
||||
"layer_number": extra.get("Layer Progressive Number").get("value"),
|
||||
"category": exp.get("category_title"),
|
||||
"deposition_time": extra.get("Duration").get("value"),
|
||||
"repetition_rate": extra.get("Repetition rate").get("value")}
|
||||
)
|
||||
return result
|
||||
|
||||
apikey = getpass("Paste API key here: ") # consider replacing with .env file
|
||||
header = Header(apikey).dump
|
||||
result = get_sample_layers_data(1108) # edit id at will in case of deletion of remote source
|
||||
print(json.dumps(result))
|
||||
if __name__=="__main__":
|
||||
print(f"=======================\n===== DEBUG MODE! =====\n=======================\n")
|
||||
ELABFTW_API_URL = "https://elabftw.fisica.unina.it/api/v2"
|
||||
apikey = getpass("Paste API key here: ")
|
||||
# TEST. In production the entryType will probably just be "items" since the entrypoint is an item (sample).
|
||||
entryType = None
|
||||
while entryType not in ["items", "experiments"]:
|
||||
eT = input("Enter a valid entry type [items, experiments]: ")
|
||||
# This allows for a shortcut: instead of prompting the type before and the elabid after I can just prompt both at the same time - e.g. e51 is exp. 51, i1108 is item 1108...
|
||||
if eT[0] in ["e", "i"] and eT[-1].isnumeric():
|
||||
try:
|
||||
elabid = int(eT[1:])
|
||||
eT = eT[0]
|
||||
except Exception:
|
||||
print("Usage: i|item|items|i[ELABID] for items, e|experiment|experiments|e[ELABID] for experiments.")
|
||||
pass
|
||||
match eT:
|
||||
case "items" | "i" | "item":
|
||||
entryType = "items"
|
||||
case "experiments" | "e" | "exp" | "experiment":
|
||||
entryType = "experiments"
|
||||
case _:
|
||||
pass
|
||||
# This will probably be reworked in production
|
||||
try:
|
||||
elabid = elabid
|
||||
except NameError:
|
||||
elabid = input("Input elabid here [default = 1111]: ") or 1111
|
||||
data = APIHandler(apikey).get_entry_from_elabid(elabid, entryType)
|
||||
if entryType == "experiments":
|
||||
layer = Layer(data)
|
||||
result = layer.__dict__
|
||||
result.pop("extra")
|
||||
print(result)
|
||||
elif entryType == "items":
|
||||
if data.get("category_title") == "Sample":
|
||||
item = Entrypoint(data)
|
||||
elif data.get("category_title") in ["PLD Target", "Substrate"]:
|
||||
item = Material(data)
|
||||
print(item.get_compound_formula(apikey))
|
||||
result = item.__dict__
|
||||
result.pop("extra")
|
||||
print(result)
|
||||
Reference in New Issue
Block a user