main.py code rearranged
now many of the instructions prev. under the __name__ = "__main__" condition have their own function (now called main())
This commit is contained in:
65
src/main.py
65
src/main.py
@@ -873,6 +873,39 @@ def build_nexus_file(pld_fabrication, output_path="output/nffa-di_unnamed.h5"):
|
||||
# * Layer.fetch_images() - dictionary
|
||||
|
||||
|
||||
def main(
|
||||
api_key,
|
||||
ELABFTW_API_URL,
|
||||
elabid,
|
||||
):
|
||||
handler = APIHandler(api_key, ELABFTW_API_URL)
|
||||
sample_raw_data = handler.get_entry_from_elabid(elabid)
|
||||
sample = Entrypoint(sample_raw_data)
|
||||
sample_name = sample.name.strip().replace(
|
||||
" ", "-"
|
||||
) # returns error if no "title" or title is not str
|
||||
# Less important variables:
|
||||
operative_unit = os.getenv("operative_unit") or None
|
||||
if operative_unit:
|
||||
operative_unit = operative_unit.strip().replace(" ", "-")
|
||||
if sample.proposal:
|
||||
sample_proposal = call_proposal_from_elabid(sample.proposal)
|
||||
else:
|
||||
sample_proposal = None
|
||||
substrate_object = chain_entrypoint_to_batch(sample) # Substrate-class object
|
||||
layers = chain_entrypoint_to_layers(sample) # list of Layer-class objects
|
||||
# n_layers = len(layers) # total number of layers on the sample
|
||||
fn_base = (
|
||||
"nffa-di_"
|
||||
+ (f"{sample_proposal}_" if sample_proposal else "")
|
||||
+ (f"{operative_unit}_" if operative_unit else "")
|
||||
+ "PLD_"
|
||||
+ sample_name[:9]
|
||||
)
|
||||
result = make_nexus_schema_dictionary(substrate_object, layers)
|
||||
return {"result": result, "fn_base": fn_base}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
load_dotenv()
|
||||
api_key = os.getenv("api_key") or getpass("Paste API key here: ", echo_char="*")
|
||||
@@ -884,32 +917,16 @@ if __name__ == "__main__":
|
||||
ELABFTW_API_URL = os.getenv("ELABFTW_API_URL") or input(
|
||||
"Enter a valid eLabFTW API URL (ends with '/api/v2)': "
|
||||
)
|
||||
handler = APIHandler(api_key, ELABFTW_API_URL)
|
||||
data = handler.get_entry_from_elabid(elabid)
|
||||
sample = Entrypoint(data)
|
||||
sample_name = sample.name.strip().replace(
|
||||
" ", "-"
|
||||
) # returns error if no "title" or title is not str
|
||||
operative_unit = os.getenv("operative_unit") or None
|
||||
if operative_unit:
|
||||
operative_unit = operative_unit.strip().replace(" ", "-")
|
||||
if sample.proposal:
|
||||
sample_proposal = call_proposal_from_elabid(sample.proposal)
|
||||
else:
|
||||
sample_proposal = None
|
||||
substrate_object = chain_entrypoint_to_batch(sample) # Substrate-class object
|
||||
layers = chain_entrypoint_to_layers(sample) # list of Layer-class objects
|
||||
n_layers = len(layers) # total number of layers on the sample
|
||||
# print(make_nexus_schema_dictionary(substrate_object, layers)) # debug
|
||||
fn_base = (
|
||||
"nffa-di_"
|
||||
+ (f"{sample_proposal}_" if sample_proposal else "")
|
||||
+ (f"{operative_unit}_" if operative_unit else "")
|
||||
+ "PLD_"
|
||||
+ sample_name[:9]
|
||||
|
||||
output = main(
|
||||
api_key,
|
||||
ELABFTW_API_URL,
|
||||
elabid,
|
||||
)
|
||||
|
||||
result = make_nexus_schema_dictionary(substrate_object, layers)
|
||||
result = output["result"]
|
||||
fn_base = output["fn_base"]
|
||||
|
||||
with open(f"output/{fn_base}.json", "w") as f:
|
||||
json.dump(result, f, indent=3)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user