From ee96100a73484a3f5e301cf817cb6b3cc74d714d744f507330dad0e7793b9581 Mon Sep 17 00:00:00 2001 From: emanuele Date: Wed, 13 May 2026 12:31:26 +0200 Subject: [PATCH] uses dotenv to store api key and other important variables if a value is not found in .env it will be prompted, but not checked next step is user docs --- src/APIHandler.py | 10 +++++++--- src/main.py | 19 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/APIHandler.py b/src/APIHandler.py index 6a25525..1f3bfa5 100644 --- a/src/APIHandler.py +++ b/src/APIHandler.py @@ -1,4 +1,5 @@ import os, requests +from dotenv import load_dotenv from getpass import getpass import elabapi_python as elabapi @@ -20,10 +21,13 @@ class APIHandler: """ # TO-DO: remove static url. - def __init__( - self, api_key="", ELABFTW_API_URL="https://elabftw.fisica.unina.it/api/v2" - ): + def __init__(self, api_key="", ELABFTW_API_URL=None): """Init method, api_key suggested but not required (empty by default).""" + if not ELABFTW_API_URL: + load_dotenv() + ELABFTW_API_URL = os.getenv("ELABFTW_API_URL") or input( + "Enter a valid eLabFTW API URL (ends with '/api/v2)': " + ) self.api_key = api_key self.auth = {"Authorization": api_key} self.content = {"Content-Type": "application/json"} diff --git a/src/main.py b/src/main.py index 61e5d63..bf2df72 100755 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import os, json, requests, h5py import numpy as np -import dotenv +from dotenv import load_dotenv from getpass import getpass from APIHandler import APIHandler from classes import * @@ -91,7 +91,7 @@ def call_layers_from_list(elabid_list): + str(e) + f"\nPlease solve the problem before retrying." + "\n\n" - + f"Last resource attempted to call: {ELABFTW_API_URL}/experiments/{elabid}" + + f"Last resource attempted to call at base url: /experiments/{elabid}" ) return list_of_layers # list of Layer-class objects @@ -870,15 +870,18 @@ def build_nexus_file(pld_fabrication, output_path="output/nffa-di_unnamed.h5"): if __name__ == "__main__": - # TO-DO: place the API base URL somewhere else. - ELABFTW_API_URL = "https://elabftw.fisica.unina.it/api/v2" - api_key = getpass("Paste API key here: ") - elabid = input("Enter elabid of your starting sample [default = 1111]: ") or 1111 + load_dotenv() + api_key = os.getenv("api_key") or getpass("Paste API key here: ") + elabid = ( + os.getenv("elabid") + or input("Enter elabid of your starting sample [default = 1111]: ") + or 1111 + ) handler = APIHandler(api_key) data = handler.get_entry_from_elabid(elabid) sample = Entrypoint(data) sample_name = sample.name.strip().replace(" ", "_") - operative_unit = "Napoli" + operative_unit = os.getenv("operative_unit") or None if sample.proposal: sample_proposal = call_proposal_from_elabid(sample.proposal) else: @@ -890,7 +893,7 @@ if __name__ == "__main__": fn_base = ( "nffa-di_" + (f"{sample_proposal}_" if sample_proposal else "") - + operative_unit + + (f"{operative_unit}_" if operative_unit else "") + "_" + sample_name )