Better constifying of color/font chars

This commit is contained in:
jesopo 2018-11-13 16:02:26 +00:00
parent 16c4e1bfc6
commit 8b9062b942
8 changed files with 67 additions and 40 deletions

View file

@ -20,7 +20,7 @@ CHOICES = [
"Naturally",
"Reply hazy, try again later",
utils.irc.underline(utils.irc.color("DO NOT WASTE MY TIME",
utils.irc.COLOR_RED)),
utils.consts.RED)),
"Hmm... Could be!",
"I'm leaning towards no",
"Without a doubt",

View file

@ -1,7 +1,7 @@
import re
from src import EventManager, ModuleManager, utils
STR_MORE = "%s (more...)" % utils.irc.FONT_RESET
STR_MORE = "%s (more...)" % utils.consts.RESET
STR_CONTINUED = "(...continued) "
COMMAND_METHOD = "command-method"
@ -44,7 +44,7 @@ class Out(object):
prefix = ""
if not self._hide_prefix:
prefix = utils.irc.FONT_RESET + "[%s] " % self.prefix()
prefix = utils.consts.RESET + "[%s] " % self.prefix()
method = self._get_method()
if method == "PRIVMSG":
@ -68,11 +68,11 @@ class Out(object):
class StdOut(Out):
def prefix(self):
return utils.irc.color(utils.irc.bold(self.module_name),
utils.irc.COLOR_GREEN)
utils.consts.GREEN)
class StdErr(Out):
def prefix(self):
return utils.irc.color(utils.irc.bold("!"+self.module_name),
utils.irc.COLOR_RED)
utils.consts.RED)
def _command_method_validate(s):
if s.upper() in COMMAND_METHODS:

View file

@ -174,7 +174,7 @@ class Module(ModuleManager.BaseModule):
# rare!
message = random.choice(DUCK_MESSAGE_RARE)
duck = utils.irc.color(utils.irc.bold(duck + message),
utils.irc.COLOR_RED)
utils.consts.RED)
else:
# not rare!
duck += random.choice(DUCK_MESSAGE)

View file

@ -81,15 +81,15 @@ class Module(ModuleManager.BaseModule):
url = COMMIT_URL % (full_name, id[:8])
added = utils.irc.color("+%d" % len(commit["added"]),
utils.irc.COLOR_GREEN)
utils.consts.GREEN)
added = utils.irc.bold(added)
removed = utils.irc.color("-%d" % len(commit["removed"]),
utils.irc.COLOR_RED)
utils.consts.RED)
removed = utils.irc.bold(removed)
modified = utils.irc.color("±%d" % len(commit["modified"]),
utils.irc.COLOR_PURPLE)
utils.consts.PURPLE)
modified = utils.irc.bold(modified)
outputs.append("(%s) [%s/%s/%s files] commit by '%s': %s - %s"
@ -110,10 +110,10 @@ class Module(ModuleManager.BaseModule):
action_desc = action
if action == "closed":
if data["pull_request"]["merged"]:
action_desc = utils.irc.color("merged", utils.irc.COLOR_GREEN)
action_desc = utils.irc.color("merged", utils.consts.GREEN)
else:
action_desc = utils.irc.color("closed without merging",
utils.irc.COLOR_RED)
utils.consts.RED)
action_desc = utils.irc.bold(action_desc)
pr_title = data["pull_request"]["title"]

View file

@ -19,9 +19,9 @@ class Module(ModuleManager.BaseModule):
_client = None
PASSENGER_ACTIVITIES = ["U", "P", "R"]
COLOURS = [utils.irc.COLOR_LIGHTBLUE, utils.irc.COLOR_GREEN,
utils.irc.COLOR_RED, utils.irc.COLOR_CYAN, utils.irc.COLOR_LIGHTGREY,
utils.irc.COLOR_ORANGE]
COLOURS = [utils.consts.LIGHTBLUE, utils.consts.GREEN,
utils.consts.RED, utils.consts.CYAN, utils.consts.LIGHTGREY,
utils.consts.ORANGE]
@property
def client(self):
@ -184,7 +184,7 @@ class Module(ModuleManager.BaseModule):
severe_summary = ""
if nrcc_severe:
severe_summary += ", "
severe_summary += utils.irc.bold(utils.irc.color("%s severe messages" % nrcc_severe, utils.irc.COLOR_RED))
severe_summary += utils.irc.bold(utils.irc.color("%s severe messages" % nrcc_severe, utils.consts.RED))
station_summary = "%s (%s, %s%s)" % (query["locationName"], query["crs"], query["stationManagerCode"], severe_summary)
if not "trainServices" in query and not "busServices" in query and not "ferryServices" in query:
@ -365,7 +365,7 @@ class Module(ModuleManager.BaseModule):
if "delayReason" in query:
disruptions.append("Delayed (%s%s)" % (query["delayReason"]["value"], " at " + query["delayReason"]["_tiploc"] if query["delayReason"]["_tiploc"] else ""))
if disruptions and not external:
disruptions = utils.irc.color(", ".join(disruptions), utils.irc.COLOR_RED) + " "
disruptions = utils.irc.color(", ".join(disruptions), utils.consts.RED) + " "
elif disruptions and external:
disruptions = ", ".join(disruptions)
else: disruptions = ""
@ -490,7 +490,7 @@ class Module(ModuleManager.BaseModule):
else:
event["stdout"].write("%s%s %s %s (%s/%s/%s): %s" % (disruptions, query["operatorCode"],
query["trainid"], query["serviceType"],
utils.irc.color(done_count, utils.irc.COLOR_LIGHTBLUE),
utils.irc.color(done_count, utils.consts.LIGHTBLUE),
len(stations_filtered), total_count,
", ".join([s["summary"] for s in stations_filtered])))

View file

@ -138,7 +138,7 @@ class ModuleManager(object):
if name in self.waiting_requirement:
for requirement_name in self.waiting_requirement:
self.load_module(bot, requirement_name)
self.log.info("Module '%s' loaded", [name])
self.log.debug("Module '%s' loaded", [name])
def load_modules(self, bot: "IRCBot.Bot", whitelist: typing.List[str]=[],
blacklist: typing.List[str]=[]):
@ -173,8 +173,8 @@ class ModuleManager(object):
references -= 1 # 'del module' removes one reference
references -= 1 # one of the refs is from getrefcount
self.log.info("Module '%s' unloaded (%d reference%s)",
self.log.debug("Module '%s' unloaded (%d reference%s)",
[name, references, "" if references == 1 else "s"])
if references > 0:
self.log.info("References left for '%s': %s",
self.log.debug("References left for '%s': %s",
[name, ", ".join([str(referrer) for referrer in referrers])])

View file

@ -1,2 +1,34 @@
import typing
BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
class IRCColor(object):
def __init__(self, irc: int, ansi: typing.List[int]):
self.irc = irc
self.ansi = ansi
WHITE = IRCColor(0, [1, 37])
BLACK = IRCColor(1, [30])
BLUE = IRCColor(2, [34])
GREEN = IRCColor(3, [32])
RED = IRCColor(4, [1, 31])
BROWN = IRCColor(5, [31])
PURPLE = IRCColor(6, [35])
ORANGE = IRCColor(7, [33])
YELLOW = IRCColor(8, [1, 33])
LIGHTGREEN = IRCColor(9, [1, 32])
CYAN = IRCColor(10, [36])
LIGHTCYAN = IRCColor(11, [1, 36])
LIGHTBLUE = IRCColor(12, [1, 34])
PINK = IRCColor(13, [1, 35])
GREY = IRCColor(14, [1, 30])
LIGHTGREY = IRCColor(15, [37])
BOLD = "\x02"
ITALIC = "\x1D"
UNDERLINE = "\x1F"
INVERT = "\x16"
COLOR = "\x03"
RESET = "\x0F"

View file

@ -1,4 +1,5 @@
import json, string, re, typing
from src import utils
ASCII_UPPER = string.ascii_uppercase
ASCII_LOWER = string.ascii_lowercase
@ -117,35 +118,29 @@ def parse_line(line: str) -> IRCLine:
return IRCLine(tags, prefix, command, IRCArgs(args), has_arbitrary)
COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_GREEN = 0, 1, 2, 3
COLOR_RED, COLOR_BROWN, COLOR_PURPLE, COLOR_ORANGE = 4, 5, 6, 7
COLOR_YELLOW, COLOR_LIGHTGREEN, COLOR_CYAN, COLOR_LIGHTCYAN = (8, 9,
10, 11)
COLOR_LIGHTBLUE, COLOR_PINK, COLOR_GREY, COLOR_LIGHTGREY = (12, 13,
14, 15)
FONT_BOLD, FONT_ITALIC, FONT_UNDERLINE, FONT_INVERT = ("\x02", "\x1D",
"\x1F", "\x16")
FONT_COLOR, FONT_RESET = "\x03", "\x0F"
REGEX_COLOR = re.compile("%s\d\d(?:,\d\d)?" % FONT_COLOR)
REGEX_COLOR = re.compile("%s\d\d(?:,\d\d)?" % utils.consts.COLOR)
def color(s: str, foreground: int, background: int=None) -> str:
foreground = str(foreground).zfill(2)
def color(s: str, foreground: utils.consts.IRCColor,
background: utils.consts.IRCColor=None) -> str:
foreground_s = str(foreground.irc).zfill(2)
background_s = ""
if background:
background = str(background).zfill(2)
return "%s%s%s%s%s" % (FONT_COLOR, foreground,
"" if not background else ",%s" % background, s, FONT_COLOR)
background_s = ",%s" % str(background.irc).zfill(2)
return "%s%s%s%s%s" % (utils.consts.COLOR, foreground_s, background_s, s,
utils.consts.COLOR)
def bold(s: str) -> str:
return "%s%s%s" % (FONT_BOLD, s, FONT_BOLD)
return "%s%s%s" % (utils.consts.BOLD, s, utils.consts.BOLD)
def underline(s: str) -> str:
return "%s%s%s" % (FONT_UNDERLINE, s, FONT_UNDERLINE)
return "%s%s%s" % (utils.consts.UNDERLINE, s, utils.consts.UNDERLINE)
def strip_font(s: str) -> str:
s = s.replace(FONT_BOLD, "")
s = s.replace(FONT_ITALIC, "")
s = s.replace(utils.consts.BOLD, "")
s = s.replace(utils.consts.ITALIC, "")
s = REGEX_COLOR.sub("", s)
s = s.replace(FONT_COLOR, "")
s = s.replace(utils.consts.COLOR, "")
return s
OPT_STR = typing.Optional[str]