Skip to content

Commit

Permalink
Merge pull request #480 from janosh/better-cli-errors-on-missing-conf…
Browse files Browse the repository at this point in the history
…ig-files

Better CLI errors on missing config files
  • Loading branch information
computron authored Mar 3, 2022
2 parents 635fca9 + b3d4daf commit 2cc3b04
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 128 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pip-log.txt
# Unit test / coverage reports
.coverage
.tox
nosetests.xml

# Translations
*.mo
Expand Down
10 changes: 6 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
exclude: ^docs

repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.29.0
rev: v2.31.0
hooks:
- id: pyupgrade
args: [--py37-plus]
Expand All @@ -12,18 +14,18 @@ repos:
args: [--in-place, --remove-all-unused-imports, --remove-unused-variable, --ignore-init-module-imports]

- repo: https://github.com/psf/black
rev: 21.4b2
rev: 22.1.0
hooks:
- id: black
args: [--line-length, '120']

- repo: https://github.com/PyCQA/isort
rev: 5.9.3
rev: 5.10.1
hooks:
- id: isort

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.1.0
hooks:
- id: check-case-conflict
- id: check-symlinks
Expand Down
4 changes: 2 additions & 2 deletions fireworks/core/launchpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __init__(
uri_mode (bool): if set True, all Mongo connection parameters occur through a MongoDB URI string (set as
the host).
mongoclient_kwargs (dict): A list of any other custom keyword arguments to be
passed into the MongoClient connection (non-URI mode only). Use these kwargs to specify SSL/TLS
passed into the MongoClient connection. Use these kwargs to specify SSL/TLS or serverSelectionTimeoutMS
arguments. Note these arguments are different depending on the major pymongo version used; see
pymongo documentation for more details.
"""
Expand All @@ -206,7 +206,7 @@ def __init__(

# get connection
if uri_mode:
self.connection = MongoClient(host)
self.connection = MongoClient(host, **self.mongoclient_kwargs)
dbname = host.split("/")[-1].split("?")[0] # parse URI to extract dbname
self.db = self.connection[dbname]
else:
Expand Down
2 changes: 1 addition & 1 deletion fireworks/fw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
NEGATIVE_FWID_CTR = 0

# this is where load_object() looks for serialized objects
USER_PACKAGES = ["fireworks.user_objects", "fireworks.utilities.tests", "fw_tutorials", "fireworks.features"]
USER_PACKAGES = ["fireworks.user_objects", "fw_tutorials", "fireworks.features"]

# if you update a _fw_name, you can use this to record the change and maintain deserialization
FW_NAME_UPDATES = {
Expand Down
41 changes: 41 additions & 0 deletions fireworks/scripts/_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
from argparse import Namespace
from typing import Sequence, Tuple, Union

cfg_file_vldtor = Tuple[str, str, bool, Union[str, None]]


def _validate_config_file_paths(args: Namespace, cfg_files_to_validate: Sequence[cfg_file_vldtor]) -> None:
"""Validate the CLI config files.
Args:
args (argparse.Namespace): The parsed arguments from the CLI.
cfg_files_to_validate (list[tuple[str, str, bool, str | None]]): config files to validate.
Tuple is (config filename, CLI flag, is filepath required, default config file location).
Raises:
ValueError: If a path to a required config file is not provided.
FileNotFoundError: If a config file is provided but does not exist.
"""
for filename, cli_flag, required, default_loc in cfg_files_to_validate:

attr_name = f"{filename}_file"
file_path = getattr(args, attr_name)

# args.config_dir defaults to '.' if not specified
file_in_config_dir = os.path.join(args.config_dir, f"my_{filename}.yaml")
if file_path is None and os.path.exists(file_in_config_dir):
setattr(args, attr_name, file_in_config_dir)
elif file_path is None:
setattr(args, attr_name, default_loc)

file_path = getattr(args, attr_name, None)

# throw on missing config files
if file_path is None and required:
raise ValueError(
f"No path specified for {attr_name}. Use the {cli_flag} flag to specify or check the value "
f"of CONFIG_FILE_DIR and make sure it points at where all your config files are."
)
if file_path is not None and not os.path.exists(file_path):
raise FileNotFoundError(f"{attr_name} '{file_path}' does not exist!")
Loading

0 comments on commit 2cc3b04

Please sign in to comment.