MAJOR: solves problem related to ELABFTW_API_URL variable

if no value was specified for such variable (or .env was missing)
EAU would be set to None and get stuck in a prompt loop

solved by turning EAU into a required variable in APIHandler
(and editing a lot of code through all of src/)
This commit is contained in:
2026-05-14 17:24:02 +02:00
parent 1ce381f341
commit 685d15d55b
3 changed files with 56 additions and 36 deletions

View File

@@ -134,7 +134,7 @@ class Layer:
self.start_time = layer_data.get("created_at") or None
self.description = layer_data.get("body") or None
def get_instruments(self, api_key):
def get_instruments(self, api_key, ELABFTW_API_URL):
"""
Retruns a dictionary of all the instruments used to create the layer.
The format of the dictionary is:
@@ -144,18 +144,20 @@ class Layer:
"rheed_system": str
}
Arg: api_key: str: A valid API key for the eLabFTW instance where the data is stored, with permissions to access the relevant entries.
eLabFTW's API keys are well documented here: https://doc.elabftw.net/docs/usage/api/.
If you don't have an API key and are uncapable of creating one, contact your eLabFTW administrator.
Or RTFM and create one yourself, it's not that hard.
Args:
api_key: str: A valid API key for the eLabFTW instance where the data is stored, with permissions to access the relevant entries.
eLabFTW's API keys are well documented here: https://doc.elabftw.net/docs/usage/api/.
If you don't have an API key and are uncapable of creating one, contact your eLabFTW administrator.
Or RTFM and create one yourself, it's not that hard.
ELABFTW_API_URL: str: URL for the API root endpoint of the eLabFTW instance. Ends with '/api/v2' - no trailing slash.
"""
raw_lasersys_data = APIHandler(api_key).get_entry_from_elabid(
raw_lasersys_data = APIHandler(api_key, ELABFTW_API_URL).get_entry_from_elabid(
self.laser_system_elabid, entryType="items"
)
raw_chamber_data = APIHandler(api_key).get_entry_from_elabid(
raw_chamber_data = APIHandler(api_key, ELABFTW_API_URL).get_entry_from_elabid(
self.chamber_elabid, entryType="items"
)
raw_rheedsys_data = APIHandler(api_key).get_entry_from_elabid(
raw_rheedsys_data = APIHandler(api_key, ELABFTW_API_URL).get_entry_from_elabid(
self.rheed_system_elabid, entryType="items"
)
instruments_used = {
@@ -248,6 +250,7 @@ class Entrypoint:
* linked_experiments_elabid: list: List of eLabFTW internal id's of the experiments linked to the entrypoint.
"""
try:
self.name = sample_data["title"]
self.extra = sample_data["metadata_decoded"]["extra_fields"]
self.linked_items = sample_data["items_links"] # dict
self.batch_elabid = self.extra["Substrate batch"]["value"] # elabid
@@ -262,11 +265,6 @@ class Entrypoint:
raise KeyError(
f'The provided dictionary lacks a "{k}" key. Check the sample entry on eLabFTW and make sure you used the correct Resource template.'
)
# Non-required attributes:
self.name = (
sample_data.get("title") or None
) # error prevention is more important than preventing empty fields here
# although I don't think it's even possible to fuck up this bad...
class Material:
@@ -310,7 +308,7 @@ class Material:
f'The provided dictionary lacks a "{k}" key. Check the target/substrate entry on eLabFTW and make sure you used the correct Resource template.'
)
def get_compound_data(self, apikey):
def get_compound_data(self, apikey, ELABFTW_API_URL):
"""
Returns a dictionary with the relevant data on the compound of which the material is made.
The format of the dictionary is:
@@ -320,12 +318,14 @@ class Material:
"cas_number": str
}
Arg: api_key: str: A valid API key for the eLabFTW instance where the data is stored, with permissions to access the relevant entries.
eLabFTW's API keys are well documented here: https://doc.elabftw.net/docs/usage/api/.
If you don't have an API key and are uncapable of creating one, contact your eLabFTW administrator.
Or RTFM and create one yourself, it's not that hard.
Args:
api_key: str: A valid API key for the eLabFTW instance where the data is stored, with permissions to access the relevant entries.
eLabFTW's API keys are well documented here: https://doc.elabftw.net/docs/usage/api/.
If you don't have an API key and are uncapable of creating one, contact your eLabFTW administrator.
Or RTFM and create one yourself, it's not that hard.
ELABFTW_API_URL: str: URL for the API root endpoint of the eLabFTW instance. Ends with '/api/v2' - no trailing slash.
"""
raw_compound_data = APIHandler(apikey).get_entry_from_elabid(
raw_compound_data = APIHandler(apikey, ELABFTW_API_URL).get_entry_from_elabid(
self.compound_elabid, entryType="items"
)
name = raw_compound_data["title"]
@@ -339,8 +339,20 @@ class Material:
}
return compound_data
def get_compound_formula(self, apikey):
formula = self.get_compound_data(apikey).get("chemical_formula")
def get_compound_formula(self, apikey, ELABFTW_API_URL):
"""
Returns a string with the chemical formula of the compound.
Args:
api_key: str: A valid API key for the eLabFTW instance where the data is stored, with permissions to access the relevant entries.
eLabFTW's API keys are well documented here: https://doc.elabftw.net/docs/usage/api/.
If you don't have an API key and are uncapable of creating one, contact your eLabFTW administrator.
Or RTFM and create one yourself, it's not that hard.
ELABFTW_API_URL: str: URL for the API root endpoint of the eLabFTW instance. Ends with '/api/v2' - no trailing slash.
"""
formula = self.get_compound_data(apikey, ELABFTW_API_URL).get(
"chemical_formula"
)
return formula
@@ -454,7 +466,8 @@ if __name__ == "__main__":
# print(f"Example header:\n\t{head.header}\n")
# print("Warning: you're not supposed to be running this as the main program.")
api_key = getpass("Paste API key here [no echo]: ")
handler = APIHandler(api_key=api_key)
ELABFTW_API_URL = input("Enter a valid eLabFTW API URL (ends with '/api/v2)': ")
handler = APIHandler(api_key, ELABFTW_API_URL)
exp58 = handler.get_entry_from_elabid(elabid=58, entryType="experiments")
layer58 = Layer(exp58)
print(layer58.list_attachments())