_dumpCSV.py :  » Web-Frameworks » Webware » Webware-1.0.2 » CGIWrapper » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » CGIWrapper » _dumpCSV.py
"""CGIWrapper dump CSV admin script."""

import os

from WebUtils.Funcs import htmlEncode
from AdminPage import AdminPage


def LoadCSV(filename):
  """Load CSV file.

  Loads a CSV (comma-separated value) file from disk and returns it as a
  list of rows where each row is a list of values (which are always strings).

  """
  try:
    f = open(filename)
    try:
      rows = []
      while 1:
        line = f.readline()
        if not line:
          break
        rows.append(line.split(','))
    finally:
      f.close()
  except IOError:
    rows = []
  return rows


class _dumpCSV(AdminPage):
  """CGIWrapper class that dumps a CSV file."""

  def __init__(self, dict):
    AdminPage.__init__(self, dict)
    self._filename = self._fields['filename'].value

  def shortFilename(self):
    return os.path.splitext(os.path.split(self._filename)[1])[0]

  def title(self):
    return 'View ' + self.shortFilename()

  def writeBody(self):
    rows = LoadCSV(self._filename)

    self.writeln('<table align="center" border="0" cellpadding="2" cellspacing="2">')

    # Head row gets special formatting
    self._headings = map(lambda name: name.strip(), rows[0])
    self.writeln('<tr>')
    for value in self._headings:
      self.writeln('<th style="color:white;font-family:Arial,Helvetica,sans-serif" bgcolor="#101040">',
      value, '</th>')
    self.writeln('</tr>')

    # Data rows
    rowIndex = 1
    for row in rows[1:]:
      self.writeln('<tr>')
      colIndex = 0
      for value in row:
        self.writeln('<td style="color:#111111" bgcolor="#EEEEEE">',
          self.cellContents(rowIndex, colIndex, value), '</td>')
        colIndex += 1
      self.writeln('</tr>')
      rowIndex += 1

    self.writeln('</table>')

  def cellContents(self, rowIndex, colIndex, value):
    """Return cell contents of CSV file.

    This is a hook for subclasses to customize the contents of a cell
    based on any criteria (including location).

    """
    return htmlEncode(value)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.