diff options
author | Lars Wirzenius <liw@liw.fi> | 2011-08-08 15:12:11 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2011-08-08 15:12:11 +0100 |
commit | 870dd469930dbe88aefd05ced58e226b66c31e00 (patch) | |
tree | 82dec06249c4df317a9c8d46c15edc63a80f62cc /summain | |
parent | 02040cfb792062e66e9abc829a43b9d470b1a6d3 (diff) | |
download | summain-870dd469930dbe88aefd05ced58e226b66c31e00.tar.gz |
Refactor: move checksums to object creation, not every write_object call.
Diffstat (limited to 'summain')
-rwxr-xr-x | summain | 30 |
1 files changed, 14 insertions, 16 deletions
@@ -29,8 +29,9 @@ class OutputFormat(object): keys = ['Mtime', 'Mode', 'Ino', 'Dev', 'Nlink', 'Size', 'Uid', 'Username', 'Gid', 'Group', 'Target'] - def __init__(self, output): + def __init__(self, output, checksums): self.output = output + self.checksums = checksums def close(self): pass @@ -41,8 +42,8 @@ class OutputFormat(object): class Rfc822(OutputFormat): - def write_object(self, name, o, checksums): - keys = self.keys + checksums + def write_object(self, name, o): + keys = self.keys + self.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 != '') @@ -51,13 +52,13 @@ class Rfc822(OutputFormat): class CSV(OutputFormat): - def __init__(self, output): - OutputFormat.__init__(self, output) + def __init__(self, output, checksums): + OutputFormat.__init__(self, output, checksums) self.writer = csv.writer(output) self.wrote_headings = False - def write_object(self, name, o, checksums): - keys = self.keys + checksums + def write_object(self, name, o): + keys = self.keys + self.checksums if not self.wrote_headings: self.writer.writerow(keys) @@ -69,11 +70,8 @@ class CSV(OutputFormat): class Json(OutputFormat): - def __init__(self, output): - OutputFormat.__init__(self, output) - - def write_object(self, name, o, checksums): - keys = self.keys + checksums + def write_object(self, name, o): + keys = self.keys + self.checksums values = { 'Name': name } for k in keys: @@ -132,7 +130,7 @@ class Summain(cliapp.Application): except KeyError: raise cliapp.AppException('Unknown checksum %s' % checksum) - fmt = self.new_formatter() + fmt = self.new_formatter(checksums) for root in args: for filename in self.files(root): o = summainlib.FilesystemObject(filename, nn, pn, exclude) @@ -140,7 +138,7 @@ class Summain(cliapp.Application): name = self.relative_path(root, o) else: name = o['Name'] - fmt.write_object(name, o, checksums) + fmt.write_object(name, o) fmt.close() def relative_path(self, root, o): @@ -163,13 +161,13 @@ class Summain(cliapp.Application): else: return pathname - def new_formatter(self): + def new_formatter(self, checksums): table = { 'rfc822': Rfc822, 'csv': CSV, 'json': Json, } - return table[self.settings['output-format']](self.output) + return table[self.settings['output-format']](self.output, checksums) Summain(version=summainlib.__version__).run() |