Compare commits

..

4 Commits

Author SHA256 Message Date
38d281543e code cleanup: deletes debug lines from main.py 2026-02-13 00:07:39 +01:00
a12506b8be MAJOR: main.py successfully produces JSON following NeXus-schema
takes API key and elabid of the "entrypoint" sample as input
returns indented JSON with the reconstructed dataset!
currently lacks instruments_used data (matter of minutes)
and all the layer data (already present in Layer-class objects)
2026-02-13 00:01:24 +01:00
43cfd788f3 adds non-req. attr. "description" to class Target 2026-02-12 23:53:21 +01:00
da42de5466 handles error 400 bad request with exit message 2026-02-12 23:52:37 +01:00
3 changed files with 45 additions and 60 deletions

View File

@@ -33,6 +33,8 @@ class APIHandler:
raise ConnectionError(f"Invalid API key or authentication method.")
case 404:
raise ConnectionError(f"404: Not Found. This means there's no resource with this elabid (wrong elabid?) on your eLabFTW (wrong endpoint?).")
case 400:
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.")
case _:
raise ConnectionError(f"HTTP request failed with status code: {response.status_code} (NOTE: 4xx means user's fault).")
else:

View File

@@ -157,6 +157,8 @@ class Target(Material):
self.manufacturer = self.extra["Supplier"]["value"]
except KeyError as k:
raise KeyError(f"The provided dictionary lacks a \"{k}\" key - which is specific for PLD targets. Check the {self.name} target entry on eLabFTW and make sure you used the correct Resource template.")
# Non-required attributes:
self.description = material_data.get("body") or ""

View File

@@ -98,71 +98,52 @@ def chain_layer_to_target(layer_object):
material_object = call_material_from_elabid(target_elabid)
return material_object
#sample_object = call_entrypoint_from_elabid(elabid)
#from_entrypoint_to_material(sample_object)
def make_nexus_schema_dictionary(substrate_object, layers):
pld_fabrication = {
"sample": {
"substrate": {
"name": substrate_object.name,
"chemical_formula" : substrate_object.get_compound_formula(apikey),
"orientation" : substrate_object.orientation,
"miscut_angle" : substrate_object.miscut_angle,
"miscut_direction" : substrate_object.miscut_direction,
"thickness" : substrate_object.thickness,
"dimensions" : substrate_object.dimensions,
"surface_treatment" : substrate_object.surface_treatment,
"manufacturer" : substrate_object.manufacturer,
"batch_id" : substrate_object.batch_id,
},
"multilayer": {},
},
}
multilayer = pld_fabrication["sample"]["multilayer"]
for layer in layers:
name = "layer_" + layer.layer_number
target_object = chain_layer_to_target(layer)
target_dict = {
"name": target_object.name,
"chemical_formula" : target_object.get_compound_formula(apikey),
"description" : target_object.description,
"shape" : target_object.shape,
"dimensions" : target_object.dimensions,
"thickness" : target_object.thickness,
"solid_form" : target_object.solid_form,
"manufacturer" : target_object.manufacturer,
# TO-DO: currently not available:
# "batch_id" : target_object.batch_id,
}
multilayer[name] = {
"target": target_dict
}
return json.dumps(pld_fabrication, indent=2)
if __name__=="__main__":
print(f"=======================\n===== DEBUG MODE! =====\n=======================\n")
# TO-DO: place the API base URL somewhere else.
ELABFTW_API_URL = "https://elabftw.fisica.unina.it/api/v2"
apikey = getpass("Paste API key here: ")
elabid = input("Enter elabid of your starting sample [default= 1111]: ") or 1111
data = APIHandler(apikey).get_entry_from_elabid(elabid)
sample = Entrypoint(data)
batch = chain_entrypoint_to_batch(sample) # Material-class object
bd = batch.__dict__
bd.pop("extra")
substrate_object = chain_entrypoint_to_batch(sample) # Substrate-class object
layers = chain_entrypoint_to_layers(sample) # list of Layer-class objects
print(f"Sample name:\n{sample.name}\n")
print(f"Substrate data:\n{bd}\n")
print(f"Layers data:")
for layer in layers:
ld = layer.__dict__
ld.pop("extra")
tgt = chain_layer_to_target(layer)
td = tgt.__dict__
td.pop("extra")
print(ld)
print(td)
print()
# 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.")
# continue
# match eT:
# case "items" | "i" | "item":
# entryType = "items"
# case "experiments" | "e" | "exp" | "experiment":
# entryType = "experiments"
# case _:
# continue
# # 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))
# else:
# raise Exception("The selected item or experiment is not in one of the following categories: [Sample, PLD Target, Substrate, PLD Deposition].")
# result = item.__dict__
# result.pop("extra")
# print(result)
print(make_nexus_schema_dictionary(substrate_object, layers)) # debug