From f4f91bcdb0413a8dab1345c0c1f5fa6163e6c946 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 12 Dec 2003 14:01:16 +0000 Subject: [PATCH] Removed --exact searching, made to search keys, not values by default, and added --values switch for searching values. --- plugins/Lookup.py | 31 +++++++++++++++++-------------- test/test_Lookup.py | 14 ++++++++++---- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/plugins/Lookup.py b/plugins/Lookup.py index 3b9e2f107..98ab6868f 100644 --- a/plugins/Lookup.py +++ b/plugins/Lookup.py @@ -194,7 +194,7 @@ class Lookup(callbacks.Privmsg): If is given, looks up in the %s database. Otherwise, returns a random key: value pair from the database. There are %s in the database. - """ % (name, utils.nItems(rows, name)) + """ % (name, utils.nItems(name, rows)) f = types.FunctionType(f.func_code, f.func_globals, f.func_name, closure=f.func_closure) f.__doc__ = docstring @@ -203,25 +203,26 @@ class Lookup(callbacks.Privmsg): _sqlTrans = string.maketrans('*?', '%_') def search(self, irc, msg, args): - """[--{regexp,exact}=] + """[--{regexp}=] [--values] Searches the domain for lookups matching . If --regexp is given, its associated value is taken as a regexp and matched - against the lookups; if --exact is given, its associated value is - taken as an exact string to match against the lookups. + against the lookups. If --values is given, search the values rather + 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) db = self.dbHandler.getDb() criteria = [] formats = [] predicateName = 'p' for (option, arg) in options: - if option == '--exact': - criteria.append('value LIKE %s') - formats.append('%' + arg + '%') - elif option == '--regexp': - criteria.append('%s(value)' % predicateName) + if option == '--regexp': + criteria.append('%s(%s)' % (predicateName, column)) try: r = utils.perlReToPythonRe(arg) except ValueError, e: @@ -233,19 +234,21 @@ class Lookup(callbacks.Privmsg): db.create_function(predicateName, 1, p) predicateName += 'p' for glob in globs.split(): - criteria.append('value LIKE %s') if '?' not in glob and '*' not in glob: glob = '*%s*' % glob + criteria.append('%s LIKE %%s' % column) formats.append(glob.translate(self._sqlTrans)) + if not criteria: + raise callbacks.ArgumentError #print 'criteria: %s' % repr(criteria) #print 'formats: %s' % repr(formats) cursor = db.cursor() - sql = """SELECT key, value FROM %s WHERE %s""" % (name, - ' AND '.join(criteria)) + sql = """SELECT key, value FROM %s WHERE %s""" % \ + (name, ' AND '.join(criteria)) #print 'sql: %s' % sql cursor.execute(sql, formats) 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: lookups = ['%s: %s' % (item[0], self._shrink(item[1])) for item in cursor.fetchall()] diff --git a/test/test_Lookup.py b/test/test_Lookup.py index 64b3d3468..7e1c37dde 100644 --- a/test/test_Lookup.py +++ b/test/test_Lookup.py @@ -90,10 +90,16 @@ if sqlite: def testSearch(self): self.assertNotError('lookup add test foo.supyfact') - self.assertResponse('search test mom', 'your mom: my mom') - self.assertResponse('search test b?r', 'foo: bar') - self.assertResponse('search --exact bar test', 'foo: bar') - self.assertResponse('search --regexp m/bar/ test', 'foo: bar') + self.assertError('lookup search b?r') + self.assertResponse('lookup search test b?r', 'bar: baz') + self.assertRegexp('lookup search test foo*', 'foo.*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: