From 0d3f29b31ea8454276d9398136af7200d1d9b5f6 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 8 Aug 2011 15:01:21 +0100 Subject: Add CSV output format. --- summain | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'summain') diff --git a/summain b/summain index 73aa645..ba8b458 100755 --- a/summain +++ b/summain @@ -16,6 +16,7 @@ import cliapp +import csv import os import sys @@ -24,6 +25,9 @@ import summainlib class OutputFormat(object): + keys = ['Mtime', 'Mode', 'Ino', 'Dev', 'Nlink', 'Size', + 'Uid', 'Username', 'Gid', 'Group', 'Target'] + def __init__(self, output): self.output = output @@ -37,15 +41,31 @@ class OutputFormat(object): class Rfc822(OutputFormat): def write_object(self, name, o, checksums): - keys = (['Mtime', 'Mode', 'Ino', 'Dev', 'Nlink', 'Size', - 'Uid', 'Username', 'Gid', 'Group', 'Target'] + - checksums) + keys = self.keys + checksums values = [('Name', name)] values += [(k, o[k]) for k in keys if o[k] != ''] record = ''.join('%s: %s\n' % (k, v) for k, v in values if v != '') self.output.write('%s\n' % record) +class CSV(OutputFormat): + + def __init__(self, output): + OutputFormat.__init__(self, output) + self.writer = csv.writer(output) + self.wrote_headings = False + + def write_object(self, name, o, checksums): + keys = self.keys + checksums + + if not self.wrote_headings: + self.writer.writerow(keys) + self.wrote_headings = True + + values = [name] + [o[k] for k in keys if o[k] != ''] + self.writer.writerow(values) + + class Summain(cliapp.Application): def add_settings(self): @@ -94,7 +114,7 @@ class Summain(cliapp.Application): except KeyError: raise cliapp.AppException('Unknown checksum %s' % checksum) - fmt = Rfc822(self.output) + fmt = self.new_formatter() for root in args: for filename in self.files(root): o = summainlib.FilesystemObject(filename, nn, pn, exclude) @@ -125,5 +145,12 @@ class Summain(cliapp.Application): else: return pathname + def new_formatter(self): + table = { + 'rfc822': Rfc822, + 'csv': CSV, + } + return table[self.settings['output-format']](self.output) + Summain(version=summainlib.__version__).run() -- cgit v1.2.1