Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve YAML config loading #740

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion platforms/conda.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -e

INSTALL=$1

if [ "x${CONDA_PREFIX}" = "x" ]; then
echo "You must activate a conda environment before using this script."
exit 1
Expand Down Expand Up @@ -53,4 +55,8 @@ export TOAST_BUILD_SUITESPARSE_LIBRARY_DIR_HINTS="${LIBDIR}"
# Ensure that stale build products are removed
rm -rf "${topdir}/build"

pip install -vvv .
if [ -z "${INSTALL}" ]; then
pip install -vvv .
else
pip install -vvv --prefix "${INSTALL}" .
fi
25 changes: 18 additions & 7 deletions src/toast/config/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import argparse
import ast
import copy
import os
import re
import sys
import types
from collections import OrderedDict
from collections.abc import MutableMapping
from json import JSONDecodeError

import numpy as np
from astropy import units as u
Expand Down Expand Up @@ -60,14 +62,23 @@ def load_config(file, input=None, comm=None):

"""
ret = None
try:
ret = load_toml(file, input=input, comm=comm)
except TOMLKitError:
# If the file has a recognized extension, just use that instead of guessing.
ext = os.path.splitext(file)[1]
if ext in [".yml", ".yaml"]:
return load_yaml(file, input=input, comm=comm)
elif ext in [".json", ".jsn"]:
return load_json(file, input=input, comm=comm)
elif ext in [".toml", ".tml"]:
return load_toml(file, input=input, comm=comm)
else:
try:
ret = load_json(file, input=input, comm=comm)
except ValueError:
ret = load_yaml(file, input=input, comm=comm)
return ret
ret = load_toml(file, input=input, comm=comm)
except TOMLKitError:
try:
ret = load_json(file, input=input, comm=comm)
except (ValueError, JSONDecodeError):
ret = load_yaml(file, input=input, comm=comm)
return ret


def dump_config(file, conf, format="toml", comm=None):
Expand Down