summaryrefslogtreecommitdiff
path: root/summain
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-08-08 15:12:11 +0100
committerLars Wirzenius <liw@liw.fi>2011-08-08 15:12:11 +0100
commit870dd469930dbe88aefd05ced58e226b66c31e00 (patch)
tree82dec06249c4df317a9c8d46c15edc63a80f62cc /summain
parent02040cfb792062e66e9abc829a43b9d470b1a6d3 (diff)
downloadsummain-870dd469930dbe88aefd05ced58e226b66c31e00.tar.gz
Refactor: move checksums to object creation, not every write_object call.
Diffstat (limited to 'summain')
-rwxr-xr-xsummain30
1 files changed, 14 insertions, 16 deletions
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()