Compare commits

...

49 Commits

Author SHA1 Message Date
Kenneth Reitz 930d38cf5a Merge branch 'release/0.8.4' 2010-10-04 15:52:43 -04:00
Kenneth Reitz 5e433c263d version bump v0.8.4 2010-10-04 15:51:45 -04:00
Kenneth Reitz 19ac9b9716 Updated history for v0.8.4. 2010-10-04 15:51:05 -04:00
Kenneth Reitz 6feb59504a Version bump. 2010-10-04 15:50:52 -04:00
Kenneth Reitz 817eedd6f5 Only wrap when needed. 2010-10-04 15:50:41 -04:00
Kenneth Reitz 4d1c5a9996 Merge branch 'release/0.8.3' into develop 2010-10-04 11:55:59 -04:00
Kenneth Reitz 520a1986d7 Merge branch 'release/0.8.3' 2010-10-04 11:55:48 -04:00
Kenneth Reitz 1ea793112c Version Bump (v0.8.3) 2010-10-04 11:55:35 -04:00
Kenneth Reitz 41a7a5d329 No cli app at this time. 2010-10-04 11:55:26 -04:00
Kenneth Reitz c4edaa2ca8 Appended history. 2010-10-04 11:55:17 -04:00
Kenneth Reitz c612bb3dae Merge branch 'master' into develop 2010-10-04 11:52:12 -04:00
Kenneth Reitz c223dfbdf1 Merge branch 'hotfix/0.8.2' 2010-10-04 11:40:45 -04:00
Kenneth Reitz 49bd48b016 namspace fix. 2010-10-04 11:39:59 -04:00
Kenneth Reitz c6d90bc825 Updated history. 2010-10-04 11:39:05 -04:00
Kenneth Reitz bcd0e37a65 Version bump. 2010-10-04 11:38:28 -04:00
Kenneth Reitz 8c92e878a3 Upgraded XLS abstraction layer. 2010-10-04 11:38:17 -04:00
Kenneth Reitz da2b011358 Added separator support for XLS output. 2010-10-04 11:33:34 -04:00
Kenneth Reitz a8b0bf4b5f Typo. 2010-10-04 11:33:16 -04:00
Kenneth Reitz 6574d3e58b XLS support for Separators.
Bolden headers and Separators.
2010-10-04 10:54:14 -04:00
Kenneth Reitz 1020799828 Separator append and insert support. 2010-10-04 10:53:48 -04:00
Kenneth Reitz 333e73f892 Added wrapping support. 2010-10-04 10:19:31 -04:00
Kenneth Reitz bfe70066b8 Added Josh Ourisman to authors 2010-10-01 18:44:50 -04:00
Kenneth Reitz fbfbe01b70 Merge branch 'joshmerge' into develop 2010-10-01 17:56:31 -04:00
Kenneth Reitz 06a394ea5c typo in setup.py. 2010-10-01 17:52:50 -04:00
Kenneth Reitz 9427decdb0 Changes. 2010-10-01 17:52:08 -04:00
Kenneth Reitz fb59035f8d Added tablib.import_set() and tested accordingly. 2010-10-01 17:52:08 -04:00
Kenneth Reitz 187d12cffc Format Auto-detection in place.
Test suite updated.
2010-10-01 17:52:08 -04:00
Kenneth Reitz eaa4de7793 Auto-detectors operational. 2010-10-01 17:52:08 -04:00
Kenneth Reitz d479c5735a Hmmm.... 2010-10-01 17:52:08 -04:00
Kenneth Reitz 96668bb393 tabbed runner 2010-10-01 17:52:08 -04:00
Kenneth Reitz b369baba40 Added runner (for testing). 2010-10-01 17:52:08 -04:00
Kenneth Reitz 25f846a78a Added entrance point, setup.py updates. 2010-10-01 17:52:08 -04:00
Kenneth Reitz 22fe18239f Added legacy cli interface. 2010-10-01 17:51:30 -04:00
Josh Ourisman 149bafa97b added ability to append new column passing a callable as the value that will be applied to every row; w/ test 2010-10-01 16:17:04 -04:00
Josh Ourisman 9f7fec2379 changing syntax of checking for row and col values in append(); slightly more robust this way 2010-10-01 15:27:28 -04:00
Josh Ourisman 762ac39e27 resolved merge conflict 2010-10-01 14:57:36 -04:00
Josh Ourisman 2a7aa959b3 modified .gitignore to actually ignore .pyc files 2010-10-01 14:51:36 -04:00
Kenneth Reitz d85523b6a6 typo in setup.py. 2010-09-28 09:01:34 -04:00
Kenneth Reitz 25a5bcea0c merge 2010-09-28 08:45:14 -04:00
Kenneth Reitz f58d4b67dc Changes. 2010-09-28 08:33:57 -04:00
Kenneth Reitz a310ab7a09 Added tablib.import_set() and tested accordingly. 2010-09-25 18:35:10 -04:00
Kenneth Reitz 7f2f925ddb Format Auto-detection in place.
Test suite updated.
2010-09-25 18:09:44 -04:00
Kenneth Reitz 3fc898e222 Auto-detectors operational. 2010-09-25 18:03:03 -04:00
Kenneth Reitz de46f45e2e Hmmm.... 2010-09-25 17:36:20 -04:00
Kenneth Reitz 392eaac299 tabbed runner 2010-09-25 17:28:46 -04:00
Kenneth Reitz 3a9c3944cf Added runner (for testing). 2010-09-25 17:27:53 -04:00
Kenneth Reitz 8c402da729 Added entrance point, setup.py updates. 2010-09-25 17:27:04 -04:00
Kenneth Reitz 8feb6e8ddf Added legacy cli interface. 2010-09-25 17:26:53 -04:00
Kenneth Reitz 9072b6ddae Merge branch 'release/0.8.0' into develop 2010-09-25 17:19:06 -04:00
13 changed files with 312 additions and 21 deletions
+2 -1
View File
@@ -10,4 +10,5 @@ Development Lead
Patches and Suggestions
```````````````````````
- Luke Lee
- Luke Lee
- Josh Ourisman
+19
View File
@@ -1,5 +1,24 @@
History
=======
0.8.4 (2010-10-04)
------------------
* Upated XLS output: Only wrap if '\n' in cell.
0.8.3 (2010-10-04)
------------------
* Ability to append new column passing a callable
as the value that will be applied to every row.
0.8.2 (2010-10-04)
------------------
* Added alignment wrapping to written cells.
* Added separator support to XLS.
0.8.1 (2010-09-28)
------------------
+1 -1
View File
@@ -17,7 +17,7 @@ if sys.argv[-1] == "publish":
setup(
name='tablib',
version='0.8.1',
version='0.8.4',
description='Format agnostic tabular data library (XLS, JSON, YAML, CSV)',
long_description=open('README.rst').read() + '\n\n' +
open('HISTORY.rst').read(),
Executable
+14
View File
@@ -0,0 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Tabbed -- CLI for Tablib
Copyright (c) 2010 Kenneth Reitz. MIT License.
"""
import tablib.cli
if __name__ == '__main__':
tablib.cli.start()
+2 -2
View File
@@ -2,7 +2,7 @@
"""
from tablib.core import (
Databook, Dataset, InvalidDatasetType,
InvalidDimensions, UnsupportedFormat
Databook, Dataset, detect, import_set,
InvalidDatasetType, InvalidDimensions, UnsupportedFormat
)
+84
View File
@@ -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
+63 -6
View File
@@ -7,8 +7,8 @@ from tablib.formats import FORMATS as formats
__title__ = 'tablib'
__version__ = '0.8.1'
__build__ = 0x000801
__version__ = '0.8.4'
__build__ = 0x000804
__author__ = 'Kenneth Reitz'
__license__ = 'MIT'
__copyright__ = 'Copyright 2010 Kenneth Reitz'
@@ -20,6 +20,9 @@ class Dataset(object):
def __init__(self, *args, **kwargs):
self._data = list(args)
self.__headers = None
# ('title', index) tuples
self._separators = []
try:
self.headers = kwargs['headers']
@@ -33,6 +36,7 @@ class Dataset(object):
self._register_formats()
def __len__(self):
return self.height
@@ -173,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:
@@ -194,7 +207,26 @@ class Dataset(object):
self._data = [tuple([row]) for row in col]
def insert(self, i, row=None, col=None):
def insert_separator(self, index, text='-'):
"""Adds a separator to Dataset at given index."""
sep = (index, text)
self._separators.append(sep)
def append_separator(self, text='-'):
"""Adds a separator to Dataset."""
# change offsets if headers are or aren't defined
if not self.headers:
index = self.height if self.height else 0
else:
index = (self.height + 1) if self.height else 1
self.insert_separator(index, text)
def insert(self, i, row=None):
"""Inserts a row at given position in Dataset"""
if row:
self._validate(row)
@@ -225,9 +257,11 @@ class Databook(object):
except AttributeError:
return '<databook object>'
def wipe(self):
"""Wipe book clean."""
self._datasets = []
@classmethod
def _register_formats(cls):
@@ -249,7 +283,7 @@ class Databook(object):
self._datasets.append(dataset)
else:
raise InvalidDatasetType
def _package(self):
"""Packages Databook for delivery."""
@@ -268,6 +302,29 @@ class Databook(object):
return len(self._datasets)
def detect(stream):
"""Return (format, stream) of given stream."""
for fmt in formats:
try:
if fmt.detect(stream):
return (fmt, stream)
except AttributeError:
pass
return (None, stream)
def import_set(stream):
"""Return dataset of given stream."""
(format, stream) = detect(stream)
try:
data = Dataset()
format.import_set(data, stream)
return data
except AttributeError, e:
return None
class InvalidDatasetType(Exception):
"Only Datasets can be added to a DataBook"
+1 -1
View File
@@ -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)
+9
View File
@@ -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
+9
View File
@@ -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
+37 -9
View File
@@ -10,6 +10,10 @@ import cStringIO
title = 'xls'
extentions = ('xls',)
# special styles
wrap = xlwt.easyxf("alignment: wrap on")
bold = xlwt.easyxf("font: bold on")
def export_set(dataset):
"""Returns XLS representation of Dataset."""
@@ -17,10 +21,8 @@ def export_set(dataset):
wb = xlwt.Workbook(encoding='utf8')
ws = wb.add_sheet(dataset.title if dataset.title else 'Tabbed Dataset')
for i, row in enumerate(dataset._package(dicts=False)):
for j, col in enumerate(row):
ws.write(i, j, col)
dset_sheet(dataset, ws)
stream = cStringIO.StringIO()
wb.save(stream)
return stream.getvalue()
@@ -34,12 +36,38 @@ def export_book(databook):
for i, dset in enumerate(databook._datasets):
ws = wb.add_sheet(dset.title if dset.title else 'Sheet%s' % (i))
#for row in self._package(dicts=False):
for i, row in enumerate(dset._package(dicts=False)):
for j, col in enumerate(row):
ws.write(i, j, col)
dset_sheet(dset, ws)
stream = cStringIO.StringIO()
wb.save(stream)
return stream.getvalue()
return stream.getvalue()
def dset_sheet(dataset, ws):
"""Completes given worksheet from given Dataset."""
_package = dataset._package(dicts=False)
for i, sep in enumerate(dataset._separators):
_offset = i
_package.insert((sep[0] + _offset), (sep[1],))
for i, row in enumerate(_package):
for j, col in enumerate(row):
# bold headers
if (i == 0) and dataset.headers:
ws.write(i, j, col, bold)
# bold separators
elif len(row) < dataset.width:
ws.write(i, j, col, bold)
# wrap the rest
else:
if '\n' in col:
ws.write(i, j, col, wrap)
else:
ws.write(i, j, col)
+12 -1
View File
@@ -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)
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
+59
View File
@@ -102,6 +102,13 @@ class TablibTestCase(unittest.TestCase):
self.assertRaises(tablib.InvalidDimensions, data.append, col=new_col)
def test_add_callable_column(self):
"""Verify adding column with values specified as callable."""
new_col = ['first_again', lambda x: x[0]]
self.founders.append(col=new_col)
self.assertTrue(map(lambda x: x[0] == x[-1], self.founders))
def test_header_slicing(self):
"""Verify slicing by headers."""
@@ -261,6 +268,58 @@ class TablibTestCase(unittest.TestCase):
self.assertEqual(_csv, data.csv)
def test_csv_format_detect(self):
"""Test CSV format detection."""
_csv = (
'1,2,3\n'
'4,5,6\n'
'7,8,9\n'
)
_bunk = (
'¡¡¡¡¡¡¡¡£™∞¢£§∞§¶•¶ª∞¶•ªº••ª–º§•†•§º¶•†¥ª–º•§ƒø¥¨©πƒø†ˆ¥ç©¨√øˆ¥≈†ƒ¥ç©ø¨çˆ¥ƒçø¶'
)
self.assertTrue(tablib.formats.csv.detect(_csv))
self.assertFalse(tablib.formats.csv.detect(_bunk))
def test_json_format_detect(self):
"""Test JSON format detection."""
_json = '[{"last_name": "Adams","age": 90,"first_name": "John"}]'
_bunk = (
'¡¡¡¡¡¡¡¡£™∞¢£§∞§¶•¶ª∞¶•ªº••ª–º§•†•§º¶•†¥ª–º•§ƒø¥¨©πƒø†ˆ¥ç©¨√øˆ¥≈†ƒ¥ç©ø¨çˆ¥ƒçø¶'
)
self.assertTrue(tablib.formats.json.detect(_json))
self.assertFalse(tablib.formats.json.detect(_bunk))
def test_yaml_format_detect(self):
"""Test YAML format detection."""
_yaml = '- {age: 90, first_name: John, last_name: Adams}'
_bunk = (
'¡¡¡¡¡¡---///\n\n\n¡¡£™∞¢£§∞§¶•¶ª∞¶•ªº••ª–º§•†•§º¶•†¥ª–º•§ƒø¥¨©πƒø†ˆ¥ç©¨√øˆ¥≈†ƒ¥ç©ø¨çˆ¥ƒçø¶'
)
self.assertTrue(tablib.formats.yaml.detect(_yaml))
self.assertFalse(tablib.formats.yaml.detect(_bunk))
def test_auto_format_detect(self):
"""Test auto format detection."""
_yaml = '- {age: 90, first_name: John, last_name: Adams}'
_json = '[{"last_name": "Adams","age": 90,"first_name": "John"}]'
_csv = '1,2,3\n4,5,6\n7,8,9\n'
_bunk = '¡¡¡¡¡¡---///\n\n\n¡¡£™∞¢£§∞§¶•¶ª∞¶•ªº••ª–º§•†•§º¶•†¥ª–º•§ƒø¥¨©πƒø†ˆ¥ç©¨√øˆ¥≈†ƒ¥ç©ø¨çˆ¥ƒçø¶'
self.assertEqual(tablib.detect(_yaml)[0], tablib.formats.yaml)
self.assertEqual(tablib.detect(_csv)[0], tablib.formats.csv)
self.assertEqual(tablib.detect(_json)[0], tablib.formats.json)
self.assertEqual(tablib.detect(_bunk)[0], None)
def test_wipe(self):
"""Purge a dataset."""