Skip to content

Commit

Permalink
feature/Debugger output supports HTML colorized colors
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmitaboada committed Apr 8, 2024
1 parent f65692d commit 5c062c7
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.26] - 2024-04-08
### Feature
- Added support to Debugger for HTML colorized output

## [1.0.25] - 2023-04-01
### Changed
- Requirements updated
Expand Down
2 changes: 1 addition & 1 deletion codenerix_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.25"
__version__ = "1.0.26"

__authors__ = [
"Juanmi Taboada",
Expand Down
66 changes: 45 additions & 21 deletions codenerix_lib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,51 @@
__all__ = ["colors"]


colors = {}

colors["simplegrey"] = (0, 30)
colors["simplered"] = (0, 31)
colors["simplegreen"] = (0, 32)
colors["simpleyellow"] = (0, 33)
colors["simpleblue"] = (0, 34)
colors["simplepurple"] = (0, 35)
colors["simplecyan"] = (0, 36)
colors["simplewhite"] = (0, 37)

colors["grey"] = (1, 30)
colors["red"] = (1, 31)
colors["green"] = (1, 32)
colors["yellow"] = (1, 33)
colors["blue"] = (1, 34)
colors["purple"] = (1, 35)
colors["cyan"] = (1, 36)
colors["white"] = (1, 37)

colors["close"] = (1, 0)
# Colors
colors = {
# Simple
"simplegrey": (0, 30),
"simplered": (0, 31),
"simplegreen": (0, 32),
"simpleyellow": (0, 33),
"simpleblue": (0, 34),
"simplepurple": (0, 35),
"simplecyan": (0, 36),
"simplewhite": (0, 37),
# Bold
"grey": (1, 30),
"red": (1, 31),
"green": (1, 32),
"yellow": (1, 33),
"blue": (1, 34),
"purple": (1, 35),
"cyan": (1, 36),
"white": (1, 37),
# Close
"close": (1, 0),
}

# HTML Colors
html_colors = {
# Simple
"simplegrey": (0, (128, 128, 128)),
"simplered": (0, (255, 0, 0)),
"simplegreen": (0, (0, 255, 0)),
"simpleyellow": (0, (255, 255, 0)),
"simpleblue": (0, (0, 0, 255)),
"simplepurple": (0, (128, 0, 128)),
"simplecyan": (0, (0, 255, 255)),
"simplewhite": (0, (255, 255, 255)),
# Bold
"grey": (1, (169, 169, 169)),
"red": (1, (255, 69, 0)),
"green": (1, (0, 128, 0)),
"yellow": (1, (255, 255, 0)),
"blue": (1, (0, 0, 255)),
"purple": (1, (128, 0, 128)),
"cyan": (1, (0, 255, 255)),
"white": (1, (255, 255, 255)),
}


def colorize(msg, color=None):
Expand Down
142 changes: 122 additions & 20 deletions codenerix_lib/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, TextIO, Tuple, Union

from codenerix_lib.colors import colors
from codenerix_lib.colors import colors, html_colors

__version__ = "2023033000"

Expand Down Expand Up @@ -86,6 +86,8 @@ def set_debug(
self,
debug: Optional[Dict[str, Any]] = None,
):
self.html = None
self.html_bgcolor = None
if debug is None:
self.__autoconfig()
else:
Expand All @@ -109,6 +111,13 @@ def set_debug(
else: # pragma: no cover
raise OSError("Argument is not a dictionary")

def set_html_color(self, bgcolor=None):
self.html = True
self.html_bgcolor = bgcolor

def set_shell_color(self):
self.html = False

def set_origin(self, origin): # pragma: no cover
self.origin = origin

Expand All @@ -121,18 +130,51 @@ def set_name(self, name):
def get_name(self):
return self.__inname

def color(self, color):
# Colors$
def shell_color(self, color):
# Colors
if color in colors:
(darkbit, subcolor) = colors[color]
return "\033[%1d;%02dm" % (darkbit, subcolor)
return f"\033[{darkbit:1d};{subcolor:02d}m"
else:
if color:
self.debug(
f"\033[1;31mColor '{color}' unknown\033[1;00m\n",
)
return ""

def html_color(self, color):
# Colors
if color == "close":
return "</span>"
elif color in html_colors:
(bold, subcolor) = html_colors[color]
if not bold:
bolder = ""
else:
bolder = "; font-weight: bolder"
if not self.html_bgcolor:
bgcolor = ""
else:
bgcolor = f"; background-color:rgb{self.html_bgcolor}"
return f'<span style="color: rgb{subcolor}{bgcolor}{bolder}">'
else:
if color:
(bold, subcolor) = html_colors["red"]
self.debug(
"\033[1;31mColor '%s' unknown\033[1;00m\n" % (color),
f'<span style="color: rgb{subcolor}; font-weight: bolder">'
f"Color '{color}' unknown"
"</span>\n",
)
return ""

def color(self, color, html=None):
if html is None:
html = self.html
if not html:
return self.shell_color(color)
else:
return self.html_color(color)

def debug(
self,
msg=None,
Expand All @@ -143,6 +185,7 @@ def debug(
footer=None,
origin=False,
kind="",
html=None,
):
# If origin has been requested
if origin or getattr(self, "origin", False): # pragma: no cover
Expand All @@ -167,7 +210,7 @@ def debug(
header = head
if tail is None:
if footer is None:
tail = True
tail = "\n"
else:
tail = footer

Expand All @@ -186,10 +229,10 @@ def debug(
if name not in ["deepness", "tabular"]:
# Get color
if name == "screen" or name[-1] == "*":
color_ini = self.color(color)
color_end = self.color("close")
color_ini = self.color(color, html)
color_end = self.color("close", html)
else:
color_ini = self.color(None)
color_ini = self.color(None, html)
color_end = color_ini

# Get file output handler and indebug list
Expand Down Expand Up @@ -235,7 +278,7 @@ def debug(
message += str(msg.encode("ascii", "ignore"))
message += color_end
if tail:
message += "\n"
message += tail

# Print it on the buffer handler
if msg:
Expand All @@ -255,7 +298,14 @@ def debug(
# Say to the caller we shouldn't output anything
return False

def primary(self, msg, header=True, tail=True, show_line_info=False):
def primary(
self,
msg,
header=True,
tail=True,
show_line_info=False,
html=None,
):
if show_line_info: # pragma: no cover
line = currentframe().f_back.f_lineno
filename = currentframe().f_back.f_code.co_filename.replace(
Expand All @@ -281,9 +331,17 @@ def primary(self, msg, header=True, tail=True, show_line_info=False):
filename,
header,
tail,
html,
)

def secondary(self, msg, header=True, tail=True, show_line_info=False):
def secondary(
self,
msg,
header=True,
tail=True,
show_line_info=False,
html=None,
):
if show_line_info: # pragma: no cover
line = currentframe().f_back.f_lineno
filename = currentframe().f_back.f_code.co_filename.replace(
Expand All @@ -309,9 +367,17 @@ def secondary(self, msg, header=True, tail=True, show_line_info=False):
filename,
header,
tail,
html,
)

def success(self, msg, header=True, tail=True, show_line_info=False):
def success(
self,
msg,
header=True,
tail=True,
show_line_info=False,
html=None,
):
if show_line_info: # pragma: no cover
line = currentframe().f_back.f_lineno
filename = currentframe().f_back.f_code.co_filename.replace(
Expand All @@ -337,9 +403,17 @@ def success(self, msg, header=True, tail=True, show_line_info=False):
filename,
header,
tail,
html,
)

def danger(self, msg, header=True, tail=True, show_line_info=False):
def danger(
self,
msg,
header=True,
tail=True,
show_line_info=False,
html=None,
):
if show_line_info: # pragma: no cover
line = currentframe().f_back.f_lineno
filename = currentframe().f_back.f_code.co_filename.replace(
Expand All @@ -365,9 +439,17 @@ def danger(self, msg, header=True, tail=True, show_line_info=False):
filename,
header,
tail,
html,
)

def warning(self, msg, header=True, tail=True, show_line_info=True):
def warning(
self,
msg,
header=True,
tail=True,
show_line_info=True,
html=None,
):
if show_line_info: # pragma: no cover
line = currentframe().f_back.f_lineno
filename = currentframe().f_back.f_code.co_filename.replace(
Expand All @@ -393,9 +475,17 @@ def warning(self, msg, header=True, tail=True, show_line_info=True):
filename,
header,
tail,
html,
)

def info(self, msg, header=True, tail=True, show_line_info=False):
def info(
self,
msg,
header=True,
tail=True,
show_line_info=False,
html=None,
):
if show_line_info: # pragma: no cover
line = currentframe().f_back.f_lineno
filename = currentframe().f_back.f_code.co_filename.replace(
Expand All @@ -421,9 +511,17 @@ def info(self, msg, header=True, tail=True, show_line_info=False):
filename,
header,
tail,
html,
)

def error(self, msg, header=True, tail=True, show_line_info=True):
def error(
self,
msg,
header=True,
tail=True,
show_line_info=True,
html=None,
):
if show_line_info: # pragma: no cover
line = currentframe().f_back.f_lineno
filename = currentframe().f_back.f_code.co_filename.replace(
Expand All @@ -449,6 +547,7 @@ def error(self, msg, header=True, tail=True, show_line_info=True):
filename,
header,
tail,
html,
)

def debug_with_style(
Expand All @@ -460,6 +559,7 @@ def debug_with_style(
show_line_info,
header=True,
tail=True,
html=False,
):
if show_line_info: # pragma: no cover
line = currentframe().f_back.f_lineno
Expand All @@ -486,6 +586,7 @@ def debug_with_style(
filename,
header,
tail,
html,
)

def __warningerror(
Expand All @@ -498,6 +599,7 @@ def __warningerror(
filename,
header,
tail,
html=False,
):
# Retrieve the name of the class
clname = self.__class__.__name__
Expand All @@ -524,10 +626,10 @@ def __warningerror(

# Get color
if name == "screen" or name[-1] == "*":
color_ini = self.color(color)
color_end = self.color("close")
color_ini = self.color(color, html)
color_end = self.color("close", html)
else:
color_ini = self.color(None)
color_ini = self.color(None, html)
color_end = color_ini

# Build the message
Expand Down

0 comments on commit 5c062c7

Please sign in to comment.