import os from AdminSecurity import AdminSecurity from MiscUtils.DataTable import DataTable class DumpCSV(AdminSecurity): def filename(self): """Overidden by subclasses to specify what filename to show.""" return None def awake(self, trans): AdminSecurity.awake(self, trans) self._filename = self.filename() def shortFilename(self): return os.path.splitext(os.path.split(self._filename)[1])[0] def title(self): return 'View ' + self.shortFilename() def writeContent(self): if not os.path.exists(self._filename): self.writeln('

File does not exist.

') return table = DataTable(self._filename) if len(table) == 1: plural = '' else: plural = 's' self.writeln('

%d row%s

' % (len(table), plural)) self.writeln('') # Head row gets special formatting self._headings = map(lambda col: col.name().strip(), table.headings()) self._numCols = len(self._headings) self.writeln('') for value in self._headings: self.writeln('') self.writeln('') # Data rows rowIndex = 1 for row in table: self.writeln('') colIndex = 0 for value in row: if colIndex < self._numCols: # for those cases where a row has more columns that the header row. self.writeln('') colIndex += 1 self.writeln('') rowIndex += 1 self.writeln('
', value, '
', self.cellContents(rowIndex, colIndex, value), '
') def cellContents(self, rowIndex, colIndex, value): """Hook for subclasses to customize the contents of a cell. Based on any criteria (including location). """ return value