Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e83d8f6

Browse files
committed
Updating colorama (Issue #1784)
1 parent 0245ce6 commit e83d8f6

8 files changed

Lines changed: 381 additions & 160 deletions

File tree

doc/THIRD-PARTY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This file lists bundled packages and their associated licensing terms.
1212
Copyright (C) 2005, Zope Corporation.
1313
Copyright (C) 1998-2000, Gisle Aas.
1414
* The Colorama library located under thirdparty/colorama/.
15-
Copyright (C) 2010, Jonathan Hartley.
15+
Copyright (C) 2013, Jonathan Hartley.
1616
* The Fcrypt library located under thirdparty/fcrypt/.
1717
Copyright (C) 2000, 2001, 2004 Carey Evans.
1818
* The Odict library located under thirdparty/odict/.

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from lib.core.revision import getRevisionNumber
2121

2222
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
23-
VERSION = "1.0.3.9"
23+
VERSION = "1.0.3.10"
2424
REVISION = getRevisionNumber()
2525
STABLE = VERSION.count('.') <= 2
2626
VERSION_STRING = "sqlmap/%s#%s" % (VERSION, "stable" if STABLE else "dev")

thirdparty/colorama/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
2+
from .initialise import init, deinit, reinit, colorama_text
3+
from .ansi import Fore, Back, Style, Cursor
4+
from .ansitowin32 import AnsiToWin32
5+
6+
__version__ = '0.3.7'
7+

thirdparty/colorama/ansi.py

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,102 @@
1+
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
12
'''
23
This module generates ANSI character codes to printing colors to terminals.
34
See: http://en.wikipedia.org/wiki/ANSI_escape_code
45
'''
56

67
CSI = '\033['
8+
OSC = '\033]'
9+
BEL = '\007'
10+
711

812
def code_to_chars(code):
913
return CSI + str(code) + 'm'
1014

15+
def set_title(title):
16+
return OSC + '2;' + title + BEL
17+
18+
def clear_screen(mode=2):
19+
return CSI + str(mode) + 'J'
20+
21+
def clear_line(mode=2):
22+
return CSI + str(mode) + 'K'
23+
24+
1125
class AnsiCodes(object):
12-
def __init__(self, codes):
13-
for name in dir(codes):
26+
def __init__(self):
27+
# the subclasses declare class attributes which are numbers.
28+
# Upon instantiation we define instance attributes, which are the same
29+
# as the class attributes but wrapped with the ANSI escape sequence
30+
for name in dir(self):
1431
if not name.startswith('_'):
15-
value = getattr(codes, name)
32+
value = getattr(self, name)
1633
setattr(self, name, code_to_chars(value))
1734

18-
class AnsiFore:
19-
BLACK = 30
20-
RED = 31
21-
GREEN = 32
22-
YELLOW = 33
23-
BLUE = 34
24-
MAGENTA = 35
25-
CYAN = 36
26-
WHITE = 37
27-
RESET = 39
28-
29-
class AnsiBack:
30-
BLACK = 40
31-
RED = 41
32-
GREEN = 42
33-
YELLOW = 43
34-
BLUE = 44
35-
MAGENTA = 45
36-
CYAN = 46
37-
WHITE = 47
38-
RESET = 49
39-
40-
class AnsiStyle:
35+
36+
class AnsiCursor(object):
37+
def UP(self, n=1):
38+
return CSI + str(n) + 'A'
39+
def DOWN(self, n=1):
40+
return CSI + str(n) + 'B'
41+
def FORWARD(self, n=1):
42+
return CSI + str(n) + 'C'
43+
def BACK(self, n=1):
44+
return CSI + str(n) + 'D'
45+
def POS(self, x=1, y=1):
46+
return CSI + str(y) + ';' + str(x) + 'H'
47+
48+
49+
class AnsiFore(AnsiCodes):
50+
BLACK = 30
51+
RED = 31
52+
GREEN = 32
53+
YELLOW = 33
54+
BLUE = 34
55+
MAGENTA = 35
56+
CYAN = 36
57+
WHITE = 37
58+
RESET = 39
59+
60+
# These are fairly well supported, but not part of the standard.
61+
LIGHTBLACK_EX = 90
62+
LIGHTRED_EX = 91
63+
LIGHTGREEN_EX = 92
64+
LIGHTYELLOW_EX = 93
65+
LIGHTBLUE_EX = 94
66+
LIGHTMAGENTA_EX = 95
67+
LIGHTCYAN_EX = 96
68+
LIGHTWHITE_EX = 97
69+
70+
71+
class AnsiBack(AnsiCodes):
72+
BLACK = 40
73+
RED = 41
74+
GREEN = 42
75+
YELLOW = 43
76+
BLUE = 44
77+
MAGENTA = 45
78+
CYAN = 46
79+
WHITE = 47
80+
RESET = 49
81+
82+
# These are fairly well supported, but not part of the standard.
83+
LIGHTBLACK_EX = 100
84+
LIGHTRED_EX = 101
85+
LIGHTGREEN_EX = 102
86+
LIGHTYELLOW_EX = 103
87+
LIGHTBLUE_EX = 104
88+
LIGHTMAGENTA_EX = 105
89+
LIGHTCYAN_EX = 106
90+
LIGHTWHITE_EX = 107
91+
92+
93+
class AnsiStyle(AnsiCodes):
4194
BRIGHT = 1
4295
DIM = 2
4396
NORMAL = 22
4497
RESET_ALL = 0
4598

46-
Fore = AnsiCodes( AnsiFore )
47-
Back = AnsiCodes( AnsiBack )
48-
Style = AnsiCodes( AnsiStyle )
49-
99+
Fore = AnsiFore()
100+
Back = AnsiBack()
101+
Style = AnsiStyle()
102+
Cursor = AnsiCursor()

thirdparty/colorama/ansitowin32.py

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
1+
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
22
import re
33
import sys
4+
import os
45

56
from .ansi import AnsiFore, AnsiBack, AnsiStyle, Style
67
from .winterm import WinTerm, WinColor, WinStyle
7-
from .win32 import windll
8+
from .win32 import windll, winapi_test
9+
810

911
winterm = None
1012
if windll is not None:
1113
winterm = WinTerm()
1214

1315

16+
def is_stream_closed(stream):
17+
return not hasattr(stream, 'closed') or stream.closed
18+
19+
1420
def is_a_tty(stream):
1521
return hasattr(stream, 'isatty') and stream.isatty()
1622

@@ -40,7 +46,8 @@ class AnsiToWin32(object):
4046
sequences from the text, and if outputting to a tty, will convert them into
4147
win32 function calls.
4248
'''
43-
ANSI_RE = re.compile('\033\[((?:\d|;)*)([a-zA-Z])')
49+
ANSI_CSI_RE = re.compile('\001?\033\[((?:\d|;)*)([a-zA-Z])\002?') # Control Sequence Introducer
50+
ANSI_OSC_RE = re.compile('\001?\033\]((?:.|;)*?)(\x07)\002?') # Operating System Command
4451

4552
def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
4653
# The wrapped stream (normally sys.stdout or sys.stderr)
@@ -52,16 +59,21 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
5259
# create the proxy wrapping our output stream
5360
self.stream = StreamWrapper(wrapped, self)
5461

55-
on_windows = sys.platform.startswith('win')
62+
on_windows = os.name == 'nt'
63+
# We test if the WinAPI works, because even if we are on Windows
64+
# we may be using a terminal that doesn't support the WinAPI
65+
# (e.g. Cygwin Terminal). In this case it's up to the terminal
66+
# to support the ANSI codes.
67+
conversion_supported = on_windows and winapi_test()
5668

5769
# should we strip ANSI sequences from our output?
5870
if strip is None:
59-
strip = on_windows
71+
strip = conversion_supported or (not is_stream_closed(wrapped) and not is_a_tty(wrapped))
6072
self.strip = strip
6173

6274
# should we should convert ANSI sequences into win32 calls?
6375
if convert is None:
64-
convert = on_windows and is_a_tty(wrapped)
76+
convert = conversion_supported and not is_stream_closed(wrapped) and is_a_tty(wrapped)
6577
self.convert = convert
6678

6779
# dict of ansi codes to win32 functions and parameters
@@ -70,7 +82,6 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
7082
# are we wrapping stderr?
7183
self.on_stderr = self.wrapped is sys.stderr
7284

73-
7485
def should_wrap(self):
7586
'''
7687
True if this class is actually needed. If false, then the output
@@ -81,7 +92,6 @@ def should_wrap(self):
8192
'''
8293
return self.convert or self.strip or self.autoreset
8394

84-
8595
def get_win32_calls(self):
8696
if self.convert and winterm:
8797
return {
@@ -98,6 +108,14 @@ def get_win32_calls(self):
98108
AnsiFore.CYAN: (winterm.fore, WinColor.CYAN),
99109
AnsiFore.WHITE: (winterm.fore, WinColor.GREY),
100110
AnsiFore.RESET: (winterm.fore, ),
111+
AnsiFore.LIGHTBLACK_EX: (winterm.fore, WinColor.BLACK, True),
112+
AnsiFore.LIGHTRED_EX: (winterm.fore, WinColor.RED, True),
113+
AnsiFore.LIGHTGREEN_EX: (winterm.fore, WinColor.GREEN, True),
114+
AnsiFore.LIGHTYELLOW_EX: (winterm.fore, WinColor.YELLOW, True),
115+
AnsiFore.LIGHTBLUE_EX: (winterm.fore, WinColor.BLUE, True),
116+
AnsiFore.LIGHTMAGENTA_EX: (winterm.fore, WinColor.MAGENTA, True),
117+
AnsiFore.LIGHTCYAN_EX: (winterm.fore, WinColor.CYAN, True),
118+
AnsiFore.LIGHTWHITE_EX: (winterm.fore, WinColor.GREY, True),
101119
AnsiBack.BLACK: (winterm.back, WinColor.BLACK),
102120
AnsiBack.RED: (winterm.back, WinColor.RED),
103121
AnsiBack.GREEN: (winterm.back, WinColor.GREEN),
@@ -107,8 +125,16 @@ def get_win32_calls(self):
107125
AnsiBack.CYAN: (winterm.back, WinColor.CYAN),
108126
AnsiBack.WHITE: (winterm.back, WinColor.GREY),
109127
AnsiBack.RESET: (winterm.back, ),
128+
AnsiBack.LIGHTBLACK_EX: (winterm.back, WinColor.BLACK, True),
129+
AnsiBack.LIGHTRED_EX: (winterm.back, WinColor.RED, True),
130+
AnsiBack.LIGHTGREEN_EX: (winterm.back, WinColor.GREEN, True),
131+
AnsiBack.LIGHTYELLOW_EX: (winterm.back, WinColor.YELLOW, True),
132+
AnsiBack.LIGHTBLUE_EX: (winterm.back, WinColor.BLUE, True),
133+
AnsiBack.LIGHTMAGENTA_EX: (winterm.back, WinColor.MAGENTA, True),
134+
AnsiBack.LIGHTCYAN_EX: (winterm.back, WinColor.CYAN, True),
135+
AnsiBack.LIGHTWHITE_EX: (winterm.back, WinColor.GREY, True),
110136
}
111-
137+
return dict()
112138

113139
def write(self, text):
114140
if self.strip or self.convert:
@@ -123,7 +149,7 @@ def write(self, text):
123149
def reset_all(self):
124150
if self.convert:
125151
self.call_win32('m', (0,))
126-
elif is_a_tty(self.wrapped):
152+
elif not self.strip and not is_stream_closed(self.wrapped):
127153
self.wrapped.write(Style.RESET_ALL)
128154

129155

@@ -134,7 +160,8 @@ def write_and_convert(self, text):
134160
calls.
135161
'''
136162
cursor = 0
137-
for match in self.ANSI_RE.finditer(text):
163+
text = self.convert_osc(text)
164+
for match in self.ANSI_CSI_RE.finditer(text):
138165
start, end = match.span()
139166
self.write_plain_text(text, cursor, start)
140167
self.convert_ansi(*match.groups())
@@ -150,21 +177,29 @@ def write_plain_text(self, text, start, end):
150177

151178
def convert_ansi(self, paramstring, command):
152179
if self.convert:
153-
params = self.extract_params(paramstring)
180+
params = self.extract_params(command, paramstring)
154181
self.call_win32(command, params)
155182

156183

157-
def extract_params(self, paramstring):
158-
def split(paramstring):
159-
for p in paramstring.split(';'):
160-
if p != '':
161-
yield int(p)
162-
return tuple(split(paramstring))
184+
def extract_params(self, command, paramstring):
185+
if command in 'Hf':
186+
params = tuple(int(p) if len(p) != 0 else 1 for p in paramstring.split(';'))
187+
while len(params) < 2:
188+
# defaults:
189+
params = params + (1,)
190+
else:
191+
params = tuple(int(p) for p in paramstring.split(';') if len(p) != 0)
192+
if len(params) == 0:
193+
# defaults:
194+
if command in 'JKm':
195+
params = (0,)
196+
elif command in 'ABCD':
197+
params = (1,)
198+
199+
return params
163200

164201

165202
def call_win32(self, command, params):
166-
if params == []:
167-
params = [0]
168203
if command == 'm':
169204
for param in params:
170205
if param in self.win32_calls:
@@ -173,17 +208,29 @@ def call_win32(self, command, params):
173208
args = func_args[1:]
174209
kwargs = dict(on_stderr=self.on_stderr)
175210
func(*args, **kwargs)
176-
elif command in ('H', 'f'): # set cursor position
177-
func = winterm.set_cursor_position
178-
func(params, on_stderr=self.on_stderr)
179-
elif command in ('J'):
180-
func = winterm.erase_data
181-
func(params, on_stderr=self.on_stderr)
182-
elif command == 'A':
183-
if params == () or params == None:
184-
num_rows = 1
185-
else:
186-
num_rows = params[0]
187-
func = winterm.cursor_up
188-
func(num_rows, on_stderr=self.on_stderr)
189-
211+
elif command in 'J':
212+
winterm.erase_screen(params[0], on_stderr=self.on_stderr)
213+
elif command in 'K':
214+
winterm.erase_line(params[0], on_stderr=self.on_stderr)
215+
elif command in 'Hf': # cursor position - absolute
216+
winterm.set_cursor_position(params, on_stderr=self.on_stderr)
217+
elif command in 'ABCD': # cursor position - relative
218+
n = params[0]
219+
# A - up, B - down, C - forward, D - back
220+
x, y = {'A': (0, -n), 'B': (0, n), 'C': (n, 0), 'D': (-n, 0)}[command]
221+
winterm.cursor_adjust(x, y, on_stderr=self.on_stderr)
222+
223+
224+
def convert_osc(self, text):
225+
for match in self.ANSI_OSC_RE.finditer(text):
226+
start, end = match.span()
227+
text = text[:start] + text[end:]
228+
paramstring, command = match.groups()
229+
if command in '\x07': # \x07 = BEL
230+
params = paramstring.split(";")
231+
# 0 - change title and icon (we will only change title)
232+
# 1 - change icon (we don't support this)
233+
# 2 - change title
234+
if params[0] in '02':
235+
winterm.set_title(params[1])
236+
return text

0 commit comments

Comments
 (0)