Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
MoinMoin.parser.text_csv.Parser Class Reference

List of all members.

Public Member Functions

def __init__
def format

Public Attributes

 request
 data

Static Public Attributes

list extensions = ['.csv']
list Dependencies = []

Private Member Functions

def _read_rows

Private Attributes

 _first_row
 _show_header

Detailed Description

Definition at line 41 of file text_csv.py.


Constructor & Destructor Documentation

def MoinMoin.parser.text_csv.Parser.__init__ (   self,
  raw,
  request,
  kw 
)

Definition at line 51 of file text_csv.py.

00051 
00052     def __init__(self, raw, request, **kw):
00053         self.request = request
00054         self._first_row = None
00055         formatter = request.formatter
00056 
00057         # workaround csv.reader deficiency by encoding to utf-8
00058         # removes empty lines in front of the csv table
00059         data = raw.encode('utf-8').lstrip('\n').split('\n')
00060 
00061         delimiter = ';'
00062         # Previous versions of this parser have used only the delimiter ";" (by default).
00063         # This version now tries to sniff the delimiter from the list preferred_delimiters
00064         # Although the Python csv sniffer had quite some changes from py 2.3 to 2.5.1, we try
00065         # to avoid problems for the case it does not find a delimiter in some given data.
00066         # Newer versions of the sniffer do raise an _csv.Error while older versions do
00067         # return a whitespace as delimiter.
00068         if data[0]:
00069             try:
00070                 preferred_delimiters = [',', '\t', ';', ' ', ':']
00071                 delimiter = Sniffer().sniff(data[0], preferred_delimiters).delimiter or ';'
00072             except Error:
00073                 pass
00074 
00075         visible = None
00076         hiddenindexes = []
00077         hiddencols = []
00078         autofiltercols = []
00079         staticcols = []
00080         staticvals = []
00081         linkcols = []
00082         quotechar = '\x00' # can't be entered
00083         quoting = QUOTE_NONE
00084         name = None
00085         hdr = reader([kw.get('format_args', '').strip().encode('utf-8')], delimiter=" ")
00086         args = hdr.next()
00087 
00088         for arg in args:
00089             arg = arg.decode('utf-8')
00090             try:
00091                 key, val = arg.split('=', 1)
00092             except:
00093                 # handle compatibility with original 'csv' parser
00094                 if arg.startswith('-'):
00095                     try:
00096                         hiddenindexes.append(int(arg[1:]) - 1)
00097                     except ValueError:
00098                         pass
00099                 else:
00100                     delimiter = arg.encode('utf-8')
00101                 continue
00102             if key == 'separator' or key == 'delimiter':
00103                 delimiter = val.encode('utf-8')
00104             if key == 'quotechar':
00105                 if val == val.encode('utf-8'):
00106                     quotechar = val.encode('utf-8')
00107                     quoting = QUOTE_MINIMAL
00108             elif key == 'show':
00109                 visible = val.split(',')
00110             elif key == 'hide':
00111                 hiddencols = val.split(',')
00112             elif key == 'autofilter':
00113                 autofiltercols = val.split(',')
00114             elif key == 'name':
00115                 name = val
00116             elif key == 'static_cols':
00117                 staticcols = val.split(',')
00118             elif key == 'static_vals':
00119                 staticvals = val.split(',')
00120             elif key == 'link':
00121                 linkcols = val.split(',')
00122 
00123         if len(staticcols) > len(staticvals):
00124             staticvals.extend([''] * (len(staticcols)-len(staticvals)))
00125         elif len(staticcols) < len(staticvals):
00126             staticvals = staticvals[:len(staticcols)]
00127 
00128         r = reader(data, delimiter=delimiter, quotechar=quotechar, quoting=quoting)
00129         cols = map(lambda x: x.decode('utf-8'), r.next()) + staticcols
00130 
00131         self._show_header = True
00132 
00133         if cols == staticcols:
00134             try:
00135                 self._first_row = map(lambda x: x.decode('utf-8'), r.next())
00136                 cols = [None] * len(self._first_row) + staticcols
00137                 self._show_header = False
00138             except StopIteration:
00139                 pass
00140 
00141         num_entry_cols = len(cols) - len(staticcols)
00142 
00143         if not visible is None:
00144             for col in cols:
00145                 if not col in visible:
00146                     hiddencols.append(col)
00147 
00148         linkparse = [False] * len(cols)
00149 
00150         data = TupleDataset(name)
00151         for colidx in range(len(cols)):
00152             col = cols[colidx]
00153             autofilter = col in autofiltercols
00154             hidden = col in hiddencols or colidx in hiddenindexes
00155             data.columns.append(Column(col, autofilter=autofilter, hidden=hidden))
00156 
00157             linkparse[colidx] = col in linkcols
00158 
00159         for row in self._read_rows(r):
00160             row = map(lambda x: x.decode('utf-8'), row)
00161             if len(row) > num_entry_cols:
00162                 row = row[:num_entry_cols]
00163             elif len(row) < num_entry_cols:
00164                 row.extend([''] * (num_entry_cols-len(row)))
00165             row += staticvals
00166             for colidx in range(len(row)):
00167                 item = row[colidx]
00168                 if linkparse[colidx]:
00169                     try:
00170                         url, item = item.split(' ', 1)
00171                         if url == '':
00172                             display = escape(item)
00173                         else:
00174                             display = ''.join([
00175                                 formatter.url(1, url=url),
00176                                 formatter.text(item),
00177                                 formatter.url(0)])
00178                     except ValueError:
00179                         display = escape(item)
00180                 else:
00181                     display = escape(item)
00182                 row[colidx] = (display, item)
00183             data.addRow(tuple(row))
00184         self.data = data


Member Function Documentation

def MoinMoin.parser.text_csv.Parser._read_rows (   self,
  r 
) [private]

Definition at line 45 of file text_csv.py.

00045 
00046     def _read_rows(self, r):
00047         if self._first_row is not None:
00048             yield self._first_row
00049         for row in r:
00050             yield row

def MoinMoin.parser.text_csv.Parser.format (   self,
  formatter 
)

Definition at line 185 of file text_csv.py.

00185 
00186     def format(self, formatter):
00187         browser = DataBrowserWidget(self.request, show_header=self._show_header)
00188         browser.setData(self.data)
00189         self.request.write(browser.render(method="GET"))

Member Data Documentation

Definition at line 53 of file text_csv.py.

Definition at line 130 of file text_csv.py.

Definition at line 183 of file text_csv.py.

Definition at line 43 of file text_csv.py.

Definition at line 42 of file text_csv.py.

Definition at line 52 of file text_csv.py.


The documentation for this class was generated from the following file: