Commit d8969fe15c8411ac163b6d84bf6a7d9dc316599e

Authored by Philippe Lagadec
1 parent 9a143718

olemeta: improved display - table and colors

Showing 1 changed file with 30 additions and 6 deletions
oletools/olemeta.py
@@ -56,8 +56,9 @@ __version__ = '0.03' @@ -56,8 +56,9 @@ __version__ = '0.03'
56 56
57 #=== IMPORTS ================================================================= 57 #=== IMPORTS =================================================================
58 58
59 -import sys 59 +import sys, codecs
60 import thirdparty.olefile as olefile 60 import thirdparty.olefile as olefile
  61 +from thirdparty.tablestream import tablestream
61 62
62 63
63 #=== MAIN ================================================================= 64 #=== MAIN =================================================================
@@ -70,19 +71,42 @@ except IndexError: @@ -70,19 +71,42 @@ except IndexError:
70 # parse and display metadata: 71 # parse and display metadata:
71 meta = ole.get_metadata() 72 meta = ole.get_metadata()
72 73
73 -print('Properties from SummaryInformation stream:') 74 +# console output with UTF8 encoding:
  75 +console_utf8 = codecs.getwriter('utf8')(sys.stdout)
  76 +
  77 +# TODO: move similar code to a function
  78 +
  79 +print('Properties from the SummaryInformation stream:')
  80 +t = tablestream.TableStream([21, 30], header_row=['Property', 'Value'], outfile=console_utf8)
74 for prop in meta.SUMMARY_ATTRIBS: 81 for prop in meta.SUMMARY_ATTRIBS:
75 value = getattr(meta, prop) 82 value = getattr(meta, prop)
76 if value is not None: 83 if value is not None:
77 # TODO: pretty printing for strings, dates, numbers 84 # TODO: pretty printing for strings, dates, numbers
78 - print('- %s: %s' % (prop, value)) 85 + # TODO: better unicode handling
  86 + # print('- %s: %s' % (prop, value))
  87 + if isinstance(value, unicode):
  88 + # encode to UTF8, avoiding errors
  89 + value = value.encode('utf-8', errors='replace')
  90 + else:
  91 + value = str(value)
  92 + t.write_row([prop, value], colors=[None, 'yellow'])
  93 +t.close()
79 print '' 94 print ''
80 -print('Properties from DocumentSummaryInformation stream:') 95 +
  96 +print('Properties from the DocumentSummaryInformation stream:')
  97 +t = tablestream.TableStream([21, 30], header_row=['Property', 'Value'], outfile=console_utf8)
81 for prop in meta.DOCSUM_ATTRIBS: 98 for prop in meta.DOCSUM_ATTRIBS:
82 value = getattr(meta, prop) 99 value = getattr(meta, prop)
83 if value is not None: 100 if value is not None:
84 # TODO: pretty printing for strings, dates, numbers 101 # TODO: pretty printing for strings, dates, numbers
85 - print('- %s: %s' % (prop, value))  
86 - 102 + # TODO: better unicode handling
  103 + # print('- %s: %s' % (prop, value))
  104 + if isinstance(value, unicode):
  105 + # encode to UTF8, avoiding errors
  106 + value = value.encode('utf-8', errors='replace')
  107 + else:
  108 + value = str(value)
  109 + t.write_row([prop, value], colors=[None, 'yellow'])
  110 +t.close()
87 111
88 ole.close() 112 ole.close()