1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
#!/usr/bin/python
# debtags - Implement package tags support for Debian
#
# Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import wx
import wx.html
import wx.lib.dialogs
import sys
import re
import subprocess
from debian import debtags
import apt
from optparse import OptionParser
VERSION="0.1"
class Model(wx.EvtHandler):
class ModelEvent(wx.PyCommandEvent):
def __init__(self, event_type):
wx.PyCommandEvent.__init__(self, event_type)
# Create custom events
wxEVT_CHANGED = wx.NewEventType()
EVT_CHANGED = wx.PyEventBinder(wxEVT_CHANGED, 0)
def __init__(self, fullcoll):
wx.EvtHandler.__init__(self)
self.apt_cache = apt.Cache()
self.wanted = set()
self.unwanted = set()
self.ignored = set()
self.interesting = []
self.discriminant = []
self.uninteresting = []
self.fullcoll = fullcoll
self.subcoll = fullcoll
self.refilter()
def tag_match(self, pkg):
tags = self.fullcoll.tags_of_package(pkg)
if len(self.wanted) > 0 and not self.wanted.issubset(tags):
return False
if len(self.unwanted) > 0 and len(tags.intersection(self.unwanted)) > 0:
return False
return True
def refilter(self):
# Regenerate subcoll
if len(self.wanted) == 0 and len(self.unwanted) == 0:
self.subcoll = self.fullcoll
else:
self.subcoll = self.fullcoll.filter_packages(self.tag_match)
# Compute the most interesting tags by discriminance
self.discriminant = sorted(self.subcoll.iter_tags(), \
lambda b, a: cmp(self.subcoll.discriminance(a), self.subcoll.discriminance(b)))
#print "-----------------------------"
#for d in self.discriminant:
# print d, self.subcoll.discriminance(d)
# Notify the change
e = Model.ModelEvent(Model.wxEVT_CHANGED)
self.ProcessEvent(e)
def add_wanted(self, tag):
self.wanted.add(tag)
if tag in self.unwanted: self.unwanted.remove(tag)
if tag in self.ignored: self.ignored.remove(tag)
self.refilter()
def add_unwanted(self, tag):
self.unwanted.add(tag)
if tag in self.wanted: self.wanted.remove(tag)
if tag in self.ignored: self.ignored.remove(tag)
self.refilter()
def add_ignored(self, tag):
self.ignored.add(tag)
if tag in self.wanted: self.wanted.remove(tag)
if tag in self.unwanted: self.unwanted.remove(tag)
self.refilter()
def remove_tag_from_filter(self, tag):
if tag in self.wanted: self.wanted.remove(tag)
if tag in self.unwanted: self.unwanted.remove(tag)
if tag in self.ignored: self.ignored.remove(tag)
self.refilter()
def set_query(self, query):
query = query.split()
def match(pkg):
for q in query:
try:
if pkg.name.find(q) == -1 and \
pkg.raw_description.find(q) == -1:
return False
except UnicodeDecodeError:
desc = pkg.raw_description.decode("ascii", "replace")
if pkg.name.find(q) == -1 and \
desc.find(q) == -1:
return False
return True
subcoll = self.fullcoll.choose_packages(map(lambda x: x.name, filter(match, self.apt_cache)))
rel_index = debtags.relevance_index_function(self.fullcoll, subcoll)
# Get all the tags sorted by increasing relevance
self.interesting = sorted(subcoll.iter_tags(), lambda b, a: cmp(rel_index(a), rel_index(b)))
# Get the list of the uninteresting tags, sorted by cardinality (the
# ones that you are less likely to want, and first the ones that will
# weed out most results)
self.uninteresting = set(self.fullcoll.iter_tags())
self.uninteresting -= set(subcoll.iter_tags())
if len(self.wanted) == 0:
self.wanted = set(fullcoll.ideal_tagset(self.interesting))
self.refilter()
# Notify the change
e = Model.ModelEvent(Model.wxEVT_CHANGED)
self.ProcessEvent(e)
class TagList(wx.html.HtmlWindow):
class TagListEvent(wx.PyCommandEvent):
def __init__(self, event_type):
wx.PyCommandEvent.__init__(self, event_type)
self.action = None
self.tag = None
# Create custom events
wxEVT_ACTION = wx.NewEventType()
EVT_ACTION = wx.PyEventBinder(wxEVT_ACTION, 0)
def __init__(self, parent, model):
wx.html.HtmlWindow.__init__(self, parent)
self.model = model
self.model.Bind(Model.EVT_CHANGED, self.model_changed)
self.SetBorders(0)
self.SetFonts("", "", [4, 6, 8, 10, 11, 12, 13])
def OnLinkClicked(self, link_info):
# Notify the change
action, tag = link_info.GetHref().split(':',1)
e = TagList.TagListEvent(TagList.wxEVT_ACTION)
e.action, e.tag = action, tag
self.ProcessEvent(e)
def model_changed(self, event):
#print "TLMC"
self.SetPage("<html><body>")
first = True
if len(self.model.wanted):
if first: first = False;
else: self.AppendToPage("<br>")
self.AppendToPage("Wanted:<br>")
for tag in self.model.wanted:
self.AppendToPage(" <a href='del:%s'>%s</a><br>" % (tag, tag))
if len(self.model.unwanted):
if first: first = False;
else: self.AppendToPage("<br>")
self.AppendToPage("Not wanted:<br>")
for tag in self.model.unwanted:
self.AppendToPage(" <a href='del:%s'>%s</a><br>" % (tag, tag))
def filter_candidate(tag):
if self.model.subcoll.card(tag) == 0: return False
if tag in self.model.wanted: return False
if tag in self.model.unwanted: return False
if tag in self.model.ignored: return False
return True
interesting = filter(filter_candidate, self.model.interesting)[:7]
if len(interesting):
if first: first = False;
else: self.AppendToPage("<br>")
self.AppendToPage("Candidate tags:<br>")
for tag in interesting:
self.AppendToPage(" <a href='add:%s'>%s</a> <a href='addnot:%s'>[no]</a> (%d pkgs)<br>" % (tag, tag, tag, self.model.subcoll.card(tag)))
discr = filter(filter_candidate, self.model.discriminant)[:7]
if len(discr):
if first: first = False;
else: self.AppendToPage("<br>")
for tag in discr:
self.AppendToPage(" <a href='add:%s'>%s</a> <a href='addnot:%s'>[no]</a> (%d pkgs)<br>" % (tag, tag, tag, self.model.subcoll.card(tag)))
unint = filter(filter_candidate,
sorted(self.model.uninteresting, lambda a,b: cmp(self.model.subcoll.card(b), self.model.subcoll.card(a))))[:7]
if len(unint):
if first: first = False;
else: self.AppendToPage("<br>")
self.AppendToPage("Candidate unwanted tags:<br>")
for tag in unint:
self.AppendToPage(" <a href='addnot:%s'>%s</a> <a href='add:%s'>[yes]</a> (%d pkgs)<br>" % (tag, tag, tag, self.model.subcoll.card(tag)))
self.AppendToPage("</body></html>")
event.Skip()
class Results(wx.ListCtrl):
def __init__(self, parent, model):
wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT|wx.LC_VIRTUAL)
self.model = model
self.model.Bind(Model.EVT_CHANGED, self.model_changed)
self.packages = []
self.InsertColumn(0, "Name")
self.InsertColumn(1, "Description")
self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
self.SetColumnWidth(1, wx.LIST_AUTOSIZE)
self.Bind(wx.EVT_SIZE, self.OnResize)
def resize_columns(self):
"""
Ugly hack to have some decent size for the columns, since the ListCtrl
appearently won't autosize itself.
"""
w, h = self.GetClientSizeTuple()
self.SetColumnWidth(0, w * 0.3)
# -20 to hope to account for the vertical scrollbar, when it's present
self.SetColumnWidth(1, w * 0.7 - 20)
def model_changed(self, event):
self.packages = sorted(self.model.subcoll.iter_packages())
self.packages.sort()
self.SetItemCount(len(self.packages))
self.resize_columns()
event.Skip()
def OnResize(self, event):
self.resize_columns()
event.Skip()
def OnGetItemText(self, row, col):
if col == 0:
return self.packages[row]
else:
aptpkg = self.model.apt_cache[self.packages[row]]
return aptpkg.raw_description.split("\n")[0]
class SearchWindow(wx.Frame):
ACTION_QUIT = wx.NewId()
ACTION_CONTEXT_HELP = wx.NewId()
ACTION_ABOUT = wx.NewId()
def __init__(self, parent, model, title):
wx.Frame.__init__(self, parent, -1, title,
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
self.status_bar = self.CreateStatusBar()
# Create menu bar
self.menu_bar = wx.MenuBar()
m_file = wx.Menu()
m_file.Append(SearchWindow.ACTION_QUIT, "&Quit", "Quit wxdballe")
self.menu_bar.Append(m_file, "&File")
m_file = wx.Menu()
m_file.Append(SearchWindow.ACTION_CONTEXT_HELP, "&What is...", "Show context information about interface elements")
m_file.Append(SearchWindow.ACTION_ABOUT, "&About...", "Show information about this application")
self.menu_bar.Append(m_file, "&Help")
self.SetMenuBar(self.menu_bar)
self.Bind(wx.EVT_MENU, self.on_action)
self.model = model
self.model.Bind(Model.EVT_CHANGED, self.model_changed)
self.SetSizeHints(500, 500)
query_panel = wx.Panel(self, style=wx.SUNKEN_BORDER)
query_field = wx.Panel(query_panel)
self.query = wx.TextCtrl(query_field)
self.query.SetHelpText("Enter here some keyword about what you are looking for. They will lead you to a selection of categories that you can use to browse the packages")
self.query.Bind(wx.EVT_CHAR, self.query_changed)
# self.query.Bind(wx.EVT_TEXT, self.query_changed)
self.query_button = wx.Button(query_field, -1, "Go", style=wx.BU_EXACTFIT)
self.query_button.Bind(wx.EVT_BUTTON, self.go_button_pressed)
self.query_button.SetHelpText("Look for keyword corresponding to the text entered in the Search field.")
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(wx.StaticText(query_field, -1, "Search: "), 0, wx.ALIGN_CENTER_VERTICAL)
box.Add(self.query, 1, wx.EXPAND)
box.Add(self.query_button, 0, wx.ALIGN_CENTER_VERTICAL)
query_field.SetSizerAndFit(box)
self.tag_list = TagList(query_panel, model)
self.tag_list.Bind(TagList.EVT_ACTION, self.wanted_event)
self.tag_list.SetHelpText("Tags used for searching. Candidate tags are up for selection: click on a tag to say that you want it, and click on 'no' next to it to say that you do not want it. To remove a tag from the 'wanted' or 'unwanted' list, just click on it")
box = wx.BoxSizer(wx.VERTICAL)
box.Add(query_field, 0, wx.EXPAND)
box.Add(self.tag_list, 3, wx.EXPAND)
query_panel.SetSizerAndFit(box)
self.results = Results(self, model)
self.results.SetHelpText("List of packages matching the current selecion of tags")
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(query_panel, 2, wx.EXPAND)
box.Add(self.results, 3, wx.EXPAND)
self.SetSizerAndFit(box)
self.timed_updater = None
self.Bind(wx.EVT_CHAR, self.on_char)
self.query.SetFocus()
def on_action(self, event):
id = event.GetId()
if id == SearchWindow.ACTION_QUIT:
self.Destroy()
elif id == SearchWindow.ACTION_ABOUT:
msg = """
Prototype for a new concept of package search
=============================================
Introduction
------------
Debtags Smart Search is an attempt to use the Debtags
category data to create a powerful and intuitive search
metaphore. It combines the ease of use of a keyword search
text box with the unambiguiry and accuracy of categories.
The keywords entered are not used to filter packages, but to
create a selection of relevant tags for the user to choose
from.
The actual package filtering is done with the tags only. The
way the user interact with tags is through simple "I want
this"/"I don't want this" decisions.
Tips
----
* you can be vague while giving keywords, since you can
always refine later using the tags
* you can change the search keywords in the middle of a
search, to get a different selection of candidate tags.
"""
dia = wx.lib.dialogs.ScrolledMessageDialog(self, msg, "About Debtags Smart Search")
dia.ShowModal()
elif id == SearchWindow.ACTION_CONTEXT_HELP:
context_help = wx.ContextHelp()
context_help.BeginContextHelp()
def on_char(self, event):
c = event.GetKeyCode()
# Quit on ^Q
if c == 17:
self.Destroy()
def wanted_event(self, event):
action, tag = event.action, event.tag
if action == 'add':
self.model.add_wanted(tag)
elif action == 'addnot':
print "wanted_event -> addnot"
self.model.add_unwanted(tag)
elif action == 'del':
self.model.remove_tag_from_filter(tag)
else:
print "Unknown action", action
def go_button_pressed(self, event):
self.model.set_query(self.query.GetValue())
def query_changed(self, event):
"Delayed update of the filter from the value of the input fields"
c = event.GetKeyCode()
if c == 13:
self.model.set_query(self.query.GetValue())
event.Skip()
def model_changed(self, event):
self.status_bar.SetStatusText("%d packages" % (self.model.subcoll.package_count()))
event.Skip()
##
### Main
###
class Parser(OptionParser):
def __init__(self, *args, **kwargs):
OptionParser.__init__(self, *args, **kwargs)
def error(self, msg):
sys.stderr.write("%s: error: %s\n\n" % (self._get_prog_name(), msg))
self.print_help(sys.stderr)
sys.exit(2)
if __name__ == "__main__":
parser = Parser(usage="usage: %prog [options]",
version="%prog "+ VERSION,
description="smart search of Debian packages")
parser.add_option("--tagdb", default="/var/lib/debtags/package-tags", help="Tag database to use (default: %default)")
(options, args) = parser.parse_args()
app = wx.PySimpleApp()
provider = wx.SimpleHelpProvider()
wx.HelpProvider_Set(provider)
# Read full database
fullcoll = debtags.DB()
tag_filter = re.compile(r"^special::.+$|^.+::TODO$")
fullcoll.read(open(options.tagdb, "r"), lambda x: not tag_filter.match(x))
model = Model(fullcoll)
sw = SearchWindow(None, model, "Debtags Smart Search")
sw.Show()
app.MainLoop()
# vim:set ts=4 sw=4 expandtab:
|