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
|
#!/usr/bin/env python
"""
window.py
Copyright (C) 2006 David Boddie
This file is part of PyPI Browser, a GUI browser for the Python Package Index.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from constants import __version__
import desktop
from dialogs import ActionEditorDialog, ConfigurationDialog, DownloadDialog, \
InformationWindow
from packagemodel import PackageModel
import os
import pypi
from searchmodel import SearchModel
import sys
from ui_window import Ui_Window
import urllib2
class Window(QMainWindow, Ui_Window):
"""Window(QMainWindow, Ui_Window)
A class to provide the main application window and contain the
infrastructure used by components to communicate with each other.
"""
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
# Create a settings object that will be shared between
# application components.
self.settings = QSettings("boddie.org.uk", "PyPI Browser")
# We use two models: an underlying package model and a search
# model that filters packages based on whether they match search
# terms and whether they are marked. The tree view shows the
# contents of the search model.
self.package_server = pypi.AbstractServer()
self.packageModel = PackageModel(self.package_server)
self.searchModel = SearchModel(self.package_server)
self.searchModel.setSourceModel(self.packageModel)
self.treeView.setModel(self.searchModel)
self.markedPackages = 0
self.matchingPackages = 0
self.windows = []
# Set up signal-slot connections defined in the .ui files and
# those providing higher-level functionality.
QMetaObject.connectSlotsByName(self)
# Load user-defined actions.
ActionEditorDialog.loadSettings(self.settings, self.findChildren(QAction))
self.connect(self.openAction, SIGNAL("triggered()"), self.openIndex)
self.connect(self.downloadAction, SIGNAL("triggered()"), self.download)
self.connect(self.reloadListAction, SIGNAL("triggered()"),
self.reloadPackages)
self.connect(self.filterMarkedAction, SIGNAL("triggered(bool)"),
self.setMarkedFilter)
self.connect(self.filterNewAction, SIGNAL("triggered(bool)"),
self.setNewFilter)
self.connect(self.searchModel, SIGNAL("resultsFound(const QString &)"),
self.showResults)
self.connect(self.searchModel, SIGNAL("markedChanged(bool)"),
self.downloadAction, SLOT("setEnabled(bool)"))
self.connect(self.packageModel, SIGNAL("operationStarted()"),
self.showWaitCursor)
self.connect(self.packageModel, SIGNAL("operationFinished()"),
self.unsetCursor)
self.connect(self.searchModel, SIGNAL("operationStarted()"),
self.showWaitCursor)
self.connect(self.searchModel, SIGNAL("operationFinished()"),
self.unsetCursor)
self.connect(self.treeView, SIGNAL("activated(const QModelIndex &)"),
self.showInformation)
self.connect(self.treeView, SIGNAL("expanded(const QModelIndex &)"),
self.resizeColumns)
#self.connect(self.treeView, SIGNAL("collapsed(const QModelIndex &)"),
# self.resizeColumns)
self.connect(self.fieldComboBox,
SIGNAL("currentIndexChanged(int)"),
self.searchModel.setSearchField)
self.connect(self.searchLineEdit, SIGNAL("textChanged(const QString &)"),
self.searchModel.setSearchTerms)
self.connect(self.searchLineEdit, SIGNAL("returnPressed()"),
self.searchModel.search)
self.connect(self.searchButton, SIGNAL("clicked()"),
self.searchModel.search)
self.connect(self.configureBrowserAction, SIGNAL("triggered()"),
self.configureBrowser)
self.connect(self.editShortcutsAction, SIGNAL("triggered()"),
self.editShortcuts)
self.connect(self.aboutAction, SIGNAL("triggered()"), self.about)
self.connect(self.aboutQtAction, SIGNAL("triggered()"), self.aboutQt)
self.connect(self.openManualAction, SIGNAL("triggered()"),
self.openManual)
def about(self):
QMessageBox.about(self,
self.tr("About PyPI Browser %1").arg(__version__),
self.tr("<qt><h3>About PyPI Browser %1</h3>"
"<p>PyPI Browser allows you to examine available "
"packages in the Python Package Index and other package "
"indexes that expose a compatible XML-RPC interface.</p>"
"<p>Uses desktop integration features provided by version "
"%2 of the <i>desktop</i> module (search the package "
"index for more information).</p></qt>").arg(__version__)
.arg(desktop.__version__))
def aboutQt(self):
QMessageBox.aboutQt(self)
def changeServer(self):
"""changeModel(self, newModel)
Replace the existing package model and set up the search model
to filter the contents of the new model.
"""
self.packageModel.setServer(self.package_server)
self.searchModel.setServer(self.package_server)
def closeEvent(self, event):
"""closeEvent(self, event)
Checks for marked packages and accepts the close event only if
there are either no marked packages or if the user discards them.
"""
gen = self.marked()
try:
gen.next()
if not self.confirmDiscard():
event.ignore()
return
except StopIteration:
pass
self.packageModel.save(self.settings)
for widget in qApp.topLevelWidgets():
widget.close()
def configureBrowser(self):
"""configureBrowser(self)
Opens a configuration dialog to allow the user to change the
behaviour of the application.
"""
dialog = ConfigurationDialog(self.settings, self)
if dialog.exec_() == QDialog.Accepted:
dialog.saveSettings()
self.settings.sync()
def confirmDiscard(self):
"""confirmDiscard(self)
Opens a message dialog asking whether the user wants to discard
the current list of marked packages. Returns true if the user
discards the packages; otherwise returns false.
"""
answer = QMessageBox.warning(self, self.tr("Discard List"),
self.tr("<qt>You have marked packages for download.\n"
"Click <b>OK</b> to discard this list.</qt>"),
QMessageBox.Ok, QMessageBox.Cancel)
if answer == QMessageBox.Ok:
return True
else:
return False
def deleteWindow(self):
self.windows.remove(self.sender())
def download(self):
"""download(self)
If a download directory has been configured, a download dialog is
opened and the current list of marked packages is submitted for
retrieval.
If no valid download directory is configured, the user is asked
to configure one in the configuration dialog.
"""
if not self.settings.value("Download directory").isValid():
QMessageBox.information(self, self.tr("Cannot Download Packages"),
self.tr("<qt>You need to configure a download directory "
"before you can download packages. Open the "
"<b>Settings</b> menu and select "
"<b>Configure Browser...</b> to access the "
"browser's configuration."),
QMessageBox.Ok)
elif not self.settings.value("Package preferences").isValid():
QMessageBox.information(self, self.tr("Cannot Download Packages"),
self.tr("<qt>You need to configure preferences for the "
"types of packages you want to download. Open the "
"<b>Settings</b> menu and select "
"<b>Configure Browser...</b> to access the "
"browser's configuration."),
QMessageBox.Ok)
else:
dialog = DownloadDialog(self.settings, self)
dialog.show()
dialog.execute(list(self.marked()))
def editShortcuts(self):
"""editShortcuts(self)
Opens a dialog to allow the user to edit the shortcuts used in the
application.
"""
actions = self.findChildren(QAction)
dialog = ActionEditorDialog(actions, self)
if dialog.exec_() == QDialog.Accepted:
dialog.saveSettings(self.settings, actions)
self.settings.sync()
def listClassifiers(self, url):
"""listClassifiers(self, url)
Returns true if the list of known classifiers from the current
package index can be obtained; otherwise returns false. This
test is used to check whether the URL used for the package index
is valid. (It would be better if we could check for the presence
of a usable XML-RPC server.)
The URL used to obtain a list of classifiers is based on the Python
Package Index URL:
http://www.python.org/pypi?%3Aaction=list_classifiers
The query may possibly be used with other package indexes.
"""
try:
u = urllib2.urlopen(url+u"?%3Aaction=list_classifiers")
line = None
while line != "":
line = u.readline()
qApp.processEvents()
u.close()
except:
return False
return True
def marked(self):
"""marked(self)
This generator returns marked packages one at a time.
"""
for packageName in self.searchModel.markedPackages:
package, versions = self.searchModel.markedPackages[packageName]
for release in package.releases:
if release.version in versions:
name = release.description.metadata["name"]
release_urls = release.description.metadata["release_urls"]
home_url = release.description.metadata["home_page"]
yield (name, release.version, release_urls, home_url)
def openIndex(self):
"""openIndex(self)
Opens a new package index specified by the user in an input dialog,
checking first that the URL given corresponds to the location of
a usable XML-RPC server.
If the URL is invalid, the current package index is not replaced.
"""
gen = self.marked()
try:
gen.next()
if not self.confirmDiscard():
return
except StopIteration:
pass
url = unicode(self.settings.value("Package index").toString())
if not url:
url = u"http://cheeseshop.python.org/pypi"
url, valid = QInputDialog.getText(self, self.tr("Open Index"),
self.tr("Enter the URL of a package index."), QLineEdit.Normal, url)
if not valid:
return
self.settings.setValue("Package index", QVariant(url))
# Fetch a list of classifiers.
if not self.listClassifiers(unicode(url)):
QMessageBox.information(self, self.tr("Package Index Unavailable"),
self.tr("The package index you specified is currently "
"unavailable.\n"
"(I failed to obtain a list of package classifiers.)"))
return
self.fieldComboBox.setEnabled(True)
self.searchLineEdit.setEnabled(True)
self.searchButton.setEnabled(True)
self.reloadListAction.setEnabled(True)
self.filterMarkedAction.setEnabled(True)
self.filterNewAction.setEnabled(True)
self.package_server = pypi.PackageServer(unicode(url))
self.changeServer()
self.searchModel.clear()
self.reloadPackages()
self.windows = []
self.treeView.setEnabled(True)
self.treeView.resizeColumnToContents(0)
self.searchLineEdit.setFocus(Qt.OtherFocusReason)
self.setWindowTitle(self.tr("PyPI Browser - %1").arg(url))
def openManual(self):
"""openManual(self)
Opens the manual supplied with this application in the user's
web browser.
"""
directory = os.path.join(os.path.split(__file__)[0], "Documents")
desktop.open(os.path.join(directory, "Manual"+os.extsep+"html"))
def reloadPackages(self):
"""reloadPackages(self)
Reloads information about the packages in the current package index
while retaining search and marked package information held by the
search model.
"""
# Clear the search model first to prevent old indexes from
# being referenced when they are invalidated in the underlying
# source model.
self.searchModel.reset()
self.packageModel.listPackages()
self.packageModel.load(self.settings)
self.searchModel.search()
def resizeColumns(self):
self.treeView.resizeColumnToContents(0)
def setMarkedFilter(self, enable):
self.searchModel.setMarkedFilter(enable)
#self.treeView.resizeColumnToContents(0)
def setNewFilter(self, enable):
self.searchModel.setNewFilter(enable)
#elf.treeView.resizeColumnToContents(0)
def showInformation(self, index):
if not index.isValid():
return
elif not index.parent().isValid():
self.showPackageInformation(index)
else:
self.showReleaseInformation(index)
def showPackageInformation(self, index):
window = InformationWindow()
window.setPackageInfo(index)
window.show()
self.windows.append(window)
self.connect(window, SIGNAL("closed()"), self.deleteWindow)
def showReleaseInformation(self, index):
window = InformationWindow()
window.setPackageInfo(index.parent(), index)
window.show()
self.windows.append(window)
self.connect(window, SIGNAL("closed()"), self.deleteWindow)
def showResults(self, message):
self.statusBar().showMessage(message)
self.treeView.resizeColumnToContents(0)
def showWaitCursor(self):
self.setCursor(Qt.WaitCursor)
|