From d8969fe15c8411ac163b6d84bf6a7d9dc316599e Mon Sep 17 00:00:00 2001 From: Philippe Lagadec Date: Sun, 7 Feb 2016 16:20:56 +0100 Subject: [PATCH] olemeta: improved display - table and colors --- oletools/olemeta.py | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/oletools/olemeta.py b/oletools/olemeta.py index 1c018b0..f29097a 100755 --- a/oletools/olemeta.py +++ b/oletools/olemeta.py @@ -56,8 +56,9 @@ __version__ = '0.03' #=== IMPORTS ================================================================= -import sys +import sys, codecs import thirdparty.olefile as olefile +from thirdparty.tablestream import tablestream #=== MAIN ================================================================= @@ -70,19 +71,42 @@ except IndexError: # parse and display metadata: meta = ole.get_metadata() -print('Properties from SummaryInformation stream:') +# console output with UTF8 encoding: +console_utf8 = codecs.getwriter('utf8')(sys.stdout) + +# TODO: move similar code to a function + +print('Properties from the SummaryInformation stream:') +t = tablestream.TableStream([21, 30], header_row=['Property', 'Value'], outfile=console_utf8) for prop in meta.SUMMARY_ATTRIBS: value = getattr(meta, prop) if value is not None: # TODO: pretty printing for strings, dates, numbers - print('- %s: %s' % (prop, value)) + # TODO: better unicode handling + # print('- %s: %s' % (prop, value)) + if isinstance(value, unicode): + # encode to UTF8, avoiding errors + value = value.encode('utf-8', errors='replace') + else: + value = str(value) + t.write_row([prop, value], colors=[None, 'yellow']) +t.close() print '' -print('Properties from DocumentSummaryInformation stream:') + +print('Properties from the DocumentSummaryInformation stream:') +t = tablestream.TableStream([21, 30], header_row=['Property', 'Value'], outfile=console_utf8) for prop in meta.DOCSUM_ATTRIBS: value = getattr(meta, prop) if value is not None: # TODO: pretty printing for strings, dates, numbers - print('- %s: %s' % (prop, value)) - + # TODO: better unicode handling + # print('- %s: %s' % (prop, value)) + if isinstance(value, unicode): + # encode to UTF8, avoiding errors + value = value.encode('utf-8', errors='replace') + else: + value = str(value) + t.write_row([prop, value], colors=[None, 'yellow']) +t.close() ole.close() -- libgit2 0.21.4