From e30035350b14942573f0d81ec502e7700365f514 Mon Sep 17 00:00:00 2001 From: Ivan Usov Date: Thu, 28 Apr 2022 16:37:54 +0200 Subject: [PATCH] Generalize search for proposals Avoid hardcoding specific years in proposal paths --- pyzebra/app/app.py | 16 ++++++++++------ pyzebra/utils.py | 17 +++++++---------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/pyzebra/app/app.py b/pyzebra/app/app.py index 7bd798c..a3d6151 100644 --- a/pyzebra/app/app.py +++ b/pyzebra/app/app.py @@ -36,14 +36,18 @@ proposal_textinput.on_change("value_input", proposal_textinput_callback) doc.proposal_textinput = proposal_textinput def apply_button_callback(): - try: - proposal_path = pyzebra.find_proposal_path(proposal_textinput.value) - except ValueError as e: - print(e) - return + proposal = proposal_textinput.value.strip() + if proposal: + try: + proposal_path = pyzebra.find_proposal_path(proposal) + except ValueError as e: + print(e) + return + apply_button.disabled = True + else: + proposal_path = "" proposal_textinput.name = proposal_path - apply_button.disabled = True apply_button = Button(label="Apply", button_type="primary") apply_button.on_click(apply_button_callback) diff --git a/pyzebra/utils.py b/pyzebra/utils.py index 0deb425..0f3408c 100644 --- a/pyzebra/utils.py +++ b/pyzebra/utils.py @@ -1,20 +1,17 @@ import os -ZEBRA_PROPOSALS_PATHS = [ - f"/afs/psi.ch/project/sinqdata/{year}/zebra/" for year in (2016, 2017, 2018, 2020, 2021, 2022) -] +SINQ_PATH = "/afs/psi.ch/project/sinqdata" +ZEBRA_PROPOSALS_PATH = os.path.join(SINQ_PATH, "{year}/zebra/{proposal}") + def find_proposal_path(proposal): - proposal = proposal.strip() - if proposal: - for zebra_proposals_path in ZEBRA_PROPOSALS_PATHS: - proposal_path = os.path.join(zebra_proposals_path, proposal) + for entry in os.scandir(SINQ_PATH): + if entry.is_dir() and len(entry.name) == 4 and entry.name.isdigit(): + proposal_path = ZEBRA_PROPOSALS_PATH.format(year=entry.name, proposal=proposal) if os.path.isdir(proposal_path): # found it break - else: - raise ValueError(f"Can not find data for proposal '{proposal}'.") else: - proposal_path = "" + raise ValueError(f"Can not find data for proposal '{proposal}'.") return proposal_path