From 791df9409d654283281cdea35d4653ae1441fe63 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Fri, 7 Feb 2014 11:12:11 -0800 Subject: [PATCH] remember: add s/from/to syntax for easier modifications --- plugins/remember.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/plugins/remember.py b/plugins/remember.py index 91d7cb9..d35f9d5 100644 --- a/plugins/remember.py +++ b/plugins/remember.py @@ -2,6 +2,9 @@ remember.py: written by Scaevolus 2010 """ +import string +import re + from util import hook @@ -23,10 +26,12 @@ def get_memory(db, chan, word): @hook.command @hook.command("r") def remember(inp, nick='', chan='', db=None): - ".remember [+] -- maps word to data in the memory" + ".remember [+] s// -- maps word to data in the memory, or " + " does string replacement (not regex)" db_init(db) append = False + replacement = False try: head, tail = inp.split(None, 1) @@ -34,19 +39,32 @@ def remember(inp, nick='', chan='', db=None): return remember.__doc__ data = get_memory(db, chan, head) + _head, _tail = data.split(None, 1) if tail[0] == '+': append = True # ignore + symbol new = tail[1:] - _head, _tail = data.split(None, 1) # data is stored with the input so ignore it when re-adding it - import string if len(tail) > 1 and tail[1] in (string.punctuation + ' '): tail = _tail + new else: tail = _tail + ' ' + new + if len(tail) > 2 and tail[0] == 's' and tail[1] in string.punctuation: + args = tail.split(tail[1]) + if len(args) == 4 and args[3] == '': + args = args[:-1] + if len(args) == 3: + replacement = True + _, src, dst = args + new_data = _tail.replace(src, dst) + if new_data == _tail: + return 'replacement left data unchanged' + tail = new_data + else: + return 'invalid replacement syntax -- try s$foo$bar instead?' + db.execute("replace into memory(chan, word, data, nick) values" " (?,lower(?),?,?)", (chan, head, head + ' ' + tail, nick)) db.commit() @@ -54,6 +72,8 @@ def remember(inp, nick='', chan='', db=None): if data: if append: return "appending %s to %s" % (new, data.replace('"', "''")) + elif replacement: + return "replacing '%s' with '%s' in %s" % (src, dst, _tail) else: return 'forgetting "%s", remembering this instead.' % \ data.replace('"', "''")