view MoinMoin/converter/text_csv_in.py @ 2109:38b5c2746f83

raise minimum requirement to python 2.7 now that debian wheezy is stable (and thus even debian stable has python 2.7 - almost 3 years after the python 2.7 release), it is time to move on. there are some nice features in 2.7 - we can use them now: http://docs.python.org/2.7/whatsnew/2.7.html there is also no risk any more that language or library features creep in unnoticed that are incompatible with the minimum requirement. we also save some time as we don't have to test on / care for 2.6 any more. about this changeset: most should be self-explanatory, except this: MoinMoin/constants/keys.py - we could not use unicode key constants yet due to a bug in pre-2.6.5 python that did not allow unicode keys in kwargs. The change in MoinMoin/storage/middleware/indexing.py is also related to that, we just had to make sure keys are str, not unicode as that dict later was exploded into a function call like f(**doc). notes for developers: if you developed on 2.6 until now, you need to create a fresh new virtualenv using python 2.7.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 04 May 2013 16:08:33 +0200
parents bff62fea804a
children e0afda75944f
line wrap: on
line source
# Copyright: 2010 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - CSV text data to DOM converter
"""


from __future__ import absolute_import, division

import csv

from ._table import TableMixin
from ._util import decode_data, normalize_split_text
from MoinMoin.util.tree import moin_page


class Converter(TableMixin):
    """
    Parse the raw text and create a document object
    that can be converted into output using Emitter.
    """
    @classmethod
    def _factory(cls, type_input, type_output, **kw):
        return cls()

    def __call__(self, data, contenttype=None, arguments=None):
        text = decode_data(data, contenttype)
        content = normalize_split_text(text)
        # as of py 2.7.x (and in the year 2013), the csv module seems to still
        # have troubles with unicode, thus we encode to utf-8 ...
        content = [line.encode('utf-8') for line in content]
        dialect = csv.Sniffer().sniff(content[0])
        reader = csv.reader(content, dialect)
        # ... and decode back to unicode
        rows = []
        for encoded_row in reader:
            row = []
            for encoded_cell in encoded_row:
                row.append(encoded_cell.decode('utf-8'))
            if row:
                rows.append(row)
        table = self.build_dom_table(rows)
        body = moin_page.body(children=(table, ))
        return moin_page.page(children=(body, ))


from . import default_registry
from MoinMoin.util.mime import Type, type_moin_document
default_registry.register(Converter._factory, Type('text/csv'), type_moin_document)