From 629e45998366bffa2833849c64e5f8ba87ef2b8a Mon Sep 17 00:00:00 2001 From: James Vega Date: Fri, 7 Jan 2005 04:42:27 +0000 Subject: [PATCH] Add Note.search and change the wording for listing private note ids. --- plugins/Note.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/test_Note.py | 8 ++++++++ 2 files changed, 52 insertions(+) diff --git a/plugins/Note.py b/plugins/Note.py index 8e3f0d2d4..799b6459e 100644 --- a/plugins/Note.py +++ b/plugins/Note.py @@ -34,7 +34,9 @@ users that can be retrieved later. __revision__ = "$Id$" +import re import time +import fnmatch import operator import supybot.dbi as dbi @@ -310,6 +312,46 @@ class Note(callbacks.Privmsg): else: return '#%s (private)' % note.id + def search(self, irc, msg, args, user, optlist, glob): + """[--{regexp} ] [--sent] [] + + Searches your received notes for ones matching . If --regexp is + given, its associated value is taken as a regexp and matched against + the notes. If --sent is specified, only search sent notes. + """ + criteria = [] + def to(note): + return note.to == user.id + def frm(note): + return note.frm == user.id + own = to + for (option, arg) in optlist: + if option == 'regexp': + criteria.append(arg.search) + elif option == 'sent': + own = frm + if glob: + glob = fnmatch.translate(glob) + # ignore the trailing $ fnmatch.translate adds to the regexp + criteria.append(re.compile(glob[:-1]).search) + def match(note): + for p in criteria: + if not p(note.text): + return False + return True + notes = list(self.db.select(lambda n: match(n) and own(n))) + if not notes: + irc.reply('No matching notes were found.') + else: + utils.sortBy(operator.attrgetter('id'), notes) + ids = [self._formatNoteId(msg, note) for note in notes] + ids = self._condense(ids) + irc.reply(utils.commaAndify(ids)) + search = wrap(search, + ['user', getopts({'regexp': ('regexpMatcher', True), + 'sent': ''}), + additional('glob')]) + def list(self, irc, msg, args, user, optlist): """[--{old,sent}] [--{from,to} ] @@ -361,6 +403,8 @@ class Note(callbacks.Privmsg): temp[note[1]] = [note[0]] notes = [] for (k,v) in temp.iteritems(): + if '(private)' in k: + k = k.replace('(private)', '%s private' % utils.be(len(v))) notes.append('%s %s' % (utils.commaAndify(v), k)) return notes diff --git a/test/test_Note.py b/test/test_Note.py index 686ec0160..bdeaea92f 100644 --- a/test/test_Note.py +++ b/test/test_Note.py @@ -76,6 +76,14 @@ class NoteTestCase(PluginTestCase, PluginDocumentation): self.assertResponse('note list --old', '#1 from inkedmn') self.assertResponse('note list --old --from inkedmn','#1 from inkedmn') + def testSearch(self): + self.assertNotError('note send inkedmn testing') + _ = self.getMsg(' ') + self.assertNotError('note send inkedmn 1,2,3') + _ = self.getMsg(' ') + self.assertRegexp('note search test', r'#1') + self.assertRegexp('note search --regexp m/1,2/', r'#2') + self.assertRegexp('note search --sent test', r'#1') # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: