-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactorchange yamlencode/yamldecode to yaml_encode/yaml_decode and a…
…dd same hooks to yaml, toml, and ini providers
- Loading branch information
Showing
16 changed files
with
152 additions
and
107 deletions.
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
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
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
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
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 |
---|---|---|
@@ -1,6 +1,19 @@ | ||
from tackle import tackle | ||
from tackle.utils.hooks import get_hook | ||
|
||
|
||
def test_provider_json(): | ||
output = tackle('json.yaml') | ||
assert output | ||
|
||
|
||
def test_provider_json_encode(): | ||
Hook = get_hook('json_encode') | ||
output = Hook(data={"Section1": {"keyA": "valueA", "keyB": "valueB"}}).exec() | ||
assert output == '{"Section1": {"keyA": "valueA", "keyB": "valueB"}}' | ||
|
||
|
||
def test_provider_json_decode(): | ||
Hook = get_hook('json_decode') | ||
output = Hook(data='{"Section1": {"keyA": "valueA", "keyB": "valueB"}}').exec() | ||
assert output == {"Section1": {"keyA": "valueA", "keyB": "valueB"}} |
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
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 |
---|---|---|
@@ -1,38 +1,35 @@ | ||
try: | ||
import tomli as toml | ||
except ModuleNotFoundError: | ||
import tomllib as toml | ||
|
||
import os | ||
from typing import MutableMapping, Union | ||
from typing import Union | ||
|
||
import tomli as toml | ||
|
||
from tackle import BaseHook, Field | ||
|
||
|
||
class TomlHook(BaseHook): | ||
"""Hook for reading toml. Does not support write which needs another provider""" | ||
""" | ||
Hook for reading TOML. Wraps python's native toml library which does not support | ||
writing toml, only reading. | ||
""" | ||
|
||
hook_name: str = 'toml' | ||
path: str = Field(..., description="The file path to put read or write to.") | ||
data: Union[dict, list, str] = Field( | ||
None, | ||
description="Map/list or renderable string to a map/list key to write.", | ||
render_by_default=True, | ||
) | ||
args: list = ['path', 'data'] | ||
|
||
def exec(self) -> Union[MutableMapping, str]: | ||
path: str = Field(..., description="The file path to read or write to.") | ||
|
||
args: list = ['path'] | ||
|
||
def exec(self) -> Union[dict, str]: | ||
self.path = os.path.abspath(os.path.expanduser(os.path.expandvars(self.path))) | ||
# Make path if it does not exist | ||
# if not os.path.exists(os.path.dirname(self.path)) and self.data: | ||
# os.makedirs(os.path.dirname(self.path)) | ||
|
||
# if self.data: | ||
# with open(self.path, 'w') as f: | ||
# toml.dump(self.data, f) | ||
# return self.path | ||
# | ||
# else: | ||
with open(self.path, 'rb') as f: | ||
data = toml.load(f) | ||
return data | ||
|
||
|
||
class TomlDecodeHook(BaseHook): | ||
"""Hook for decoding a TOML string to a dict.""" | ||
|
||
hook_name: str = 'toml_decode' | ||
data: str = Field(..., description="TOML string to convert to dict.") | ||
args: list = ['data'] | ||
|
||
def exec(self) -> dict: | ||
return toml.loads(self.data) |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,19 +1,13 @@ | ||
from tackle.main import tackle | ||
from tackle.utils.hooks import get_hook | ||
|
||
|
||
def test_provider_toml_hook_read(): | ||
o = tackle('read.yaml', no_input=True) | ||
assert 'owner' in o['read'].keys() | ||
|
||
|
||
# @pytest.fixture() | ||
# def clean_toml(): | ||
# """Remove outputs.""" | ||
# yield | ||
# if os.path.exists('writing.toml'): | ||
# os.remove('writing.toml') | ||
# | ||
# | ||
# def test_provider_toml_hook_write(clean_toml): | ||
# tackle('write.yaml', no_input=True) | ||
# assert os.path.exists('writing.toml') | ||
def test_provider_toml_decode(): | ||
Hook = get_hook('toml_decode') | ||
output = Hook(data='[Section1]\nkeyA = "valueA"\nkeyB = "valueB"\n').exec() | ||
assert output == {"Section1": {"keyA": "valueA", "keyB": "valueB"}} |
This file was deleted.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
in->: file read.yaml | ||
|
||
#out_filter->: "{{in|yamldecode}}" | ||
out_function->: "{{yaml_decode(in)['stuff']}}" | ||
|
||
out->: yaml_decode {{in}} |
2 changes: 1 addition & 1 deletion
2
providers/yaml/tests/yamlencode.yaml → providers/yaml/tests/yaml_encode.yaml
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
in->: yaml read.yaml | ||
|
||
#out->: "{{in|yamlencode}}" | ||
out->: "{{yamlencode(in)}}" | ||
out->: "{{yaml_encode(in)}}" |
This file was deleted.
Oops, something went wrong.