Fixed format auto-detection with unexpected extensions.
This commit is contained in:
parent
991487590f
commit
6cd075edce
|
@ -20,7 +20,8 @@ class IODict(dict):
|
|||
|
||||
@staticmethod
|
||||
def _decode_init(s, **kwargs):
|
||||
default_format = io_util.autodetect_format(s, default='json')
|
||||
autodetected_format = io_util.autodetect_format(s)
|
||||
default_format = autodetected_format or 'json'
|
||||
format = kwargs.pop('format', default_format).lower()
|
||||
if format in ['b64', 'base64']:
|
||||
kwargs.setdefault('subformat', 'json')
|
||||
|
|
|
@ -8,12 +8,10 @@ import os
|
|||
import requests
|
||||
|
||||
|
||||
def autodetect_format(s, default=None):
|
||||
if is_data(s):
|
||||
return default
|
||||
elif is_url(s) or is_filepath(s):
|
||||
def autodetect_format(s):
|
||||
if is_url(s) or is_filepath(s):
|
||||
return get_format_by_path(s)
|
||||
return default
|
||||
return None
|
||||
|
||||
|
||||
def decode(s, format, **kwargs):
|
||||
|
@ -38,8 +36,9 @@ def is_data(s):
|
|||
|
||||
|
||||
def is_filepath(s):
|
||||
return any([s.endswith(extension)
|
||||
for extension in get_serializers_extensions()])
|
||||
if any([s.endswith(ext) for ext in get_serializers_extensions()]):
|
||||
return True
|
||||
return os.path.isfile(s)
|
||||
|
||||
|
||||
def is_url(s):
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
{"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}
|
|
@ -82,6 +82,18 @@ class io_dict_json_test_case(io_dict_test_case):
|
|||
d = IODict(filepath)
|
||||
self.assertTrue(isinstance(d, dict))
|
||||
|
||||
def test_from_json_with_valid_file_valid_content_but_unexpected_extension(self):
|
||||
filepath = self.input_path('valid-content.json.txt')
|
||||
# static method
|
||||
d = IODict.from_json(filepath)
|
||||
self.assertTrue(isinstance(d, dict))
|
||||
# constructor
|
||||
d = IODict(filepath, format='json')
|
||||
self.assertTrue(isinstance(d, dict))
|
||||
# constructor with format autodetection
|
||||
d = IODict(filepath)
|
||||
self.assertTrue(isinstance(d, dict))
|
||||
|
||||
def test_from_json_with_valid_file_valid_content_invalid_format(self):
|
||||
filepath = self.input_path('valid-content.base64')
|
||||
with self.assertRaises(ValueError):
|
||||
|
|
|
@ -15,10 +15,6 @@ class io_util_test_case(unittest.TestCase):
|
|||
s = '{"a": 1, "b": 2, "c": 3}'
|
||||
self.assertEqual(io_util.autodetect_format(s), None)
|
||||
|
||||
def test_autodetect_format_by_data_with_default(self):
|
||||
s = '{"a": 1, "b": 2, "c": 3}'
|
||||
self.assertEqual(io_util.autodetect_format(s, default='json'), 'json')
|
||||
|
||||
def test_autodetect_format_by_path(self):
|
||||
s = 'path-to/data.xml'
|
||||
self.assertEqual(io_util.autodetect_format(s), 'xml')
|
||||
|
|
Loading…
Reference in New Issue