Add Note.search and change the wording for listing private note ids.

This commit is contained in:
James Vega 2005-01-07 04:42:27 +00:00
parent 35cc40ac1f
commit 629e459983
2 changed files with 52 additions and 0 deletions

View File

@ -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} <value>] [--sent] [<glob>]
Searches your received notes for ones matching <glob>. 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} <user>]
@ -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

View File

@ -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: