Skip to content

Commit

Permalink
feat: add toml support
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Jul 11, 2022
1 parent 25ab052 commit d4698df
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
17 changes: 17 additions & 0 deletions tackle/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,20 @@ class MalformedFunctionFieldException(TackleFunctionCreateException):
Happens when a tackle file is parsed.
"""


class TackleGeneralException(Exception):
"""Base hook call exception class."""

def __init__(self, message: str):
sys.tracebacklimit = 0
super().__init__(message)


class TackleImportError(TackleGeneralException):
"""
Exception when functions with field inputs of type dict are not formatted
appropriately.
Happens when a tackle file is parsed.
"""
22 changes: 19 additions & 3 deletions tackle/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import os
import logging

from tackle.exceptions import ContextDecodingException, UnsupportedBaseFileTypeException
from tackle.exceptions import (
ContextDecodingException,
UnsupportedBaseFileTypeException,
TackleImportError,
)
from tackle.utils.paths import make_sure_path_exists

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -66,6 +70,18 @@ def read_config_file(file, file_extension=None):
for doc in yaml.load_all(f.read()):
output.append(doc)
return output
elif file_extension == 'toml':
try:
import toml
except ImportError:
raise TackleImportError(
f"Error parsing {file} No toml package installed. Install it with "
"`pip install toml` and try again."
) from None
with open(file) as f:
data = toml.load(f)
return data

else:
raise UnsupportedBaseFileTypeException(
'Unable to parse file {}. Error: Unsupported extension (json/yaml only)'
Expand All @@ -75,11 +91,11 @@ def read_config_file(file, file_extension=None):
except ValueError as e:
# JSON decoding error. Let's throw a new exception that is more
# friendly for the developer or user.
our_exc_message = (
message = (
f'JSON decoding error while loading "{file}". Decoding'
f' error details: "{str(e)}"'
)
raise ContextDecodingException(our_exc_message)
raise ContextDecodingException(message) from None


def apply_overwrites_to_inputs(input, overwrite_dict):
Expand Down
2 changes: 1 addition & 1 deletion tackle/utils/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def is_directory_with_tackle(value) -> bool:
def is_file(value) -> bool:
"""Return True if the input looks like a file."""
FILE_REGEX = re.compile(
r"""^.*\.(yaml|yml|json)$""",
r"""^.*\.(yaml|yml|json|toml)$""",
re.VERBOSE,
)
return bool(FILE_REGEX.match(value))
Expand Down
5 changes: 5 additions & 0 deletions tests/parser/fixtures/toml.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

baz = "bar"

[foo]
"bar->" = "{{baz}}"
5 changes: 5 additions & 0 deletions tests/parser/fixtures/toml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

baz: "bar"

foo:
bar: bar
1 change: 1 addition & 0 deletions tests/parser/test_parser_parse_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
('docker-compose.yml', 'docker-compose.yml'),
('list-list.yaml', 'list-list.yaml'),
('var-hook.yaml', 'var-hook-output.yaml'),
('toml.toml', 'toml.yaml'),
# # Broken
# # TODO: https://github.com/robcxyz/tackle-box/issues/52
# ('bug-mixed-flags.yaml', 'bug-mixed-flags.yaml'),
Expand Down

0 comments on commit d4698df

Please sign in to comment.