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

Support Jsonnet as a config format #19

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion pyrallis/options.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import contextlib
from typing import Union

from pyrallis.parsers.config_parsers import ParserEnum, YAMLParser, JSONParser, TOMLParser
from pyrallis.parsers.config_parsers import ParserEnum, YAMLParser, JSONParser, JSONNETParser, TOMLParser


class ConfigType(ParserEnum):
YAML = YAMLParser
JSON = JSONParser
JSONNET = JSONNETParser
TOML = TOMLParser


Expand Down
18 changes: 18 additions & 0 deletions pyrallis/parsers/config_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ def save_config(d, stream=None, **kwargs):
return json.dump(d, stream, **kwargs)


class JSONNETParser(JSONParser):

@staticmethod
def parse_string(s):
import json
import _jsonnet
try:
s = _jsonnet.evaluate_snippet("snippet", s) # "snippet" is a dummy filename parameter
return json.loads(s)
except json.decoder.JSONDecodeError:
return s # No parsing to be done, simply return value

@staticmethod
def load_config(stream):
s = stream.read() # Jsonnet doesn't provide stream API
return JSONNETParser.parse_string(s)


class TOMLParser(Parser):

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/test_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SomeClass:
assert pyrallis.decode(SomeClass, pyrallis.encode(b)) == b


@parametrize('config_type', ['', 'yaml', 'json', 'toml'])
@parametrize('config_type', ['', 'yaml', 'json', 'jsonnet', 'toml'])
def test_dump_load(simple_attribute, config_type, tmp_path):
some_type, _, expected_value = simple_attribute

Expand Down