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

build: Add support for JSON configuration files #17

Closed
wants to merge 1 commit into from
Closed
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
54 changes: 43 additions & 11 deletions tcbuilder/backend/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Backend handling for build subcommand
"""

import json
import os
import copy
import logging
Expand Down Expand Up @@ -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)
Copy link
Contributor

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 that JSONDecodeError has lineno (and also colno) attributes: https://docs.python.org/3/library/json.html#json.JSONDecodeError

raise error_exc


def parse_config_file(config_path, schema_path=DEFAULT_SCHEMA_FILE, substs=None):
"""Parse a configuration file against the expected schema
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 config being undefined.


# Make variable substitutions.
if substs is not None:
Expand Down