Skip to content

Commit 2cc3b04

Browse files
authored
Merge pull request #480 from janosh/better-cli-errors-on-missing-config-files
Better CLI errors on missing config files
2 parents 635fca9 + b3d4daf commit 2cc3b04

18 files changed

+229
-128
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pip-log.txt
3030
# Unit test / coverage reports
3131
.coverage
3232
.tox
33-
nosetests.xml
3433

3534
# Translations
3635
*.mo

.pre-commit-config.yaml

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
exclude: ^docs
2+
13
repos:
24
- repo: https://github.com/asottile/pyupgrade
3-
rev: v2.29.0
5+
rev: v2.31.0
46
hooks:
57
- id: pyupgrade
68
args: [--py37-plus]
@@ -12,18 +14,18 @@ repos:
1214
args: [--in-place, --remove-all-unused-imports, --remove-unused-variable, --ignore-init-module-imports]
1315

1416
- repo: https://github.com/psf/black
15-
rev: 21.4b2
17+
rev: 22.1.0
1618
hooks:
1719
- id: black
1820
args: [--line-length, '120']
1921

2022
- repo: https://github.com/PyCQA/isort
21-
rev: 5.9.3
23+
rev: 5.10.1
2224
hooks:
2325
- id: isort
2426

2527
- repo: https://github.com/pre-commit/pre-commit-hooks
26-
rev: v4.0.1
28+
rev: v4.1.0
2729
hooks:
2830
- id: check-case-conflict
2931
- id: check-symlinks

fireworks/core/launchpad.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def __init__(
182182
uri_mode (bool): if set True, all Mongo connection parameters occur through a MongoDB URI string (set as
183183
the host).
184184
mongoclient_kwargs (dict): A list of any other custom keyword arguments to be
185-
passed into the MongoClient connection (non-URI mode only). Use these kwargs to specify SSL/TLS
185+
passed into the MongoClient connection. Use these kwargs to specify SSL/TLS or serverSelectionTimeoutMS
186186
arguments. Note these arguments are different depending on the major pymongo version used; see
187187
pymongo documentation for more details.
188188
"""
@@ -206,7 +206,7 @@ def __init__(
206206

207207
# get connection
208208
if uri_mode:
209-
self.connection = MongoClient(host)
209+
self.connection = MongoClient(host, **self.mongoclient_kwargs)
210210
dbname = host.split("/")[-1].split("?")[0] # parse URI to extract dbname
211211
self.db = self.connection[dbname]
212212
else:

fireworks/fw_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
NEGATIVE_FWID_CTR = 0
1818

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

2222
# if you update a _fw_name, you can use this to record the change and maintain deserialization
2323
FW_NAME_UPDATES = {

fireworks/scripts/_helpers.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
from argparse import Namespace
3+
from typing import Sequence, Tuple, Union
4+
5+
cfg_file_vldtor = Tuple[str, str, bool, Union[str, None]]
6+
7+
8+
def _validate_config_file_paths(args: Namespace, cfg_files_to_validate: Sequence[cfg_file_vldtor]) -> None:
9+
"""Validate the CLI config files.
10+
11+
Args:
12+
args (argparse.Namespace): The parsed arguments from the CLI.
13+
cfg_files_to_validate (list[tuple[str, str, bool, str | None]]): config files to validate.
14+
Tuple is (config filename, CLI flag, is filepath required, default config file location).
15+
16+
Raises:
17+
ValueError: If a path to a required config file is not provided.
18+
FileNotFoundError: If a config file is provided but does not exist.
19+
"""
20+
for filename, cli_flag, required, default_loc in cfg_files_to_validate:
21+
22+
attr_name = f"{filename}_file"
23+
file_path = getattr(args, attr_name)
24+
25+
# args.config_dir defaults to '.' if not specified
26+
file_in_config_dir = os.path.join(args.config_dir, f"my_{filename}.yaml")
27+
if file_path is None and os.path.exists(file_in_config_dir):
28+
setattr(args, attr_name, file_in_config_dir)
29+
elif file_path is None:
30+
setattr(args, attr_name, default_loc)
31+
32+
file_path = getattr(args, attr_name, None)
33+
34+
# throw on missing config files
35+
if file_path is None and required:
36+
raise ValueError(
37+
f"No path specified for {attr_name}. Use the {cli_flag} flag to specify or check the value "
38+
f"of CONFIG_FILE_DIR and make sure it points at where all your config files are."
39+
)
40+
if file_path is not None and not os.path.exists(file_path):
41+
raise FileNotFoundError(f"{attr_name} '{file_path}' does not exist!")

0 commit comments

Comments
 (0)