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
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import os, requests
|
import os, requests
|
||||||
|
from dotenv import load_dotenv
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
import elabapi_python as elabapi
|
import elabapi_python as elabapi
|
||||||
|
|
||||||
@@ -20,10 +21,13 @@ class APIHandler:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# TO-DO: remove static url.
|
# TO-DO: remove static url.
|
||||||
def __init__(
|
def __init__(self, api_key="", ELABFTW_API_URL=None):
|
||||||
self, api_key="", ELABFTW_API_URL="https://elabftw.fisica.unina.it/api/v2"
|
|
||||||
):
|
|
||||||
"""Init method, api_key suggested but not required (empty by default)."""
|
"""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.api_key = api_key
|
||||||
self.auth = {"Authorization": api_key}
|
self.auth = {"Authorization": api_key}
|
||||||
self.content = {"Content-Type": "application/json"}
|
self.content = {"Content-Type": "application/json"}
|
||||||
|
|||||||
19
src/main.py
19
src/main.py
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os, json, requests, h5py
|
import os, json, requests, h5py
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import dotenv
|
from dotenv import load_dotenv
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
from APIHandler import APIHandler
|
from APIHandler import APIHandler
|
||||||
from classes import *
|
from classes import *
|
||||||
@@ -91,7 +91,7 @@ def call_layers_from_list(elabid_list):
|
|||||||
+ str(e)
|
+ str(e)
|
||||||
+ f"\nPlease solve the problem before retrying."
|
+ f"\nPlease solve the problem before retrying."
|
||||||
+ "\n\n"
|
+ "\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
|
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__":
|
if __name__ == "__main__":
|
||||||
# TO-DO: place the API base URL somewhere else.
|
load_dotenv()
|
||||||
ELABFTW_API_URL = "https://elabftw.fisica.unina.it/api/v2"
|
api_key = os.getenv("api_key") or getpass("Paste API key here: ")
|
||||||
api_key = getpass("Paste API key here: ")
|
elabid = (
|
||||||
elabid = input("Enter elabid of your starting sample [default = 1111]: ") or 1111
|
os.getenv("elabid")
|
||||||
|
or input("Enter elabid of your starting sample [default = 1111]: ")
|
||||||
|
or 1111
|
||||||
|
)
|
||||||
handler = APIHandler(api_key)
|
handler = APIHandler(api_key)
|
||||||
data = handler.get_entry_from_elabid(elabid)
|
data = handler.get_entry_from_elabid(elabid)
|
||||||
sample = Entrypoint(data)
|
sample = Entrypoint(data)
|
||||||
sample_name = sample.name.strip().replace(" ", "_")
|
sample_name = sample.name.strip().replace(" ", "_")
|
||||||
operative_unit = "Napoli"
|
operative_unit = os.getenv("operative_unit") or None
|
||||||
if sample.proposal:
|
if sample.proposal:
|
||||||
sample_proposal = call_proposal_from_elabid(sample.proposal)
|
sample_proposal = call_proposal_from_elabid(sample.proposal)
|
||||||
else:
|
else:
|
||||||
@@ -890,7 +893,7 @@ if __name__ == "__main__":
|
|||||||
fn_base = (
|
fn_base = (
|
||||||
"nffa-di_"
|
"nffa-di_"
|
||||||
+ (f"{sample_proposal}_" if sample_proposal else "")
|
+ (f"{sample_proposal}_" if sample_proposal else "")
|
||||||
+ operative_unit
|
+ (f"{operative_unit}_" if operative_unit else "")
|
||||||
+ "_"
|
+ "_"
|
||||||
+ sample_name
|
+ sample_name
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user