diff --git a/src/xfel_calibrate/calibrate.py b/src/xfel_calibrate/calibrate.py index 3f6a1101738074dddb2a70f31d3927c5a41c8449..d0e522bf6cf153c305408ab8f436ec153abe60b5 100755 --- a/src/xfel_calibrate/calibrate.py +++ b/src/xfel_calibrate/calibrate.py @@ -1,7 +1,6 @@ #!/usr/bin/env python import argparse -import ast import inspect import locale import math @@ -9,6 +8,7 @@ import os import pprint import re import stat +import string import sys import textwrap import warnings @@ -399,25 +399,20 @@ def make_extended_parser() -> argparse.ArgumentParser: # notebook TODO: This is a very hacky workaround, better implementation # is not really possible with the current state of this module user_notebook_path = nb_info["user"]["notebook"] - # Pulls out the variables in the templated path string, so that they - # can be added to the argument parser + # Pull out the variables in the templated path string, and get values + # from command line args (e.g. --proposal 1234 -> {proposal}) user_notebook_variables = [ - k.value.id - for k - in ast.walk(ast.parse(f"f'{user_notebook_path}'")) - if isinstance(k, ast.FormattedValue) + name for (_, name, _, _) in string.Formatter().parse(user_notebook_path) + if name is not None ] - user_notebook_parser = argparse.ArgumentParser() - + user_notebook_parser = argparse.ArgumentParser(add_help=False) for var in user_notebook_variables: user_notebook_parser.add_argument(f"--{var}") - user_notebook_args, _ = user_notebook_parser.parse_known_args( - args=list(filter(lambda x: x != "-h", _)) # Drop help from args - ) + user_notebook_args, _ = user_notebook_parser.parse_known_args() - nb_info["notebook"] = nb_info["user"]["notebook"].format(**vars(user_notebook_args)) + nb_info["notebook"] = user_notebook_path.format(**vars(user_notebook_args)) notebook = nb_info["notebook"] cvar = nb_info.get("concurrency", {}).get("parameter", None)