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_stream_save_xls_not_supported(tmpdir):
source = 'data/table.csv'
target = str(tmpdir.join('table.xls'))
with Stream(source, headers=1) as stream:
with pytest.raises(exceptions.FormatError) as excinfo:
stream.save(target)
assert 'xls' in str(excinfo.value)
def test_stream_html_content():
# Link to html file containing information about csv file
source = 'https://github.com/frictionlessdata/tabulator-py/blob/master/data/table.csv'
with pytest.raises(exceptions.FormatError) as excinfo:
Stream(source).open()
assert 'HTML' in str(excinfo.value)
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)
# 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
# Not supported compression
elif compression:
message = 'Compression "%s" is not supported for your Python version'
raise exceptions.TabulatorException(message % compression)
# Attach stats to the loader
if getattr(self.__loader, 'attach_stats', None):
self.__stats = {'size': 0, 'hash': ''}
getattr(self.__loader, 'attach_stats')(self.__stats)
# Initiate parser
parser_class = self.__custom_parsers.get(format)
if parser_class is None:
if format not in config.PARSERS:
message = 'Format "%s" is not supported' % format
raise exceptions.FormatError(message)
parser_class = helpers.import_attribute(config.PARSERS[format])
parser_options = helpers.extract_options(options, parser_class.options)
self.__parser = parser_class(self.__loader,
force_parse=self.__force_parse,
**parser_options)
# Bad options
if options:
message = 'Not supported option(s) "%s" for scheme "%s" and format "%s"'
message = message % (', '.join(options), scheme, format)
warnings.warn(message, UserWarning)
# Open and setup
self.__parser.open(source, encoding=self.__encoding)
self.__extract_sample()
self.__extract_headers()
def __detect_html(self):
# Prepare text
text = ''
for row_number, headers, row in self.__sample_extended_rows:
for value in row:
if isinstance(value, six.string_types):
text += value
# Detect html content
html_source = helpers.detect_html(text)
if html_source:
message = 'Format has been detected as HTML (not supported)'
raise exceptions.FormatError(message)
**options: Extra options passed to the writer.
"""
# Get encoding/format
if encoding is None:
encoding = config.DEFAULT_ENCODING
if format is None:
_, format = helpers.detect_scheme_and_format(target)
# Prepare writer class
writer_class = self.__custom_writers.get(format)
if writer_class is None:
if format not in config.WRITERS:
message = 'Format "%s" is not supported' % format
raise exceptions.FormatError(message)
writer_class = helpers.import_attribute(config.WRITERS[format])
# Prepare writer options
writer_options = helpers.extract_options(options, writer_class.options)
if options:
message = 'Not supported options "%s" for format "%s"'
message = message % (', '.join(options), format)
raise exceptions.TabulatorException(message)
# Write data to target
writer = writer_class(**writer_options)
writer.write(self.iter(), target, headers=self.headers, encoding=encoding)