From 5aa8839513c1f654405365dd2b93692c0170b4af Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sat, 19 Jun 2021 14:21:34 -0400 Subject: SlackTools: category search --- SlackTools/plugin.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/SlackTools/plugin.py b/SlackTools/plugin.py index e4cc8bb..cacbd87 100644 --- a/SlackTools/plugin.py +++ b/SlackTools/plugin.py @@ -63,6 +63,7 @@ class SlackTools(callbacks.Plugin): threaded = True db = None + categories = None def getMaxResults(self, msg): maxresults = self.registryValue('maxresults') @@ -78,6 +79,21 @@ class SlackTools(callbacks.Plugin): self.db = sqlite3.connect(filename) return self.db + def getCategory(self, cat): + if self.categories is None: + self.categories = {} + db = self.InitDB(); + cursor = db.cursor() + cursor.execute("select id, name from categories"); + result = cursor.fetchall() + for (id, name) in result: + self.categories[name] = id + + if cat in self.categories: + return self.categories[cat] + else: + return None + def PkgQuery(self, irc, msg, searchtype, term): db = self.InitDB(); if db is None: @@ -86,7 +102,19 @@ class SlackTools(callbacks.Plugin): maxresults = self.getMaxResults(msg) cursor = db.cursor() if(searchtype == 'pkg'): - cursor.execute("select c.name, p.name, p.descrip from categories c, packages p where c.id=p.category and p.name glob ? order by c.name, p.name limit ?", (term, maxresults+1)) + args = term.split('/',1) + # no category, search all categories + if len(args) == 1: + term = "*" + term + "*" + cursor.execute("select c.name, p.name, p.descrip from categories c, packages p where c.id=p.category and p.name glob ? order by c.name, p.name limit ?", (term, maxresults+1)) + else: + # category given, only search it + category = self.getCategory(args[0]) + if(category is None): + irc.error("invalid category: '" + args[0] + "', valid categories are: " + str.join(" ", sorted(self.categories.keys())), Raise=True) + term = args[1] + "*" + cursor.execute("select c.name, p.name, p.descrip from categories c, packages p where c.id=p.category and p.category=? and p.name glob ? order by c.name, p.name limit ?", (category, term, maxresults+1)) + # file search (no way to specify category) else: cursor.execute("select distinct c.name, p.name, p.descrip from categories c, packages p, files f where c.id=p.category and p.id=f.package and f.path glob ? order by c.name, p.name limit ?", (term, maxresults+1)) @@ -106,7 +134,7 @@ class SlackTools(callbacks.Plugin): # search for packages by name def pkgsearch(self, irc, msg, args, pkg): - """ + """ [/] Search the Slackware package database for packages named like . This is a case-sensitive substring match (e.g. "core" matches "coreutils" @@ -114,7 +142,7 @@ class SlackTools(callbacks.Plugin): packages whose names begin with "c" and end with "s". """ - self.PkgQuery(irc, msg, 'pkg', "*" + pkg + "*") + self.PkgQuery(irc, msg, 'pkg', pkg) pkgsearch = thread(wrap(pkgsearch, ['somethingWithoutSpaces'])) @@ -144,7 +172,7 @@ class SlackTools(callbacks.Plugin): if(fileflag): self.PkgQuery(irc, msg, 'file', term) else: - self.PkgQuery(irc, msg, 'pkg', "*" + term + "*") + self.PkgQuery(irc, msg, 'pkg', term) pkg = thread(wrap(pkg, [optional(('literal', ['-f'])), 'somethingWithoutSpaces'])) -- cgit v1.2.3