-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
72 lines (66 loc) · 2.11 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
@author: [email protected]
"""
import time
import sys
import os
if os.name == 'nt':
DEFAULT, BLACK, BLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, GREEN, CYAN, RED, LIGHTMAGENTA, YELLOW, WHITE = range(17)
try:
from ctypes import *
from win32con import *
except:
pass
CloseHandle = windll.kernel32.CloseHandle
GetStdHandle = windll.kernel32.GetStdHandle
GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
STD_OUTPUT_HANDLE = -11
class COORD(Structure):
_fields_ = [
('X', c_short),
('Y', c_short),
]
class SMALL_RECT(Structure):
_fields_ = [
('Left', c_short),
('Top', c_short),
('Right', c_short),
('Bottom', c_short),
]
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
_fields_ = [
('dwSize', COORD),
('dwCursorPosition', COORD),
('wAttributes', c_uint),
('srWindow', SMALL_RECT),
('dwMaximumWindowSize', COORD),
]
else:
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
def println(msg, color):
dt = time.strftime('%Y-%m-%d %H:%M:%S')
msg = '[%s] %s' % (dt, msg)
if os.name == 'nt':
# windows behavior
hconsole = GetStdHandle(STD_OUTPUT_HANDLE)
cmd_info = CONSOLE_SCREEN_BUFFER_INFO()
GetConsoleScreenBufferInfo(hconsole, byref(cmd_info))
old_color = cmd_info.wAttributes
fore = color
if fore: fore = fore - 1
else: fore = old_color & 0x0F
back = 1
if back: back = (back - 1) << 4
else: back = old_color & 0xF0
SetConsoleTextAttribute(hconsole, fore + back)
print msg
SetConsoleTextAttribute(hconsole, old_color)
else:
# linux / osx behavior
print '\x1b[1;3%sm%s\x1b[0m' % (color, msg)
sys.stdout.flush()
def md5(self, key):
return hashlib.md5(key).hexdigest()