Skip to content

Commit

Permalink
Merge pull request airspeed-velocity#1420 from lucascolley/jsonc
Browse files Browse the repository at this point in the history
Allow `asv.conf.jsonc` file by default
  • Loading branch information
HaoZeke authored Aug 12, 2024
2 parents 484fbcd + 8dd9153 commit 7c04d4e
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions asv/config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import os
import sys
from pathlib import Path

from .console import log
from . import util

# TODO: Some verification of the config values


def _get_config_path():
"""
Check the default config file path for all valid extensions.
Raise if no file is found, or if multiple are found for the valid extensions.
Return the path of the config file if exactly one is found.
"""
extensions = ['.json', '.jsonc']
path = Path('asv.conf')

num_matches = 0
for e in extensions:
p = path.with_suffix(e)
if p.exists():
num_matches += 1
path = p

if num_matches == 0:
raise util.UserError(
f"No `asv.conf` file found for valid extensions: {extensions}."
)
elif num_matches > 1:
raise util.UserError(
f"Multiple `asv.conf` files found for valid extensions: {extensions}. "
f"Please specify which file to use with `--config=FILEPATH`."
)
else:
# exactly one valid config file found
return str(path)


class Config:
"""
Manages the configuration for a benchmark project.
Expand Down Expand Up @@ -48,13 +78,13 @@ def __init__(self):
def load(cls, path=None):
"""
Load a configuration from a file. If no file is provided,
defaults to `asv.conf.json`.
defaults to `asv.conf` with valid extensions `['.json', '.jsonc']`.
"""
if not path:
path = "asv.conf.json"

if not os.path.isfile(path):
raise util.UserError(f"Config file {path} not found.")
if path:
if not Path(path).exists():
raise util.UserError(f"Config file {path} not found.")
else:
path = _get_config_path()

d = util.load_json(path, cls.api_version, js_comments=True)
try:
Expand Down

0 comments on commit 7c04d4e

Please sign in to comment.