Skip to content

Commit

Permalink
added support for serializing timedelta and relativedelta - revert wh…
Browse files Browse the repository at this point in the history
…itespace changes
  • Loading branch information
gabi committed May 23, 2021
1 parent a826b18 commit 5c741cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
17 changes: 9 additions & 8 deletions pyhocon/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pyhocon.config_tree import ConfigValues
from pyhocon.config_tree import NoneValue


try:
basestring
except NameError:
Expand Down Expand Up @@ -278,14 +279,14 @@ def convert_from_file(cls, input_file=None, output_file=None, output_format='jso
def __escape_match(cls, match):
char = match.group(0)
return {
'\b': r'\b',
'\t': r'\t',
'\n': r'\n',
'\f': r'\f',
'\r': r'\r',
'"': r'\"',
'\\': r'\\',
}.get(char) or (r'\u%04x' % ord(char))
'\b': r'\b',
'\t': r'\t',
'\n': r'\n',
'\f': r'\f',
'\r': r'\r',
'"': r'\"',
'\\': r'\\',
}.get(char) or (r'\u%04x' % ord(char))

@classmethod
def __escape_string(cls, string):
Expand Down
30 changes: 14 additions & 16 deletions tests/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,28 @@ class TestHOCONConverter(object):

def test_to_json(self):
converted = HOCONConverter.to_json(TestHOCONConverter.CONFIG)
assert [line.strip() for line in TestHOCONConverter.EXPECTED_JSON.split('\n') if line.strip()] \
== [line.strip() for line in converted.split('\n') if line.strip()]
assert [line.strip() for line in TestHOCONConverter.EXPECTED_JSON.split('\n') if line.strip()]\
== [line.strip() for line in converted.split('\n') if line.strip()]

def test_to_yaml(self):
converted = HOCONConverter.to_yaml(TestHOCONConverter.CONFIG)
assert [line.strip() for line in TestHOCONConverter.EXPECTED_YAML.split('\n') if line.strip()] \
== [line.strip() for line in converted.split('\n') if line.strip()]
assert [line.strip() for line in TestHOCONConverter.EXPECTED_YAML.split('\n') if line.strip()]\
== [line.strip() for line in converted.split('\n') if line.strip()]

def test_to_properties(self):
converted = HOCONConverter.to_properties(TestHOCONConverter.CONFIG)
assert [line.strip() for line in TestHOCONConverter.EXPECTED_PROPERTIES.split('\n') if line.strip()] \
== [line.strip() for line in converted.split('\n') if line.strip()]
assert [line.strip() for line in TestHOCONConverter.EXPECTED_PROPERTIES.split('\n') if line.strip()]\
== [line.strip() for line in converted.split('\n') if line.strip()]

def test_to_hocon(self):
converted = HOCONConverter.to_hocon(TestHOCONConverter.CONFIG)
assert [line.strip() for line in TestHOCONConverter.EXPECTED_HOCON.split('\n') if line.strip()] \
== [line.strip() for line in converted.split('\n') if line.strip()]
assert [line.strip() for line in TestHOCONConverter.EXPECTED_HOCON.split('\n') if line.strip()]\
== [line.strip() for line in converted.split('\n') if line.strip()]

def test_to_compact_hocon(self):
converted = HOCONConverter.to_hocon(TestHOCONConverter.CONFIG, compact=True)
assert [line.strip() for line in TestHOCONConverter.EXPECTED_COMPACT_HOCON.split('\n') if line.strip()] \
== [line.strip() for line in converted.split('\n') if line.strip()]
assert [line.strip() for line in TestHOCONConverter.EXPECTED_COMPACT_HOCON.split('\n') if line.strip()]\
== [line.strip() for line in converted.split('\n') if line.strip()]

def _test_convert_from_file(self, input, expected_output, format):
with tempfile.NamedTemporaryFile('w') as fdin:
Expand All @@ -158,20 +158,18 @@ def _test_convert_from_file(self, input, expected_output, format):
HOCONConverter.convert_from_file(fdin.name, fdout.name, format)
with open(fdout.name) as fdi:
converted = fdi.read()
assert [line.strip() for line in expected_output.split('\n') if line.strip()] \
== [line.strip() for line in converted.split('\n') if line.strip()]
assert [line.strip() for line in expected_output.split('\n') if line.strip()]\
== [line.strip() for line in converted.split('\n') if line.strip()]

def test_convert_from_file(self):
self._test_convert_from_file(TestHOCONConverter.CONFIG_STRING, TestHOCONConverter.EXPECTED_JSON, 'json')
self._test_convert_from_file(TestHOCONConverter.CONFIG_STRING, TestHOCONConverter.EXPECTED_YAML, 'yaml')
self._test_convert_from_file(TestHOCONConverter.CONFIG_STRING, TestHOCONConverter.EXPECTED_PROPERTIES,
'properties')
self._test_convert_from_file(TestHOCONConverter.CONFIG_STRING, TestHOCONConverter.EXPECTED_PROPERTIES, 'properties')
self._test_convert_from_file(TestHOCONConverter.CONFIG_STRING, TestHOCONConverter.EXPECTED_HOCON, 'hocon')

def test_invalid_format(self):
with pytest.raises(Exception):
self._test_convert_from_file(TestHOCONConverter.CONFIG_STRING, TestHOCONConverter.EXPECTED_PROPERTIES,
'invalid')
self._test_convert_from_file(TestHOCONConverter.CONFIG_STRING, TestHOCONConverter.EXPECTED_PROPERTIES, 'invalid')


def test_substitutions_conversions():
Expand Down

0 comments on commit 5c741cc

Please sign in to comment.