From 2770ac6114d9f319a2cbd39d3dc555cb330d3345 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Mon, 11 Nov 2019 14:38:48 +0100 Subject: [PATCH] Improved url, file, data autodetect in io_util.read_content. --- benedict/utils/io_util.py | 29 +++++++++++++++++++++++------ tests/test_io_dict.py | 32 ++++++++++++++++---------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/benedict/utils/io_util.py b/benedict/utils/io_util.py index fc6027c..73220c9 100644 --- a/benedict/utils/io_util.py +++ b/benedict/utils/io_util.py @@ -192,13 +192,22 @@ def encode_yaml(d, **kwargs): def read_content(s): # s -> filepath or url or data - if s.startswith('http://') or s.startswith('https://'): - content = read_url(s) - elif os.path.isfile(s): - content = read_file(s) + num_lines = len(s.splitlines()) + if num_lines > 1: + # data + return s + if any([s.startswith(protocol) for protocol in ['http://', 'https://']]): + # url + return read_url(s) + elif any([s.endswith(extension) for extension in _get_formats_extensions()]): + # filepath + if os.path.isfile(s): + return read_file(s) + else: + return None else: - content = s - return content + # data + return s def read_file(filepath): @@ -278,6 +287,9 @@ _formats = { }, } +_formats_extensions = [ + '.{}'.format(extension) for extension in _formats.keys()] + def _get_format(format): return _formats.get( @@ -290,3 +302,8 @@ def _get_format_decoder(format): def _get_format_encoder(format): return _get_format(format).get('encoder', None) + + +def _get_formats_extensions(): + return _formats_extensions + diff --git a/tests/test_io_dict.py b/tests/test_io_dict.py index e9312c6..d08a450 100644 --- a/tests/test_io_dict.py +++ b/tests/test_io_dict.py @@ -224,23 +224,23 @@ class io_dict_test_case(unittest.TestCase): # with self.assertRaises(ValueError): # IODict(filepath, format='csv') - # def test_from_csv_with_invalid_file(self): - # filepath = self.input_path('invalid-file.csv') - # # static method - # with self.assertRaises(ValueError): - # IODict.from_csv(filepath) - # # constructor - # with self.assertRaises(ValueError): - # IODict(filepath, format='csv') + def test_from_csv_with_invalid_file(self): + filepath = self.input_path('invalid-file.csv') + # static method + with self.assertRaises(ValueError): + IODict.from_csv(filepath) + # constructor + with self.assertRaises(ValueError): + IODict(filepath, format='csv') - # def test_from_csv_with_valid_url_valid_content(self): - # url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.csv' - # # static method - # d = IODict.from_csv(url) - # self.assertTrue(isinstance(d, dict)) - # # constructor - # d = IODict(url, format='csv') - # self.assertTrue(isinstance(d, dict)) + def test_from_csv_with_valid_url_valid_content(self): + url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.csv' + # static method + d = IODict.from_csv(url) + self.assertTrue(isinstance(d, dict)) + # constructor + d = IODict(url, format='csv') + self.assertTrue(isinstance(d, dict)) # def test_from_csv_with_valid_url_invalid_content(self): # url = 'https://github.com/fabiocaccamo/python-benedict'