summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Reitz <me@kennethreitz.com>2010-09-25 05:49:21 -0400
committerKenneth Reitz <me@kennethreitz.com>2010-09-25 05:49:21 -0400
commit63d025888aeaf61022524f09d520e4b29687bdc5 (patch)
tree8ed36b98dece842a2ba3156aa7f76159ef1d9100
parent5a993ac281f5dbfd0cee2039c61af0701e5fa323 (diff)
downloadtablib-63d025888aeaf61022524f09d520e4b29687bdc5.tar.gz
Added format importers.
-rw-r--r--tablib/formats/__init__.py11
-rw-r--r--tablib/formats/_csv.py41
-rw-r--r--tablib/formats/_json.py32
-rw-r--r--tablib/formats/_xls.py42
-rw-r--r--tablib/formats/_yaml.py27
5 files changed, 153 insertions, 0 deletions
diff --git a/tablib/formats/__init__.py b/tablib/formats/__init__.py
new file mode 100644
index 0000000..b9c7451
--- /dev/null
+++ b/tablib/formats/__init__.py
@@ -0,0 +1,11 @@
+# -*- coding: utf-8 -*-
+
+""" Tablib - formats
+"""
+
+import _csv as csv
+import _json as json
+import _xls as xls
+import _yaml as yaml
+
+formats = (csv, json, xls, yaml)
diff --git a/tablib/formats/_csv.py b/tablib/formats/_csv.py
new file mode 100644
index 0000000..0554976
--- /dev/null
+++ b/tablib/formats/_csv.py
@@ -0,0 +1,41 @@
+# -*- coding: utf-8 -*-
+
+import cStringIO
+import csv
+import os
+
+import simplejson as json
+
+import tablib
+
+
+title = 'csv'
+extentions = ('csv',)
+
+
+
+def export_set(dataset):
+ """Returns CSV representation of Dataset."""
+ stream = cStringIO.StringIO()
+ _csv = csv.writer(stream)
+
+ for row in dataset._package(dicts=False):
+ _csv.writerow(row)
+
+ return stream.getvalue()
+
+
+def import_set(in_stream, headers=True):
+ """Returns dataset from CSV stream."""
+
+ data = tablib.core.Dataset()
+
+ rows = csv.reader(in_stream.split())
+ for i, row in enumerate(rows):
+
+ if (i == 1) and (headers):
+ data.headers = row
+ else:
+ data.append(row)
+
+ return data \ No newline at end of file
diff --git a/tablib/formats/_json.py b/tablib/formats/_json.py
new file mode 100644
index 0000000..acbaf57
--- /dev/null
+++ b/tablib/formats/_json.py
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+
+import simplejson as json
+import tablib.core
+
+title = 'json'
+extentions = ('json', 'jsn')
+
+
+def export_set(dataset):
+ """Returns JSON representation of Dataset."""
+ return json.dumps(dataset.dict)
+
+
+def export_book(databook):
+ """Returns JSON representation of Databook."""
+
+ return json.dumps(databook._package())
+
+
+def detect(contents):
+ """Return True if contets are JSON."""
+ return False
+
+
+def import_set(in_stream):
+ """Returns dataset from JSON stream."""
+ data = tablib.core.Dataset()
+ data.dict = json.loads(in_stream)
+
+ return data
+
diff --git a/tablib/formats/_xls.py b/tablib/formats/_xls.py
new file mode 100644
index 0000000..67bd31c
--- /dev/null
+++ b/tablib/formats/_xls.py
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+
+import xlwt
+import cStringIO
+
+
+title = 'xls'
+extentions = ('xls')
+
+
+def export_set(dataset):
+ """Returns XLS representation of 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)
+
+ stream = cStringIO.StringIO()
+ wb.save(stream)
+ return stream.getvalue()
+
+
+def export_book(databook):
+ """Returns XLS representation of DataBook."""
+
+ wb = xlwt.Workbook(encoding='utf8')
+
+ 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)
+
+
+ stream = cStringIO.StringIO()
+ wb.save(stream)
+ return stream.getvalue() \ No newline at end of file
diff --git a/tablib/formats/_yaml.py b/tablib/formats/_yaml.py
new file mode 100644
index 0000000..35c2bf2
--- /dev/null
+++ b/tablib/formats/_yaml.py
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+
+import yaml
+import tablib
+
+title = 'yaml'
+extentions = ('yaml', 'yml')
+
+
+
+def export_set(dataset):
+ """Returns YAML representation of Dataset."""
+ return yaml.dump(dataset.dict)
+
+
+def export_book(databook):
+ """Returns YAML representation of Databook."""
+ return yaml.dump(databook._package())
+
+
+def import_set(in_stream):
+ """Returns dataset from YAML stream."""
+
+ data = tablib.core.Dataset()
+ data.dict = yaml.load(in_stream)
+
+ return data \ No newline at end of file