def correction(match, conn, chan, message, nick): """ :type match: re.__Match :type conn: cloudbot.client.Client :type chan: str """ groups = [b.replace("\/", "/") for b in re.split(r"(?<!\\)/", match.groups()[0])] find = groups[0] replace = groups[1].replace("\n","\\n").replace("\r","\\r") for item in conn.history[chan].__reversed__(): sender, timestamp, msg = item if sender != nick: # TODO: allow 's//g' to correct anything continue elif correction_re.match(msg): # don't correct corrections, it gets really confusing continue msg = msg.replace("\n","\\n").replace("\r","\\r") if find.lower() in msg.lower(): if "\x01ACTION" in msg: msg = msg.replace("\x01ACTION", "").replace("\x01", "") mod_msg = ireplace(msg, find, "\x02" + replace + "\x02") message("Correction, * {} {}".format(sender, mod_msg)) else: mod_msg = ireplace(msg, find, "\x02" + replace + "\x02") message("Correction, <{}> {}".format(sender, mod_msg)) msg = ireplace(msg, find, replace) conn.history[chan].append((sender, timestamp, msg)) return else: continue
def correction(match, conn, nick, chan, message): """ :type match: re.__Match :type conn: cloudbot.client.Client :type chan: str """ groups = [b.replace("\/", "/") for b in re.split(r"(?<!\\)/", match.groups()[0])] find = groups[0] replace = groups[1] if find == replace: return "really dude? you want me to replace {} with {}?".format(find, replace) for item in conn.history[chan].__reversed__(): name, timestamp, msg = item if correction_re.match(msg): # don't correct corrections, it gets really confusing continue if find.lower() in msg.lower(): if "\x01ACTION" in msg: msg = msg.replace("\x01ACTION", "").replace("\x01", "") mod_msg = ireplace(msg, find, "\x02" + replace + "\x02") message("Correction, * {} {}".format(name, mod_msg)) else: mod_msg = ireplace(msg, find, "\x02" + replace + "\x02") message("Correction, <{}> {}".format(name, mod_msg)) msg = ireplace(msg, find, replace) if nick.lower() in name.lower(): conn.history[chan].append((name, timestamp, msg)) return else: continue
def test_ireplace(): assert ireplace(test_ireplace_input, "fox", "cat") == "The quick brown cat cat cat jumped over the lazy dog" assert ireplace(test_ireplace_input, "FOX", "cAt") == "The quick brown cAt cAt cAt jumped over the lazy dog" assert ireplace(test_ireplace_input, "fox", "cat", 1) == "The quick brown cat fox FOX jumped over the lazy dog" assert ireplace(test_ireplace_input, "fox", "cat", 2) == "The quick brown cat cat FOX jumped over the lazy dog" # test blank input - this should behave like the native string.replace() assert ireplace("Hello", "", "?") == "?H?e?l?l?o?"
def test_ireplace(): assert ireplace( test_ireplace_input, "fox", "cat") == "The quick brown cat cat cat jumped over the lazy dog" assert ireplace( test_ireplace_input, "FOX", "cAt") == "The quick brown cAt cAt cAt jumped over the lazy dog" assert ireplace( test_ireplace_input, "fox", "cat", 1) == "The quick brown cat fox FOX jumped over the lazy dog" assert ireplace( test_ireplace_input, "fox", "cat", 2) == "The quick brown cat cat FOX jumped over the lazy dog"
def test_ireplace(): assert ireplace( test_ireplace_input, "fox", "cat") == "The quick brown cat cat cat jumped over the lazy dog" assert ireplace( test_ireplace_input, "FOX", "cAt") == "The quick brown cAt cAt cAt jumped over the lazy dog" assert ireplace( test_ireplace_input, "fox", "cat", 1) == "The quick brown cat fox FOX jumped over the lazy dog" assert ireplace( test_ireplace_input, "fox", "cat", 2) == "The quick brown cat cat FOX jumped over the lazy dog" # test blank input - this should behave like the native string.replace() assert ireplace("Hello", "", "?") == "?H?e?l?l?o?"
def correction(match, conn, nick, chan, message): """ :type match: re.__Match :type conn: cloudbot.client.Client :type chan: str """ groups = [unescape_re.sub(r"\1", group or "") for group in match.groups()] find = groups[0] replace = groups[1] if find == replace: return "really dude? you want me to replace {} with {}?".format( find, replace) if not find.strip( ): # Replacing empty or entirely whitespace strings is spammy return "really dude? you want me to replace nothing with {}?".format( replace) for name, timestamp, msg in reversed(conn.history[chan]): if correction_re.match(msg): # don't correct corrections, it gets really confusing continue if find.lower() in msg.lower(): find_esc = re.escape(find) replace_esc = re.escape(replace) if msg.startswith('\x01ACTION'): mod_msg = msg[7:].strip(' \x01') fmt = "* {} {}" else: mod_msg = msg fmt = "<{}> {}" mod_msg = ireplace(re.escape(mod_msg), find_esc, "\x02" + replace_esc + "\x02") mod_msg = unescape_re.sub(r"\1", mod_msg) message("Correction, {}".format(fmt.format(name, mod_msg))) if nick.lower() == name.lower(): msg = ireplace(re.escape(msg), find_esc, replace_esc) msg = unescape_re.sub(r"\1", msg) conn.history[chan].append((name, timestamp, msg)) break
def correction(match, conn, nick, chan, message): """ :type match: re.__Match :type conn: cloudbot.client.Client :type chan: str """ groups = [ b.replace("\/", "/") for b in re.split(r"(?<!\\)/", match.groups()[0]) ] find = groups[0] replace = groups[1] if find == replace: return "really dude? you want me to replace {} with {}?".format( find, replace) for item in conn.history[chan].__reversed__(): name, timestamp, msg = item if correction_re.match(msg): # don't correct corrections, it gets really confusing continue if find.lower() in msg.lower(): if "\x01ACTION" in msg: msg = msg.replace("\x01ACTION", "").replace("\x01", "") mod_msg = ireplace(msg, find, "\x02" + replace + "\x02") message("Correction, * {} {}".format(name, mod_msg)) else: mod_msg = ireplace(msg, find, "\x02" + replace + "\x02") message("Correction, <{}> {}".format(name, mod_msg)) msg = ireplace(msg, find, replace) if nick.lower() in name.lower(): conn.history[chan].append((name, timestamp, msg)) return else: continue
def test_ireplace(): assert ireplace(test_ireplace_input, "fox", "cat") == "The quick brown cat cat cat jumped over the lazy dog" assert ireplace(test_ireplace_input, "FOX", "cAt") == "The quick brown cAt cAt cAt jumped over the lazy dog" assert ireplace(test_ireplace_input, "fox", "cat", 1) == "The quick brown cat fox FOX jumped over the lazy dog" assert ireplace(test_ireplace_input, "fox", "cat", 2) == "The quick brown cat cat FOX jumped over the lazy dog"