-
-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #256 - Implement class-based formats
This allows to extend Tablib with new formats far more easily.
- Loading branch information
Showing
18 changed files
with
1,081 additions
and
1,022 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,77 @@ | ||
""" Tablib - formats | ||
""" | ||
from collections import OrderedDict | ||
from functools import partialmethod | ||
|
||
from . import _csv as csv | ||
from . import _dbf as dbf | ||
from . import _df as df | ||
from . import _html as html | ||
from . import _jira as jira | ||
from . import _json as json | ||
from . import _latex as latex | ||
from . import _ods as ods | ||
from . import _rst as rst | ||
from . import _tsv as tsv | ||
from . import _xls as xls | ||
from . import _xlsx as xlsx | ||
from . import _yaml as yaml | ||
|
||
# xlsx before as xls (xlrd) can also read xlsx | ||
available = (json, xlsx, xls, yaml, csv, dbf, tsv, html, jira, latex, ods, df, rst) | ||
from ._csv import CSVFormat | ||
from ._dbf import DBFFormat | ||
from ._df import DataFrameFormat | ||
from ._html import HTMLFormat | ||
from ._jira import JIRAFormat | ||
from ._json import JSONFormat | ||
from ._latex import LATEXFormat | ||
from ._ods import ODSFormat | ||
from ._rst import ReSTFormat | ||
from ._tsv import TSVFormat | ||
from ._xls import XLSFormat | ||
from ._xlsx import XLSXFormat | ||
from ._yaml import YAMLFormat | ||
|
||
|
||
class Registry: | ||
_formats = OrderedDict() | ||
|
||
def register(self, key, format_): | ||
from tablib.core import Databook, Dataset | ||
|
||
# Create Databook.<format> read or read/write properties | ||
try: | ||
setattr(Databook, format_.title, property(format_.export_book, format_.import_book)) | ||
except AttributeError: | ||
try: | ||
setattr(Databook, format_.title, property(format_.export_book)) | ||
except AttributeError: | ||
pass | ||
|
||
# Create Dataset.<format> read or read/write properties, | ||
# and Dataset.get_<format>/set_<format> methods. | ||
try: | ||
try: | ||
setattr(Dataset, format_.title, property(format_.export_set, format_.import_set)) | ||
setattr(Dataset, 'get_%s' % format_.title, partialmethod(Dataset._get_in_format, format_)) | ||
setattr(Dataset, 'set_%s' % format_.title, partialmethod(Dataset._set_in_format, format_)) | ||
except AttributeError: | ||
setattr(Dataset, format_.title, property(format_.export_set)) | ||
setattr(Dataset, 'get_%s' % format_.title, partialmethod(Dataset._get_in_format, format_)) | ||
|
||
except AttributeError: | ||
raise Exception("Your format class should minimally implement the export_set interface.") | ||
|
||
self._formats[key] = format_ | ||
|
||
def register_builtins(self): | ||
# Registration ordering matters for autodetection. | ||
self.register('json', JSONFormat()) | ||
# xlsx before as xls (xlrd) can also read xlsx | ||
self.register('xlsx', XLSXFormat()) | ||
self.register('xls', XLSFormat()) | ||
self.register('yaml', YAMLFormat()) | ||
self.register('csv', CSVFormat()) | ||
self.register('tsv', TSVFormat()) | ||
self.register('ods', ODSFormat()) | ||
self.register('dbf', DBFFormat()) | ||
self.register('html', HTMLFormat()) | ||
self.register('jira', JIRAFormat()) | ||
self.register('latex', LATEXFormat()) | ||
self.register('df', DataFrameFormat()) | ||
self.register('rst', ReSTFormat()) | ||
|
||
def formats(self): | ||
for frm in self._formats.values(): | ||
yield frm | ||
|
||
def get_format(self, key): | ||
return self._formats[key] | ||
|
||
|
||
registry = Registry() |
Oops, something went wrong.