-
Notifications
You must be signed in to change notification settings - Fork 14
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
build: Add support for JSON configuration files #17
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
Backend handling for build subcommand | ||
""" | ||
|
||
import json | ||
import os | ||
import copy | ||
import logging | ||
|
@@ -228,6 +229,44 @@ def make_download_fname(fname): | |
|
||
return out_fname, is_temp | ||
|
||
def _load_config_file_yaml(config_path): | ||
""" | ||
Load the YAML configuration file | ||
""" | ||
|
||
# Load the YAML configuration file (user-supplied): | ||
with open(config_path) as file: | ||
try: | ||
config = yaml.load(file, Loader=yaml.FullLoader) | ||
return config | ||
|
||
except yaml.YAMLError as ex: | ||
error_exc = ParseError(getattr(ex, "problem", "parsing error")) | ||
error_exc.set_source(file=config_path) | ||
if hasattr(ex, "problem_mark"): | ||
mark = getattr(ex, "problem_mark") | ||
error_exc.set_source(line=mark.line, column=mark.column) | ||
raise error_exc | ||
|
||
|
||
def _load_config_file_json(config_path): | ||
""" | ||
Load the JSON configuration file | ||
""" | ||
|
||
# Load the JSON configuration file (user-supplied): | ||
with open(config_path) as file: | ||
try: | ||
config = json.load(file) | ||
return config | ||
|
||
except json.JSONDecodeError as ex: | ||
error_exc = ParseError(getattr(ex, "msg", "parsing error")) | ||
error_exc.set_source(file=config_path) | ||
if hasattr(ex, "doc"): | ||
error_exc.set_source(line=ex.doc) | ||
raise error_exc | ||
|
||
|
||
def parse_config_file(config_path, schema_path=DEFAULT_SCHEMA_FILE, substs=None): | ||
"""Parse a configuration file against the expected schema | ||
|
@@ -242,18 +281,11 @@ def parse_config_file(config_path, schema_path=DEFAULT_SCHEMA_FILE, substs=None) | |
raise PathNotExistError( | ||
f"Build configuration file '{config_path}' does not exist.") | ||
|
||
# Load the YAML configuration file (user-supplied): | ||
with open(config_path) as file: | ||
try: | ||
config = yaml.load(file, Loader=yaml.FullLoader) | ||
|
||
except yaml.YAMLError as ex: | ||
error_exc = ParseError(getattr(ex, "problem", "parsing error")) | ||
error_exc.set_source(file=config_path) | ||
if hasattr(ex, "problem_mark"): | ||
mark = getattr(ex, "problem_mark") | ||
error_exc.set_source(line=mark.line, column=mark.column) | ||
raise error_exc | ||
if config_path.endswith(".json"): | ||
config = _load_config_file_json(config_path) | ||
elif config_path.endswith(".yaml") or config_path.endswith(".yml"): | ||
config = _load_config_file_yaml(config_path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably provide a clean error message (and abort) if no supported extension was found. Now I think it will raise an exception for |
||
|
||
# Make variable substitutions. | ||
if substs is not None: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this use
ex.doc
as the line number? Seems thatJSONDecodeError
haslineno
(and alsocolno
) attributes: https://docs.python.org/3/library/json.html#json.JSONDecodeError