Improved url, file, data autodetect in io_util.read_content.
This commit is contained in:
parent
e57527b55e
commit
2770ac6114
|
@ -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:
|
||||
content = s
|
||||
return content
|
||||
return None
|
||||
else:
|
||||
# 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
|
||||
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in New Issue