Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Exposed version and Stream::test (#134) (#142))
Browse files Browse the repository at this point in the history
* exposed version to API and CLI

* added Stream::test
  • Loading branch information
roll authored Jan 13, 2017
1 parent 643f3ba commit 2278e39
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
12 changes: 12 additions & 0 deletions tabulator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import io
import os


# General

from .stream import Stream
from . import exceptions

# Deprecated

from .topen import topen
from .stream import Stream as Table

# Version

__version__ = io.open(
os.path.join(os.path.dirname(__file__), 'VERSION'),
encoding='utf-8').read().strip()
5 changes: 3 additions & 2 deletions tabulator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import six
import click
from .stream import Stream
import tabulator


# Module API
Expand All @@ -18,9 +18,10 @@
@click.option('--format')
@click.option('--encoding')
@click.option('--limit', type=click.INT)
@click.version_option(tabulator.__version__, message='%(version)s')
def cli(source, limit, **options):
options = {key: value for key, value in options.items() if value is not None}
with Stream(source, **options) as stream:
with tabulator.Stream(source, **options) as stream:
cast = str
if six.PY2:
cast = unicode # noqa
Expand Down
2 changes: 1 addition & 1 deletion tabulator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import unicode_literals


# Module API
# General

DEFAULT_SCHEME = 'file'
DEFAULT_ENCODING = 'utf-8'
Expand Down
25 changes: 25 additions & 0 deletions tabulator/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,31 @@ def save(self, target, format=None, encoding=None, **options):
writer = writer_class(**writer_options)
writer.write(target, encoding, extended_rows)

@staticmethod
def test(source, scheme=None, format=None):
"""Test if this source has supported scheme and format.
Args:
source (str): stream source
scheme (str): stream scheme
format (str): stream format
Returns:
bool: True if source source has supported scheme and format
"""
if scheme is None:
scheme = helpers.detect_scheme(source)
if not scheme:
scheme = config.DEFAULT_SCHEME
if scheme not in config.LOADERS:
return False
if format is None:
format = helpers.detect_format(source)
if format not in config.PARSERS:
return False
return True

# Private

def __extract_sample(self):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ def test_cli():
result = runner.invoke(cli, ['data/table.csv'])
assert result.exit_code == 0
assert result.output.startswith('id, name\n1, english\n2,')


def test_cli_version():
runner = CliRunner()
result = runner.invoke(cli, ['--version'])
assert result.exit_code == 0
assert len(result.output.split('.')) == 3
15 changes: 15 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals

import tabulator


# Tests

def test_public_api():
assert isinstance(tabulator.Stream, type)
assert isinstance(tabulator.exceptions, object)
assert len(tabulator.__version__.split('.')) == 3
41 changes: 41 additions & 0 deletions tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import io
import pytest
from tabulator import Stream, exceptions

Expand Down Expand Up @@ -181,3 +182,43 @@ def test_stream_http_error():
stream = Stream('http://github.com/bad_path.csv')
with pytest.raises(exceptions.HTTPError) as excinfo:
stream.open()


# Tests [test]

def test_stream_test_schemes():
# Supported
assert Stream.test('path.csv')
assert Stream.test('file://path.csv')
assert Stream.test('http://example.com/path.csv')
assert Stream.test('https://example.com/path.csv')
assert Stream.test('ftp://example.com/path.csv')
assert Stream.test('ftps://example.com/path.csv')
assert Stream.test('path.csv', scheme='file')
# Not supported
assert not Stream.test('ssh://example.com/path.csv')
assert not Stream.test('bad://example.com/path.csv')

def test_stream_test_formats():
# Supported
assert Stream.test('path.csv')
assert Stream.test('path.json')
assert Stream.test('path.jsonl')
assert Stream.test('path.ndjson')
assert Stream.test('path.tsv')
assert Stream.test('path.xls')
assert Stream.test('path.ods')
assert Stream.test('path.no-format', format='csv')
# Not supported
assert not Stream.test('path.txt')
assert not Stream.test('path.bad')

def test_stream_test_special():
# Gsheet
assert Stream.test('https://docs.google.com/spreadsheets/d/id', format='csv')
# File-like
assert Stream.test(io.open('data/table.csv', encoding='utf-8'), format='csv')
# Text
assert Stream.test('text://name,value\n1,2', format='csv')
# Native
assert Stream.test([{'name': 'value'}])

0 comments on commit 2278e39

Please sign in to comment.