turns apikey into global variable

This commit is contained in:
2026-01-26 15:56:45 +01:00
parent 7ce544ebe7
commit 0d224b14b5

View File

@@ -20,7 +20,7 @@ def valid_elabfiles(path):
pass pass
return elabfiles return elabfiles
def call_sample(apikey, elabid, SERVER_URL="https://elabftw.fisica.unina.it/"): # TO-DO: rm default server def call_sample(elabid, SERVER_URL="https://elabftw.fisica.unina.it/"): # TO-DO: rm default server
'''Queries the Resources (/items) API endpoint '''Queries the Resources (/items) API endpoint
of eLabFTW instance to request data (JSON) of eLabFTW instance to request data (JSON)
on a certain sample given its eLab-ID. on a certain sample given its eLab-ID.
@@ -40,14 +40,13 @@ def call_sample(apikey, elabid, SERVER_URL="https://elabftw.fisica.unina.it/"):
) )
return sample.json() return sample.json()
def id2sample(apikey, elabid): def id2sample(elabid):
'''Fetches sample data (JSON) from eLabFTW '''Fetches sample data (JSON) from eLabFTW
instance (using function "call_sample()") instance (using function "call_sample()")
and extracts significant information. and extracts significant information.
Currently, it only returns the sample's title.''' Currently, it only returns the sample's title.'''
#apikey = getpass("Paste API key here: ") # move outside loops sample_data = call_sample(elabid)
sample_data = call_sample(apikey, elabid)
sample_title = sample_data["title"] sample_title = sample_data["title"]
return sample_title return sample_title
@@ -56,13 +55,12 @@ def fetch_and_group(path):
files in a given folder, then files in a given folder, then
''' '''
sample_dict = {} sample_dict = {}
apikey = getpass("Paste API key here: ")
for filename in valid_elabfiles(path): for filename in valid_elabfiles(path):
with open(os.path.join(path, filename), "r") as f: with open(os.path.join(path, filename), "r") as f:
layer = json.load(f) layer = json.load(f)
extra = layer["metadata_decoded"]["extra_fields"] extra = layer["metadata_decoded"]["extra_fields"]
sample_id = extra["Sample"]["value"] sample_id = extra["Sample"]["value"]
sample_title = id2sample(apikey, sample_id) sample_title = id2sample(sample_id)
lpn = int(extra["Layer Progressive Number"]["value"]) # Layer Progressive Number lpn = int(extra["Layer Progressive Number"]["value"]) # Layer Progressive Number
if not sample_dict.get(sample_title): # if not existent yet, initialize if not sample_dict.get(sample_title): # if not existent yet, initialize
sample_dict[sample_title] = { sample_dict[sample_title] = {
@@ -82,5 +80,6 @@ def fetch_and_group(path):
} }
return sample_dict return sample_dict
apikey = getpass("Paste API key here: ")
sample_dict = fetch_and_group("./tests/objects") sample_dict = fetch_and_group("./tests/objects")
print(json.dumps(sample_dict, indent=3)) print(json.dumps(sample_dict, indent=3))