Removed --exact searching, made to search keys, not values by default, and added --values switch for searching values.

This commit is contained in:
Jeremy Fincher 2003-12-12 14:01:16 +00:00
parent 06c85f5c93
commit f4f91bcdb0
2 changed files with 27 additions and 18 deletions

View File

@ -194,7 +194,7 @@ class Lookup(callbacks.Privmsg):
If <key> is given, looks up <key> in the %s database. Otherwise, If <key> is given, looks up <key> in the %s database. Otherwise,
returns a random key: value pair from the database. There are returns a random key: value pair from the database. There are
%s in the database. %s in the database.
""" % (name, utils.nItems(rows, name)) """ % (name, utils.nItems(name, rows))
f = types.FunctionType(f.func_code, f.func_globals, f = types.FunctionType(f.func_code, f.func_globals,
f.func_name, closure=f.func_closure) f.func_name, closure=f.func_closure)
f.__doc__ = docstring f.__doc__ = docstring
@ -203,25 +203,26 @@ class Lookup(callbacks.Privmsg):
_sqlTrans = string.maketrans('*?', '%_') _sqlTrans = string.maketrans('*?', '%_')
def search(self, irc, msg, args): def search(self, irc, msg, args):
"""[--{regexp,exact}=<value>] <name> <glob> """[--{regexp}=<value>] [--values] <name> <glob>
Searches the domain <name> for lookups matching <glob>. If --regexp Searches the domain <name> for lookups matching <glob>. If --regexp
is given, its associated value is taken as a regexp and matched is given, its associated value is taken as a regexp and matched
against the lookups; if --exact is given, its associated value is against the lookups. If --values is given, search the values rather
taken as an exact string to match against the lookups. than the keys.
""" """
(options, rest) = getopt.getopt(args, '', ['regexp=', 'exact=']) column = 'key'
while '--values' in args:
column = 'value'
args.remove('--values')
(options, rest) = getopt.getopt(args, '', ['regexp='])
(name, globs) = privmsgs.getArgs(rest, optional=1) (name, globs) = privmsgs.getArgs(rest, optional=1)
db = self.dbHandler.getDb() db = self.dbHandler.getDb()
criteria = [] criteria = []
formats = [] formats = []
predicateName = 'p' predicateName = 'p'
for (option, arg) in options: for (option, arg) in options:
if option == '--exact': if option == '--regexp':
criteria.append('value LIKE %s') criteria.append('%s(%s)' % (predicateName, column))
formats.append('%' + arg + '%')
elif option == '--regexp':
criteria.append('%s(value)' % predicateName)
try: try:
r = utils.perlReToPythonRe(arg) r = utils.perlReToPythonRe(arg)
except ValueError, e: except ValueError, e:
@ -233,19 +234,21 @@ class Lookup(callbacks.Privmsg):
db.create_function(predicateName, 1, p) db.create_function(predicateName, 1, p)
predicateName += 'p' predicateName += 'p'
for glob in globs.split(): for glob in globs.split():
criteria.append('value LIKE %s')
if '?' not in glob and '*' not in glob: if '?' not in glob and '*' not in glob:
glob = '*%s*' % glob glob = '*%s*' % glob
criteria.append('%s LIKE %%s' % column)
formats.append(glob.translate(self._sqlTrans)) formats.append(glob.translate(self._sqlTrans))
if not criteria:
raise callbacks.ArgumentError
#print 'criteria: %s' % repr(criteria) #print 'criteria: %s' % repr(criteria)
#print 'formats: %s' % repr(formats) #print 'formats: %s' % repr(formats)
cursor = db.cursor() cursor = db.cursor()
sql = """SELECT key, value FROM %s WHERE %s""" % (name, sql = """SELECT key, value FROM %s WHERE %s""" % \
' AND '.join(criteria)) (name, ' AND '.join(criteria))
#print 'sql: %s' % sql #print 'sql: %s' % sql
cursor.execute(sql, formats) cursor.execute(sql, formats)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.reply(msg, 'No %ss matched that query.' % name) irc.reply(msg, 'No %s matched that query.' % utils.pluralize(name))
else: else:
lookups = ['%s: %s' % (item[0], self._shrink(item[1])) lookups = ['%s: %s' % (item[0], self._shrink(item[1]))
for item in cursor.fetchall()] for item in cursor.fetchall()]

View File

@ -90,10 +90,16 @@ if sqlite:
def testSearch(self): def testSearch(self):
self.assertNotError('lookup add test foo.supyfact') self.assertNotError('lookup add test foo.supyfact')
self.assertResponse('search test mom', 'your mom: my mom') self.assertError('lookup search b?r')
self.assertResponse('search test b?r', 'foo: bar') self.assertResponse('lookup search test b?r', 'bar: baz')
self.assertResponse('search --exact bar test', 'foo: bar') self.assertRegexp('lookup search test foo*', 'foo.*foo:bar')
self.assertResponse('search --regexp m/bar/ test', 'foo: bar') self.assertRegexp('lookup search --regexp m/^b/ test',
'bar: baz')
# Values searches.
self.assertResponse('search test --values mom', 'your mom: my mom')
self.assertResponse('search test --values b?r', 'foo: bar')
self.assertResponse('search --values --regexp m/bar/ test',
'foo: bar')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: