Skip to content
Snippets Groups Projects
Commit be685a0a authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Merge branch 'refactor/user-nb-path' into 'master'

Simplify handling of user notebook paths

See merge request detectors/pycalibration!554
parents b687cd33 7a6b505b
No related branches found
No related tags found
1 merge request!554Simplify handling of user notebook paths
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
import ast
import inspect import inspect
import locale import locale
import math import math
...@@ -9,6 +8,7 @@ import os ...@@ -9,6 +8,7 @@ import os
import pprint import pprint
import re import re
import stat import stat
import string
import sys import sys
import textwrap import textwrap
import warnings import warnings
...@@ -399,25 +399,20 @@ def make_extended_parser() -> argparse.ArgumentParser: ...@@ -399,25 +399,20 @@ def make_extended_parser() -> argparse.ArgumentParser:
# notebook TODO: This is a very hacky workaround, better implementation # notebook TODO: This is a very hacky workaround, better implementation
# is not really possible with the current state of this module # is not really possible with the current state of this module
user_notebook_path = nb_info["user"]["notebook"] user_notebook_path = nb_info["user"]["notebook"]
# Pulls out the variables in the templated path string, so that they # Pull out the variables in the templated path string, and get values
# can be added to the argument parser # from command line args (e.g. --proposal 1234 -> {proposal})
user_notebook_variables = [ user_notebook_variables = [
k.value.id name for (_, name, _, _) in string.Formatter().parse(user_notebook_path)
for k if name is not None
in ast.walk(ast.parse(f"f'{user_notebook_path}'"))
if isinstance(k, ast.FormattedValue)
] ]
user_notebook_parser = argparse.ArgumentParser() user_notebook_parser = argparse.ArgumentParser(add_help=False)
for var in user_notebook_variables: for var in user_notebook_variables:
user_notebook_parser.add_argument(f"--{var}") user_notebook_parser.add_argument(f"--{var}")
user_notebook_args, _ = user_notebook_parser.parse_known_args( user_notebook_args, _ = user_notebook_parser.parse_known_args()
args=list(filter(lambda x: x != "-h", _)) # Drop help from 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"] notebook = nb_info["notebook"]
cvar = nb_info.get("concurrency", {}).get("parameter", None) cvar = nb_info.get("concurrency", {}).get("parameter", None)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment