diff options
| author | Kenneth Reitz <me@kennethreitz.com> | 2010-10-04 11:55:48 -0400 |
|---|---|---|
| committer | Kenneth Reitz <me@kennethreitz.com> | 2010-10-04 11:55:48 -0400 |
| commit | 520a1986d72f4baad3607911db0023967e1b9246 (patch) | |
| tree | 07161b0af50e3b828a2638750e390555af6474bc /tablib | |
| parent | c223dfbdf12bfa9ec6458184b83ac3e458efb185 (diff) | |
| parent | 1ea793112cdbc7328e37138e12aa0bfa8cedd9ea (diff) | |
| download | tablib-0.8.3.tar.gz | |
Merge branch 'release/0.8.3'v0.8.3
Diffstat (limited to 'tablib')
| -rw-r--r-- | tablib/__init__.py | 4 | ||||
| -rw-r--r-- | tablib/cli.py | 84 | ||||
| -rw-r--r-- | tablib/core.py | 17 | ||||
| -rw-r--r-- | tablib/formats/__init__.py | 2 | ||||
| -rw-r--r-- | tablib/formats/_csv.py | 9 | ||||
| -rw-r--r-- | tablib/formats/_json.py | 9 | ||||
| -rw-r--r-- | tablib/formats/_yaml.py | 13 |
7 files changed, 130 insertions, 8 deletions
diff --git a/tablib/__init__.py b/tablib/__init__.py index fadd8dd..3f23850 100644 --- a/tablib/__init__.py +++ b/tablib/__init__.py @@ -2,7 +2,7 @@ """ from tablib.core import ( - Databook, Dataset, InvalidDatasetType, - InvalidDimensions, UnsupportedFormat + Databook, Dataset, detect, import_set, + InvalidDatasetType, InvalidDimensions, UnsupportedFormat ) diff --git a/tablib/cli.py b/tablib/cli.py new file mode 100644 index 0000000..6d773c9 --- /dev/null +++ b/tablib/cli.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# encoding: utf-8 + +""" Tabbed CLI Inteface Application +""" + +import io +import sys + +import argue + +import tablib +from helpers import Struct, piped + + + +FORMATS = [fmt.title for fmt in tablib.formats.FORMATS] + +opts = [] + +opts.append(('v', 'version', False, 'Report tabbed version')) + +for format in FORMATS: + opts.append(('', format, False, 'Output to %s' % (format.upper()))) + + + +@argue.command(options=opts, usage='[FILE] [--FORMAT | FILE]') +def start(in_file=None, out_file=None, **opts): + """Covertly convert dataset formats""" + + opts = Struct(**opts) + + if opts.version: + print('Tabbed, Ver. %s' % tablib.core.__version__) + sys.exit(0) + + stdin = piped() + + if stdin: + data = tablib.import_set(stdin) + + elif in_file: + + try: + in_stream =- io.open(in_file, 'r').read() + except Exception, e: + print(' %s cannot be read.' % in_file) + sys.exit(65) + + try: + tablib.import_set(in_stream) + except Exception, e: + raise e + print('Import format not supported.') + sys.exit(65) + else: + print('Please provide input.') + sys.exit(65) + + + _formats_sum = sum(opts[f] for f in FORMATS) + + # Multiple output formats given + if _formats_sum > 1: + print('Please specify a single output format.') + sys.exit(64) + + # No output formats given + elif _formats_sum < 1: + print('Please specify an output format.') + sys.exit(64) + + + # fetch options.formats list + # if sum(()) > 1 + # log only one data format please + # if sum of formats == 0, specity format + + # look for filename + + # print opts.__dict__ + # print in_file + # print out_file
\ No newline at end of file diff --git a/tablib/core.py b/tablib/core.py index e7b622c..69e7549 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -7,8 +7,8 @@ from tablib.formats import FORMATS as formats __title__ = 'tablib' -__version__ = '0.8.1' -__build__ = 0x000801 +__version__ = '0.8.3' +__build__ = 0x000803 __author__ = 'Kenneth Reitz' __license__ = 'MIT' __copyright__ = 'Copyright 2010 Kenneth Reitz' @@ -177,10 +177,19 @@ class Dataset(object): def append(self, row=None, col=None): """Adds a row to the end of Dataset""" - if row: + if row is not None: self._validate(row) self._data.append(tuple(row)) - elif col: + elif col is not None: + col = list(col) + if self.headers: + header = [col.pop(0)] + else: + header = [] + if len(col) == 1 and callable(col[0]): + col = map(col[0], self._data) + col = tuple(header + col) + self._validate(col=col) if self.headers: diff --git a/tablib/formats/__init__.py b/tablib/formats/__init__.py index b22a959..69eada7 100644 --- a/tablib/formats/__init__.py +++ b/tablib/formats/__init__.py @@ -8,4 +8,4 @@ import _json as json import _xls as xls import _yaml as yaml -FORMATS = (csv, json, xls, yaml) +FORMATS = (json, xls, yaml, csv) diff --git a/tablib/formats/_csv.py b/tablib/formats/_csv.py index 8b19da7..27d2e0d 100644 --- a/tablib/formats/_csv.py +++ b/tablib/formats/_csv.py @@ -40,3 +40,12 @@ def import_set(dset, in_stream, headers=True): dset.headers = row else: dset.append(row) + + +def detect(stream): + """Returns True if given stream is valid CSV.""" + try: + rows = dialect = csv.Sniffer().sniff(stream) + return True + except csv.Error: + return False
\ No newline at end of file diff --git a/tablib/formats/_json.py b/tablib/formats/_json.py index 1f92b58..f7c88ee 100644 --- a/tablib/formats/_json.py +++ b/tablib/formats/_json.py @@ -36,3 +36,12 @@ def import_book(dbook, in_stream): data.title = sheet['title'] data.dict = sheet['data'] dbook.add_sheet(data) + + +def detect(stream): + """Returns True if given stream is valid JSON.""" + try: + json.loads(stream) + return True + except json.decoder.JSONDecodeError: + return False
\ No newline at end of file diff --git a/tablib/formats/_yaml.py b/tablib/formats/_yaml.py index 4cac8aa..57d63d7 100644 --- a/tablib/formats/_yaml.py +++ b/tablib/formats/_yaml.py @@ -39,4 +39,15 @@ def import_book(dbook, in_stream): data = tablib.core.Dataset() data.title = sheet['title'] data.dict = sheet['data'] - dbook.add_sheet(data)
\ No newline at end of file + dbook.add_sheet(data) + +def detect(stream): + """Returns True if given stream is valid YAML.""" + try: + _yaml = yaml.load(stream) + if isinstance(_yaml, (list, tuple, dict)): + return True + else: + return False + except yaml.parser.ParserError: + return False
\ No newline at end of file |
