🏡 index : ~doyle/dave.git

author Jordan Doyle <jordan@doyle.wf> 2017-08-05 11:22:18.0 +01:00:00
committer Jordan Doyle <jordan@doyle.wf> 2017-08-05 11:22:18.0 +01:00:00
commit
30fab221bfce663146d5e95ae8372d14cf7d4458 [patch]
tree
85c14b660f9182929100d7c348a9e907ed978a7b
parent
23f895b43a7ba8aee05079511e81794862f8745d
download
30fab221bfce663146d5e95ae8372d14cf7d4458.tar.gz

Only decode messages from redis once



Diff

 dave/modules/sed.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/dave/modules/sed.py b/dave/modules/sed.py
index 10da8f0..4479bbf 100644
--- a/dave/modules/sed.py
+++ a/dave/modules/sed.py
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
"""Pass any messages beginning with 'sed' to GNU sed."""
import dave.module
import dave.config
import re
from twisted.python import log

@dave.module.help("Syntax: s/find/replace/flags", "sed")
@dave.module.match(r"^((s|y)(/|\||!)(.*?)(\3)(.*?)(\3)([gIi]+)?)$")
@@ -11,6 +12,12 @@
    key = "msg:{}:{}".format(source, sender)

    for i, msg in enumerate(dave.config.redis.lrange(key, 0, -1)):
        try:
            msg = msg.decode('utf-8')
        except Exception as e:
            log.err(e, "Failed decoding previous messages from redis.")
            continue

        flags = list(args[7]) if args[7] else []
        f = re.UNICODE

@@ -19,16 +26,16 @@

        try:
            # bold replacements
            toDisplay = re.sub(args[3], "\x02{}\x0F".format(args[5]),
                             msg.decode('utf-8'), count=0 if 'g' in flags else 1, flags=f)
            toSave = re.sub(args[3], args[5],
                            msg.decode('utf-8'), count=0 if 'g' in flags else 1, flags=f)
            toDisplay = re.sub(args[3], "\x02{}\x0F".format(args[5]), msg,
                             count=0 if 'g' in flags else 1, flags=f)
            toSave = re.sub(args[3], args[5], msg,
                            count=0 if 'g' in flags else 1, flags=f)
        except Exception as e:
            bot.reply(source, sender,
                      "There was a problem with your sed command: {}".format(str(e)))
            return

        if toSave.strip() != msg:
        if toSave != msg:
            bot.msg(source, "<{}> {}".format(sender, toDisplay.strip()))
            dave.config.redis.lset(key, i, toSave.strip())
            return