1#!/usr/bin/env python3 2########################################################################## 3# 4# Copyright 2008 VMware, Inc. 5# All Rights Reserved. 6# 7# Permission is hereby granted, free of charge, to any person obtaining a 8# copy of this software and associated documentation files (the 9# "Software"), to deal in the Software without restriction, including 10# without limitation the rights to use, copy, modify, merge, publish, 11# distribute, sub license, and/or sell copies of the Software, and to 12# permit persons to whom the Software is furnished to do so, subject to 13# the following conditions: 14# 15# The above copyright notice and this permission notice (including the 16# next paragraph) shall be included in all copies or substantial portions 17# of the Software. 18# 19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22# IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 23# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26# 27########################################################################## 28 29 30import sys 31 32 33class Formatter: 34 '''Plain formatter''' 35 36 def __init__(self, stream): 37 self.stream = stream 38 39 def text(self, text): 40 self.stream.write(text) 41 42 def newline(self): 43 self.text('\n') 44 45 def function(self, name): 46 self.text(name) 47 48 def variable(self, name): 49 self.text(name) 50 51 def literal(self, value): 52 self.text(str(value)) 53 54 def address(self, addr): 55 self.text(str(addr)) 56 57 58class AnsiFormatter(Formatter): 59 '''Formatter for plain-text files which outputs ANSI escape codes. See 60 http://en.wikipedia.org/wiki/ANSI_escape_code for more information 61 concerning ANSI escape codes. 62 ''' 63 64 _csi = '\33[' 65 66 _normal = '0m' 67 _bold = '1m' 68 _italic = '3m' # Not widely supported 69 _red = '31m' 70 _green = '32m' 71 _blue = '34m' 72 73 def _escape(self, code): 74 self.text(self._csi + code) 75 76 def function(self, name): 77 self._escape(self._bold) 78 Formatter.function(self, name) 79 self._escape(self._normal) 80 81 def variable(self, name): 82 Formatter.variable(self, name) 83 84 def literal(self, value): 85 self._escape(self._blue) 86 Formatter.literal(self, value) 87 self._escape(self._normal) 88 89 def address(self, value): 90 self._escape(self._green) 91 Formatter.address(self, value) 92 self._escape(self._normal) 93 94 95class WindowsConsoleFormatter(Formatter): 96 '''Formatter for the Windows Console. See 97 http://code.activestate.com/recipes/496901/ for more information. 98 ''' 99 100 STD_INPUT_HANDLE = -10 101 STD_OUTPUT_HANDLE = -11 102 STD_ERROR_HANDLE = -12 103 104 FOREGROUND_BLUE = 0x01 105 FOREGROUND_GREEN = 0x02 106 FOREGROUND_RED = 0x04 107 FOREGROUND_INTENSITY = 0x08 108 BACKGROUND_BLUE = 0x10 109 BACKGROUND_GREEN = 0x20 110 BACKGROUND_RED = 0x40 111 BACKGROUND_INTENSITY = 0x80 112 113 _normal = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED 114 _bold = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY 115 _italic = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED 116 _red = FOREGROUND_RED | FOREGROUND_INTENSITY 117 _green = FOREGROUND_GREEN | FOREGROUND_INTENSITY 118 _blue = FOREGROUND_BLUE | FOREGROUND_INTENSITY 119 120 def __init__(self, stream): 121 Formatter.__init__(self, stream) 122 123 if stream is sys.stdin: 124 nStdHandle = self.STD_INPUT_HANDLE 125 elif stream is sys.stdout: 126 nStdHandle = self.STD_OUTPUT_HANDLE 127 elif stream is sys.stderr: 128 nStdHandle = self.STD_ERROR_HANDLE 129 else: 130 nStdHandle = None 131 132 if nStdHandle: 133 import ctypes 134 self.handle = ctypes.windll.kernel32.GetStdHandle(nStdHandle) 135 else: 136 self.handle = None 137 138 def _attribute(self, attr): 139 if self.handle: 140 import ctypes 141 ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, attr) 142 143 def function(self, name): 144 self._attribute(self._bold) 145 Formatter.function(self, name) 146 self._attribute(self._normal) 147 148 def variable(self, name): 149 self._attribute(self._italic) 150 Formatter.variable(self, name) 151 self._attribute(self._normal) 152 153 def literal(self, value): 154 self._attribute(self._blue) 155 Formatter.literal(self, value) 156 self._attribute(self._normal) 157 158 def address(self, value): 159 self._attribute(self._green) 160 Formatter.address(self, value) 161 self._attribute(self._normal) 162 163 164def DefaultFormatter(stream): 165 if sys.platform in ('linux2', 'linux', 'cygwin'): 166 return AnsiFormatter(stream) 167 elif sys.platform in ('win32', ): 168 return WindowsConsoleFormatter(stream) 169 else: 170 return Formatter(stream) 171 172