-
Notifications
You must be signed in to change notification settings - Fork 0
/
printtool.py
46 lines (43 loc) · 1.28 KB
/
printtool.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
#!python
#==========================================================#
# title: printtool.py
# decscription: smarter and more colorful printing
# author: sjdillon
# date: 20150307
# usage:
# from printtool import print_message,
# print_message ('script has been run, but source has changed','EXIT')
# python_version :2.7.7
#==========================================================#
from colorama import Fore, Back, Style
import colorama
colorama.init()
def print_message(msg,status):
if status in ('OK','PASS') :
color=Fore.YELLOW
elif status in ('RUNNING','FILE'):
color=Fore.GREEN
elif status in ('ERROR','QUIT'):
color=Fore.RED
else:
color=Fore.WHITE
color=color+Style.BRIGHT
msg='%s[%s]%s: %s' % (color,status,Style.RESET_ALL,msg)
print msg
def print_table(rows, headers=None):
"""print a table from rows"""
"""if no headers passed, will use db column names"""
table=[]
for row in rows:
if headers==None:
headers=list(row._fields)
table_row=[]
for cell in xrange(len(row)):
table_row.append(row[cell])
table.append(table_row)
print tabulate(table, headers, tablefmt="grid")
def test_messages():
print_message('testing yellow', 'OK')
print_message('testing red', 'ERROR')
print_message('testing green', 'RUNNING')
print_message('testing white', 'NONE')