Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: iso-8859-1 -*-
- """
- MoinMoin - Comma-separated values input converter
- Convert from Comma-separated values into DOM table format.
- @copyright: 2010 MoinMoin:DmitryAndreev
- @license: GNU GPL, see COPYING for details.
- """
- from __future__ import absolute_import
- delimiter = ';'
- quotechar = '"'
- from MoinMoin.util.tree import moin_page
- from csv import reader as csvparser
- def unicode_csvparser(unicode_csv_data, **kwargs):
- """
- This is csv.reader with unicode support
- """
- # csv.py doesn't do Unicode; encode temporarily as UTF-8:
- csv_reader = csvparser(utf_8_encoder(unicode_csv_data), **kwargs)
- for row in csv_reader:
- # decode UTF-8 back to Unicode, cell by cell:
- yield [unicode(cell, 'utf-8') for cell in row]
- def utf_8_encoder(unicode_csv_data):
- """
- Convert text in utf-8
- """
- for line in unicode_csv_data:
- yield line.encode('utf-8')
- class Converter(object):
- """
- Parse csv and create table in DOM format.
- """
- @classmethod
- def factory(cls, _request, input, output, **kw):
- if output.type == 'application' and output.subtype == 'x.moin.document':
- if input.type == 'text' and input.subtype == 'csv':
- return cls
- if (input.type == 'text' and input.subtype == 'format' and
- input.parameters.get('name') == 'csv'):
- return cls
- def __init__(self,_request, delimiter = delimiter, quotechar = delimiter):
- self.delimiter = delimiter
- self.quotechar = quotechar
- pass
- def __call__(self, content, arguments={}):
- """
- Parse the csv text and return DOM tree.
- @param content: csv text, list of lines.
- """
- delimiter = arguments.get('delimiter', defaule=delimiter)
- splited_rows = unicode_csvparser(content, delimiter=self.delimiter, quotechar=self.quotechar)
- rows = []
- for row in splited_rows:
- cells = []
- for cell in row:
- cells.append(moin_page.table_cell(children=(cell, )))
- rows.append(moin_page.table_row(children=cells))
- table_body = moin_page.table_body(children=rows)
- table = moin_page.table(children=(table_body, ))
- page_body = moin_page.body(children=(table, ))
- return moin_page.page(children=(page_body, ))
- from . import default_registry
- default_registry.register(Converter.factory)
Advertisement
Add Comment
Please, Sign In to add comment