Guest User

CSV > DOM converter

a guest
May 20th, 2010
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. # -*- coding: iso-8859-1 -*-
  2. """
  3. MoinMoin - Comma-separated values input converter
  4.  
  5. Convert from Comma-separated values into DOM table format.
  6.  
  7. @copyright: 2010 MoinMoin:DmitryAndreev
  8. @license: GNU GPL, see COPYING for details.
  9. """
  10. from __future__ import absolute_import
  11.  
  12. delimiter = ';'
  13. quotechar = '"'
  14.  
  15. from MoinMoin.util.tree import moin_page
  16.  
  17. from csv import reader as csvparser
  18.  
  19. def unicode_csvparser(unicode_csv_data, **kwargs):
  20.     """
  21.    This is csv.reader with unicode support
  22.    """
  23.     # csv.py doesn't do Unicode; encode temporarily as UTF-8:
  24.     csv_reader = csvparser(utf_8_encoder(unicode_csv_data), **kwargs)
  25.     for row in csv_reader:
  26.     # decode UTF-8 back to Unicode, cell by cell:
  27.         yield [unicode(cell, 'utf-8') for cell in row]
  28.  
  29. def utf_8_encoder(unicode_csv_data):
  30.     """
  31.    Convert text in utf-8
  32.    """
  33.     for line in unicode_csv_data:
  34.         yield line.encode('utf-8')
  35.  
  36. class Converter(object):
  37.     """
  38.    Parse csv and create table in DOM format.
  39.    """
  40.     @classmethod
  41.     def factory(cls, _request, input, output, **kw):
  42.         if output.type == 'application' and output.subtype == 'x.moin.document':
  43.             if input.type == 'text' and input.subtype == 'csv':
  44.                 return cls
  45.             if (input.type == 'text' and input.subtype == 'format' and
  46.                     input.parameters.get('name') == 'csv'):
  47.                 return cls
  48.  
  49.     def __init__(self,_request, delimiter = delimiter, quotechar = delimiter):
  50.         self.delimiter = delimiter
  51.         self.quotechar = quotechar
  52.         pass
  53.  
  54.     def __call__(self, content, arguments={}):
  55.         """
  56.        Parse the csv text and return DOM tree.
  57.        
  58.        @param content: csv text, list of lines.
  59.        """
  60.         delimiter = arguments.get('delimiter', defaule=delimiter)
  61.         splited_rows = unicode_csvparser(content, delimiter=self.delimiter, quotechar=self.quotechar)
  62.         rows = []
  63.         for row in splited_rows:
  64.             cells = []
  65.             for cell in row:
  66.                 cells.append(moin_page.table_cell(children=(cell, )))
  67.             rows.append(moin_page.table_row(children=cells))
  68.        
  69.         table_body = moin_page.table_body(children=rows)
  70.         table = moin_page.table(children=(table_body, ))
  71.         page_body = moin_page.body(children=(table, ))
  72.  
  73.         return moin_page.page(children=(page_body, ))
  74.  
  75. from . import default_registry
  76. default_registry.register(Converter.factory)
Advertisement
Add Comment
Please, Sign In to add comment