From 870dd469930dbe88aefd05ced58e226b66c31e00 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 8 Aug 2011 15:12:11 +0100 Subject: Refactor: move checksums to object creation, not every write_object call. --- summain | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'summain') diff --git a/summain b/summain index 9bc8b65..aa7f077 100755 --- a/summain +++ b/summain @@ -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() -- cgit v1.2.1