utils.irc doesn't need to know about the whole of utils

This commit is contained in:
jesopo 2019-10-29 18:00:19 +00:00
parent d2b1250c97
commit 46e4b75f6b

View file

@ -1,5 +1,5 @@
import json, string, re, typing, uuid import json, string, re, typing, uuid
from src import utils from . import consts
ASCII_UPPER = string.ascii_uppercase ASCII_UPPER = string.ascii_uppercase
ASCII_LOWER = string.ascii_lowercase ASCII_LOWER = string.ascii_lowercase
@ -29,35 +29,35 @@ def lower(case_mapping: str, s: str) -> str:
def equals(case_mapping: str, s1: str, s2: str) -> bool: def equals(case_mapping: str, s1: str, s2: str) -> bool:
return lower(case_mapping, s1) == lower(case_mapping, s2) return lower(case_mapping, s1) == lower(case_mapping, s2)
REGEX_COLOR = re.compile("%s(?:(\d{1,2})(?:,(\d{1,2}))?)?" % utils.consts.COLOR) REGEX_COLOR = re.compile("%s(?:(\d{1,2})(?:,(\d{1,2}))?)?" % consts.COLOR)
def color(s: str, foreground: utils.consts.IRCColor, def color(s: str, foreground: consts.IRCColor,
background: utils.consts.IRCColor=None) -> str: background: consts.IRCColor=None) -> str:
foreground_s = str(foreground.irc).zfill(2) foreground_s = str(foreground.irc).zfill(2)
background_s = "" background_s = ""
if background: if background:
background_s = ",%s" % str(background.irc).zfill(2) background_s = ",%s" % str(background.irc).zfill(2)
return "%s%s%s%s%s" % (utils.consts.COLOR, foreground_s, background_s, s, return "%s%s%s%s%s" % (consts.COLOR, foreground_s, background_s, s,
utils.consts.COLOR) consts.COLOR)
def bold(s: str) -> str: def bold(s: str) -> str:
return "%s%s%s" % (utils.consts.BOLD, s, utils.consts.BOLD) return "%s%s%s" % (consts.BOLD, s, consts.BOLD)
def underline(s: str) -> str: def underline(s: str) -> str:
return "%s%s%s" % (utils.consts.UNDERLINE, s, utils.consts.UNDERLINE) return "%s%s%s" % (consts.UNDERLINE, s, consts.UNDERLINE)
def strip_font(s: str) -> str: def strip_font(s: str) -> str:
s = s.replace(utils.consts.BOLD, "") s = s.replace(consts.BOLD, "")
s = s.replace(utils.consts.ITALIC, "") s = s.replace(consts.ITALIC, "")
s = REGEX_COLOR.sub("", s) s = REGEX_COLOR.sub("", s)
s = s.replace(utils.consts.COLOR, "") s = s.replace(consts.COLOR, "")
return s return s
FORMAT_TOKENS = [ FORMAT_TOKENS = [
utils.consts.BOLD, consts.BOLD,
utils.consts.RESET, consts.RESET,
utils.consts.UNDERLINE consts.UNDERLINE
] ]
FORMAT_STRIP = [ FORMAT_STRIP = [
"\x08" # backspace "\x08" # backspace
@ -95,7 +95,7 @@ def _format_tokens(s: str) -> typing.List[str]:
background = "" background = ""
is_background = False is_background = False
if char == utils.consts.COLOR: if char == consts.COLOR:
if is_color: if is_color:
matches.append(char) matches.append(char)
else: else:
@ -109,7 +109,7 @@ def _format_tokens(s: str) -> typing.List[str]:
def _color_match(code: typing.Optional[str], foreground: bool) -> str: def _color_match(code: typing.Optional[str], foreground: bool) -> str:
if not code: if not code:
return "" return ""
color = utils.consts.COLOR_CODES[int(code)] color = consts.COLOR_CODES[int(code)]
return color.to_ansi(not foreground) return color.to_ansi(not foreground)
def parse_format(s: str) -> str: def parse_format(s: str) -> str:
@ -122,7 +122,7 @@ def parse_format(s: str) -> str:
replace = "" replace = ""
type = token[0] type = token[0]
if type == utils.consts.COLOR: if type == consts.COLOR:
match = REGEX_COLOR.match(token) match = REGEX_COLOR.match(token)
if match and (match.group(1) or match.group(2)): if match and (match.group(1) or match.group(2)):
@ -138,23 +138,23 @@ def parse_format(s: str) -> str:
else: else:
if has_foreground: if has_foreground:
has_foreground = False has_foreground = False
replace += utils.consts.ANSI_FOREGROUND_RESET replace += consts.ANSI_FOREGROUND_RESET
if has_background: if has_background:
has_background = False has_background = False
replace += utils.consts.ANSI_BACKGROUND_RESET replace += consts.ANSI_BACKGROUND_RESET
elif type == utils.consts.BOLD: elif type == consts.BOLD:
if bold: if bold:
replace += utils.consts.ANSI_BOLD_RESET replace += consts.ANSI_BOLD_RESET
else: else:
replace += utils.consts.ANSI_BOLD replace += consts.ANSI_BOLD
bold = not bold bold = not bold
elif type == utils.consts.RESET: elif type == consts.RESET:
replace += utils.consts.ANSI_RESET replace += consts.ANSI_RESET
elif type == utils.consts.UNDERLINE: elif type == consts.UNDERLINE:
if underline: if underline:
replace += utils.consts.ANSI_UNDERLINE_RESET replace += consts.ANSI_UNDERLINE_RESET
else: else:
replace += utils.consts.ANSI_UNDERLINE replace += consts.ANSI_UNDERLINE
underline = not underline underline = not underline
elif type in FORMAT_STRIP: elif type in FORMAT_STRIP:
replace = "" replace = ""
@ -162,13 +162,13 @@ def parse_format(s: str) -> str:
s = s.replace(token, replace, 1) s = s.replace(token, replace, 1)
if has_foreground: if has_foreground:
s += utils.consts.ANSI_FOREGROUND_RESET s += consts.ANSI_FOREGROUND_RESET
if has_background: if has_background:
s += utils.consts.ANSI_BACKGROUND_RESET s += consts.ANSI_BACKGROUND_RESET
if bold: if bold:
s += utils.consts.ANSI_BOLD_RESET s += consts.ANSI_BOLD_RESET
if underline: if underline:
s += utils.consts.ANSI_UNDERLINE_RESET s += consts.ANSI_UNDERLINE_RESET
return s return s