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