from __future__ import print_function
from __future__ import unicode_literals
import os
from colorama import Fore
from tabulate import tabulate
from mylinux.libs.decorator import ClassProperty
'''
Standard usage would be
error = []
error.append(['FAIL','Json load','message explaining error')
'''
[docs]class Tli(object):
__errorTitle = Fore.RED + 'PROBLEM : {}' + Fore.RESET + '\n'
__reportTitle = 'REPORT : {}\n'
__allStructure = '\n>>> {}{}'
__redStrings = ['FAIL', 'ERROR', 'err']
__greenStrings = ['OK', 'SUCCESS', 'PASS', 'ok']
__yellowStrings = ['WARNING', 'None', 'none']
__errorHeaders = ['Status', 'Error', 'Message']
__reportHeaders = ['Status', 'Package', 'Message']
@ClassProperty
@classmethod
def errorHeaders(cls):
return cls.__errorHeaders
@ClassProperty
@classmethod
def reportHeaders(cls):
return cls.__reportHeaders
@classmethod
def __getErrorString(cls, title, array, headers=None):
# Specijal coloring...
string = cls.__getFormatedString(
cls.__errorTitle.format(title),
array,
headers=cls.__errorHeaders if not headers else headers
)
return string
@classmethod
[docs] def raiseError(cls,errorType,title,array,headers=None):
raise errorType(cls.__getErrorString(title,array,headers))
@classmethod
[docs] def printReport(cls, title, array, headers=None):
os.system('clear')
# Specijal coloring...
string = cls.__getFormatedString(
cls.__reportTitle.format(title),
array,
headers=cls.__reportHeaders if not headers else headers
)
print(string)
@classmethod
def __colorSpecialStrings(cls, array):
for i, innerArray in enumerate(array):
for j, element in enumerate(innerArray):
if element in cls.__greenStrings:
array[i][j] = Fore.GREEN + element + Fore.RESET
elif element in cls.__redStrings:
array[i][j] = Fore.RED + element + Fore.RESET
elif element in cls.__yellowStrings:
array[i][j] = Fore.YELLOW + element + Fore.RESET
return array
@classmethod
def __getFormatedString(cls, title, array, headers):
array = [array] if not isinstance(array[0], list) else array
array = cls.__colorSpecialStrings(array)
table = tabulate(array, headers, tablefmt="psql")
return cls.__allStructure.format(title, table).encode('utf-8')