untested: adds methods to Layer class to fetch attachments list

one method fetches all
one filters textual uploads
one filters png and bmp images
This commit is contained in:
2026-05-08 23:32:59 +02:00
parent 0102bb282e
commit 865f5cab6b

View File

@@ -15,8 +15,10 @@ class Layer:
def __init__(self, layer_data): def __init__(self, layer_data):
try: try:
self.elabid = layer_data["id"]
self.operator = layer_data["fullname"] self.operator = layer_data["fullname"]
self.extra = layer_data["metadata_decoded"]["extra_fields"] self.extra = layer_data["metadata_decoded"]["extra_fields"]
self.uploads = layer_data["uploads"] # dict
self.layer_number = self.extra["Layer Progressive Number"][ self.layer_number = self.extra["Layer Progressive Number"][
"value" "value"
] # integer ] # integer
@@ -127,14 +129,14 @@ class Layer:
self.start_time = layer_data.get("created_at") or None self.start_time = layer_data.get("created_at") or None
self.description = layer_data.get("body") or None self.description = layer_data.get("body") or None
def get_instruments(self, apikey): def get_instruments(self, api_key):
raw_lasersys_data = APIHandler(apikey).get_entry_from_elabid( raw_lasersys_data = APIHandler(api_key).get_entry_from_elabid(
self.laser_system_elabid, entryType="items" self.laser_system_elabid, entryType="items"
) )
raw_chamber_data = APIHandler(apikey).get_entry_from_elabid( raw_chamber_data = APIHandler(api_key).get_entry_from_elabid(
self.chamber_elabid, entryType="items" self.chamber_elabid, entryType="items"
) )
raw_rheedsys_data = APIHandler(apikey).get_entry_from_elabid( raw_rheedsys_data = APIHandler(api_key).get_entry_from_elabid(
self.rheed_system_elabid, entryType="items" self.rheed_system_elabid, entryType="items"
) )
instruments_used = { instruments_used = {
@@ -144,15 +146,61 @@ class Layer:
} }
return instruments_used return instruments_used
# Three possible approaches for the next two methods: either return the raw data as retrieved from eLabFTW, def list_attachments(self):
# or process it and return only the relevant information, or even simply return the list of filenames. """
# Returns a dictionary of all the attachments linked to the layer, where:
def fetch_textual_uploads(self, api_key): * Each key is the attachment's elabid;
""" """ * Each value is a dictionary containing the attachment's filename, hashname and related experiment elabid (= self.elabid).
return
def fetch_rheed_images(self, api_key): Data is already in layer_data, so the API key is unrequired. Same goes for:
return * fetch_textual_uploads() - no arguments;
* fetch_images() - no arguments.
"""
# Remember: Layers are experiments, so we only need to look for attachments in the experiment endpoint.
attachments = {
attachment["id"]: {
"filename": attachment["real_name"],
"hashname": attachment["long_name"],
"related_experiment": attachment["item_id"],
}
for attachment in self.uploads
}
return attachments
def fetch_textual_uploads(self):
"""
Starting from the list of attachments, filters out and returns a list of the textual uploads linked to the layer, which can be either plain text, csv, tsv etc.
Returns only their names, so that the user may select which one to import into the NeXus file as a dataset.
It only looks for .txt, .csv and .tsv files, although it could be easily modified to include other formats.
It is also file extension-sensitive, so anything not ending with .txt, .csv or .tsv won't be retrieved.
That's because the API (v5.3.11) doesn't provide MIME Type or similar metadata on the attachments, so the only way to know if an attachment is an image or not is through its filename.
"""
attachments = self.list_attachments()
textual_uploads = {
attachment: attachments[attachment]
for attachment in attachments
if attachments[attachments]["filename"][-4:] in (".txt", ".csv", ".tsv")
}
return textual_uploads
def fetch_images(self):
"""
Starting from the list of attachments, filters out and returns a Starting from the list of attachments, filters out and returns a list of all the (PNG or BMP) images attached to the layer.
Hopefully one of them is a RHEED pattern.
Returns only their names, so that the user may select which one to import into the NeXus file as a RHEED acquisition.
It only looks for .png and .bmp files, although it could be easily modified to include other formats.
It is also file extension-sensitive, so anything not ending with .png or .bmp won't be retrieved, even if it's an actual image.
That's because the API (v5.3.11) doesn't provide MIME Type or similar metadata on the attachments, so the only way to know if an attachment is an image or not is through its filename.
"""
attachments = self.list_attachments()
pictures = {
attachment: attachments[attachment]
for attachment in attachments
if attachments[attachments]["filename"][-4:] in (".png", ".bmp")
}
return pictures
class Entrypoint: class Entrypoint:
@@ -280,4 +328,3 @@ 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")
print("Warning: you're not supposed to be running this as the main program.") print("Warning: you're not supposed to be running this as the main program.")