diff options
author | Lars Wirzenius <liw@liw.fi> | 2011-08-08 15:01:21 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2011-08-08 15:01:21 +0100 |
commit | 0d3f29b31ea8454276d9398136af7200d1d9b5f6 (patch) | |
tree | 9168b03412815ab7338e4892c8d17260c13c80f5 /summain | |
parent | 94c14e3e7bfcaed2202640e2cf685d81bea4524f (diff) | |
download | summain-0d3f29b31ea8454276d9398136af7200d1d9b5f6.tar.gz |
Add CSV output format.
Diffstat (limited to 'summain')
-rwxr-xr-x | summain | 35 |
1 files changed, 31 insertions, 4 deletions
@@ -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() |