From 68f67f9b71e1bbc9647d350cb6b62d72ebfbeab9 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 8 Aug 2011 14:50:00 +0100 Subject: Move output formatting out from FilesystemObject class. --- summain | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 5 deletions(-) (limited to 'summain') diff --git a/summain b/summain index 1525570..717d398 100755 --- a/summain +++ b/summain @@ -22,6 +22,30 @@ import sys import summainlib +class OutputFormat(object): + + def __init__(self, output): + self.output = output + + def close(self): + pass + + def write_object(self, name, o, checksums): + raise NotImplemented() + + +class Rfc822(OutputFormat): + + def write_object(self, name, o, checksums): + keys = (['Mtime', 'Mode', 'Ino', 'Dev', 'Nlink', 'Size', + 'Uid', 'Username', 'Gid', 'Group', 'Target'] + + 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 Summain(cliapp.Application): def add_settings(self): @@ -63,18 +87,43 @@ class Summain(cliapp.Application): pn = summainlib.SamePath() checksums = [x.upper() for x in self.settings['checksum'] or ['SHA1']] - o = summainlib.FilesystemObject('.', nn, pn, exclude, checksums) + o = summainlib.FilesystemObject('.', nn, pn, exclude) for checksum in checksums: try: o[checksum] except KeyError: raise cliapp.AppException('Unknown checksum %s' % checksum) + + fmt = Rfc822(self.output) for root in args: for filename in self.files(root): - o = summainlib.FilesystemObject(filename, nn, pn, exclude, - checksums) - self.output.write(o.format(root if relative else None)) - self.output.write('\n') + o = summainlib.FilesystemObject(filename, nn, pn, exclude) + if relative: + name = self.relative_path(root) + else: + name = o['Name'] + fmt.write_object(name, o, checksums) + fmt.close() + + def relative_path(self, root): + '''Return a path that is relative to root, if possible. + + If pathname does not start with root, then return it + unmodified. + + ''' + + if root.endswith(os.sep): + root2 = root + else: + root2 = root + os.sep + pathname = self['Name'] + if pathname.startswith(root2): + return pathname[len(root2):] + elif pathname == root and self._isdir(): + return '.' + else: + return pathname Summain(version=summainlib.__version__).run() -- cgit v1.2.1