Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_validate_test_schemes():
# Supported
assert validate('path.csv')
assert validate('file://path.csv')
assert validate('http://example.com/path.csv')
assert validate('https://example.com/path.csv')
assert validate('ftp://example.com/path.csv')
assert validate('ftps://example.com/path.csv')
assert validate('path.csv', scheme='file')
# Not supported
with pytest.raises(exceptions.SchemeError) as excinfo:
validate('ssh://example.com/path.csv')
with pytest.raises(exceptions.SchemeError) as excinfo:
validate('bad://example.com/path.csv')
def test_validate_test_schemes():
# Supported
assert validate('path.csv')
assert validate('file://path.csv')
assert validate('http://example.com/path.csv')
assert validate('https://example.com/path.csv')
assert validate('ftp://example.com/path.csv')
assert validate('ftps://example.com/path.csv')
assert validate('path.csv', scheme='file')
# Not supported
with pytest.raises(exceptions.SchemeError) as excinfo:
validate('ssh://example.com/path.csv')
with pytest.raises(exceptions.SchemeError) as excinfo:
validate('bad://example.com/path.csv')
def test_stream_scheme_error():
stream = Stream('', scheme='bad_scheme')
with pytest.raises(exceptions.SchemeError) as excinfo:
stream.open()
assert 'bad_scheme' in str(excinfo.value)
# Get compression
for type in config.SUPPORTED_COMPRESSION:
if self.__compression == type or detected_format == type:
compression = type
else:
scheme = self.__scheme
format = self.__format
# Initiate loader
self.__loader = None
if scheme is not None:
loader_class = self.__custom_loaders.get(scheme)
if loader_class is None:
if scheme not in config.LOADERS:
message = 'Scheme "%s" is not supported' % scheme
raise exceptions.SchemeError(message)
loader_path = config.LOADERS[scheme]
if loader_path:
loader_class = helpers.import_attribute(loader_path)
if loader_class is not None:
loader_options = helpers.extract_options(options, loader_class.options)
if compression and 'http_stream' in loader_class.options:
loader_options['http_stream'] = False
self.__loader = loader_class(
bytes_sample_size=self.__bytes_sample_size,
**loader_options)
# Zip compression
if compression == 'zip' and six.PY3:
source = self.__loader.load(source, mode='b')
with zipfile.ZipFile(source) as archive:
name = archive.namelist()[0]
FormatError: The file format is not supported.
# Returns
bool: Whether tabulator is able to load the source file.
"""
# Get scheme and format
detected_scheme, detected_format = helpers.detect_scheme_and_format(source)
scheme = scheme or detected_scheme
format = format or detected_format
# Validate scheme and format
if scheme is not None:
if scheme not in config.LOADERS:
raise exceptions.SchemeError('Scheme "%s" is not supported' % scheme)
if format not in config.PARSERS:
raise exceptions.FormatError('Format "%s" is not supported' % format)
return True
def _compose_error_from_exception(exception):
code = 'source-error'
message = str(exception)
fatal_error = True
if isinstance(exception, tabulator.exceptions.SourceError):
code = 'source-error'
elif isinstance(exception, tabulator.exceptions.SchemeError):
code = 'scheme-error'
elif isinstance(exception, tabulator.exceptions.FormatError):
code = 'format-error'
elif isinstance(exception, tabulator.exceptions.EncodingError):
code = 'encoding-error'
elif isinstance(exception, tabulator.exceptions.IOError):
code = 'io-error'
elif isinstance(exception, tabulator.exceptions.HTTPError):
code = 'http-error'
elif isinstance(exception, tableschema.exceptions.IntegrityError):
code = 'source-error'
fatal_error = False
return (Error(code, message=message), fatal_error)