From 676cd2bf6576967c8b36419b62c3053637fd1fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Mart=C3=ADn?= Date: Mon, 3 Jun 2013 16:50:38 +0200 Subject: [PATCH] [utils.miner] Added coloring utils --- utils/miner/utils.py | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/utils/miner/utils.py b/utils/miner/utils.py index 825b646..acdf96b 100644 --- a/utils/miner/utils.py +++ b/utils/miner/utils.py @@ -3,6 +3,8 @@ import re from conf import CLASS_BLACKLIST import random import string +import sys +import os def replace_classnames(code): char_set = string.ascii_lowercase @@ -35,3 +37,85 @@ def sanitize(string): sane = regex.sub(r'\1"\2"', sane) sane = replace_classnames(sane) return sane + +# Functions +class Colors: + END = '\033[0m' + + def __init__(self, c='ter,'): + if c == 'custom': + self.custom() + else: + self.term() + + def custom(self): + self.WHITE = '\033[89m' + self.BLACK = '\033[90m' + self.RED = '\033[91m' + self.GREEN = '\033[92m' + self.YELLOW = '\033[93m' + self.BLUE = '\033[94m' + self.PURPLE = '\033[95m' + self.CYAN = '\033[96m' + + def term(self): + self.WHITE = '\033[29m' + self.BLACK = '\033[30m' + self.RED = '\033[31m' + self.GREEN = '\033[32m' + self.YELLOW = '\033[33m' + self.BLUE = '\033[34m' + self.PURPLE = '\033[35m' + self.CYAN = '\033[36m' + +colors = Colors('custom') + + +def echo(string, end='\r\n', color=None): + if color: + sys.stdout.write("%s%s%s" % ( + color, + string, + colors.END + )) + else: + sys.stdout.write(string) + if end: + sys.stdout.write("\n") + sys.stdout.flush() + + +# Shortcurts +def check_status(status=None, words=['done', 'failed']): + if status is not None: + if status == 0: + echo(words[0], color=colors.GREEN) + else: + echo(words[1], color=colors.RED) + pass + #exit(-1) + + +def exists(path): + return os.path.exists(path) + + +def title(string): + print("") + echo("[==] %s" % string, color=colors.PURPLE) + + +def info(string): + echo("[ i] %s" % string, color=colors.BLUE) + + +def error(string): + echo("[ E] %s" % string, color=colors.RED) + + +def success(string): + echo("[OK] %s" % string, color=colors.GREEN) + + +def sub(string, end='', color=None): + echo(" %s " % string, end=end, color=color)