1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 22:59:01 +00:00
This commit is contained in:
deadc0de6
2021-04-29 20:40:42 +02:00
parent d3a9aea44f
commit 1da3ca6da1

View File

@@ -10,6 +10,7 @@ import inspect
class Logger: class Logger:
"""logging facility for dotdrop"""
RED = '\033[91m' RED = '\033[91m'
GREEN = '\033[92m' GREEN = '\033[92m'
@@ -25,67 +26,82 @@ class Logger:
self.debug = debug self.debug = debug
def log(self, string, end='\n', pre='', bold=False): def log(self, string, end='\n', pre='', bold=False):
cs = self._color(self.BLUE) """normal log"""
ce = self._color(self.RESET) cstart = self._color(self.BLUE)
cend = self._color(self.RESET)
if bold: if bold:
bl = self._color(self.BOLD) bold = self._color(self.BOLD)
fmt = '{}{}{}{}{}{}{}'.format(pre, cs, bl, fmt = '{}{}{}{}{}'.format(pre, cstart, bold,
string, ce, string, cend)
end, ce) fmt += '{}{}'.format(end, cend)
else: else:
fmt = '{}{}{}{}{}'.format(pre, cs, string, end, ce) fmt = '{}{}{}{}{}'.format(pre, cstart, string, end, cend)
sys.stdout.write(fmt) sys.stdout.write(fmt)
def sub(self, string, end='\n'): def sub(self, string, end='\n'):
cs = self._color(self.BLUE) """sub log"""
ce = self._color(self.RESET) cstart = self._color(self.BLUE)
sys.stdout.write('\t{}->{} {}{}'.format(cs, ce, string, end)) cend = self._color(self.RESET)
sys.stdout.write('\t{}->{} {}{}'.format(cstart, cend, string, end))
def emph(self, string): def emph(self, string):
cs = self._color(self.EMPH) """emphasis log"""
ce = self._color(self.RESET) cstart = self._color(self.EMPH)
sys.stderr.write('{}{}{}'.format(cs, string, ce)) cend = self._color(self.RESET)
sys.stderr.write('{}{}{}'.format(cstart, string, cend))
def err(self, string, end='\n'): def err(self, string, end='\n'):
cs = self._color(self.RED) """error log"""
ce = self._color(self.RESET) cstart = self._color(self.RED)
cend = self._color(self.RESET)
msg = '{} {}'.format(string, end) msg = '{} {}'.format(string, end)
sys.stderr.write('{}[ERR] {}{}'.format(cs, msg, ce)) sys.stderr.write('{}[ERR] {}{}'.format(cstart, msg, cend))
def warn(self, string, end='\n'): def warn(self, string, end='\n'):
cs = self._color(self.YELLOW) """warning log"""
ce = self._color(self.RESET) cstart = self._color(self.YELLOW)
sys.stderr.write('{}[WARN] {} {}{}'.format(cs, string, end, ce)) cend = self._color(self.RESET)
sys.stderr.write('{}[WARN] {} {}{}'.format(cstart, string, end, cend))
def dbg(self, string, force=False): def dbg(self, string, force=False):
"""debug log"""
if not force and not self.debug: if not force and not self.debug:
return return
frame = inspect.stack()[1] frame = inspect.stack()[1]
mod = inspect.getmodule(frame[0]).__name__ mod = inspect.getmodule(frame[0]).__name__
func = inspect.stack()[1][3] func = inspect.stack()[1][3]
cs = self._color(self.MAGENTA) cstart = self._color(self.MAGENTA)
ce = self._color(self.RESET) cend = self._color(self.RESET)
cl = self._color(self.LMAGENTA) clight = self._color(self.LMAGENTA)
bl = self._color(self.BOLD) bold = self._color(self.BOLD)
line = '{}{}[DEBUG][{}.{}]{}{} {}{}\n' line = '{}{}[DEBUG][{}.{}]{}{} {}{}\n'
sys.stderr.write(line.format(bl, cl, mod, func, ce, cs, string, ce)) sys.stderr.write(line.format(bold, clight,
mod, func,
cend, cstart,
string, cend))
def dry(self, string, end='\n'): def dry(self, string, end='\n'):
cs = self._color(self.GREEN) """dry run log"""
ce = self._color(self.RESET) cstart = self._color(self.GREEN)
sys.stdout.write('{}[DRY] {} {}{}'.format(cs, string, end, ce)) cend = self._color(self.RESET)
sys.stdout.write('{}[DRY] {} {}{}'.format(cstart, string, end, cend))
def raw(self, string, end='\n'): @classmethod
def raw(cls, string, end='\n'):
"""raw log"""
sys.stdout.write('{}{}'.format(string, end)) sys.stdout.write('{}{}'.format(string, end))
def ask(self, query): def ask(self, query):
cs = self._color(self.BLUE) """ask user for confirmation"""
ce = self._color(self.RESET) cstart = self._color(self.BLUE)
q = '{}{}{}'.format(cs, query + ' [y/N] ? ', ce) cend = self._color(self.RESET)
r = input(q) query = '{}{}{}'.format(cstart, query + ' [y/N] ? ', cend)
return r == 'y' resp = input(query)
return resp == 'y'
def _color(self, col): @classmethod
def _color(cls, col):
"""is color supported"""
if not sys.stdout.isatty(): if not sys.stdout.isatty():
return '' return ''
return col return col