Make timeout for network fetches configurable and change default to 5 seconds

This commit is contained in:
Kovid Goyal 2008-01-11 19:57:17 +00:00
parent d7fa576f6f
commit be1c79f0de
6 changed files with 65 additions and 29 deletions

View File

@ -1,4 +1,4 @@
## Copyright (C) 2008 Roger Critchlow
## Copyright (C) 2008 Roger Critchlow <rec@elf.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

View File

@ -29,12 +29,12 @@ BASE_URL = 'http://isbndb.com/api/books.xml?access_key=%(key)s&page_number=1&res
class ISBNDBError(Exception):
pass
def fetch_metadata(url, max=100):
def fetch_metadata(url, max=100, timeout=5.):
books = []
page_number = 1
total_results = sys.maxint
timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(2.)
socket.setdefaulttimeout(timeout)
try:
while len(books) < total_results and max > 0:
try:
@ -123,7 +123,7 @@ key is the account key you generate after signing up for a free account from isb
return parser
def create_books(opts, args, logger=None):
def create_books(opts, args, logger=None, timeout=5.):
if logger is None:
level = logging.DEBUG if opts.verbose else logging.INFO
logger = logging.getLogger('isbndb')
@ -137,7 +137,7 @@ def create_books(opts, args, logger=None):
logger.info('ISBNDB query: '+url)
return [ISBNDBMetadata(book) for book in fetch_metadata(url)]
return [ISBNDBMetadata(book) for book in fetch_metadata(url, timeout=timeout)]
def main(args=sys.argv):
parser = option_parser()

View File

@ -39,6 +39,7 @@ class ConfigDialog(QDialog, Ui_Dialog):
dirs = settings.value('frequently used directories', QVariant(QStringList())).toStringList()
rn = bool(settings.value('use roman numerals for series number',
QVariant(True)).toBool())
self.timeout.setValue(settings.value('network timeout', QVariant(5)).toInt()[0])
self.roman_numerals.setChecked(rn)
self.directory_list.addItems(dirs)
self.connect(self.add_button, SIGNAL('clicked(bool)'), self.add_dir)
@ -62,6 +63,9 @@ class ConfigDialog(QDialog, Ui_Dialog):
self.directory_list.takeItem(idx)
def accept(self):
settings = QSettings()
settings.setValue('use roman numerals for series number', QVariant(self.roman_numerals.isChecked()))
settings.setValue('network timeout', QVariant(self.timeout.value()))
path = qstring_to_unicode(self.location.text())
if not path or not os.path.exists(path) or not os.path.isdir(path):
d = error_dialog(self, _('Invalid database location'), _('Invalid database location ')+path+_('<br>Must be a directory.'))
@ -72,7 +76,5 @@ class ConfigDialog(QDialog, Ui_Dialog):
else:
self.database_location = os.path.abspath(path)
self.directories = [qstring_to_unicode(self.directory_list.item(i).text()) for i in range(self.directory_list.count())]
settings = QSettings()
settings.setValue('frequently used directories', QVariant(self.directories))
settings.setValue('use roman numerals for series number', QVariant(self.roman_numerals.isChecked()))
QDialog.accept(self)

View File

@ -19,8 +19,8 @@
<item row="0" column="0" >
<widget class="QStackedWidget" name="stackedWidget" >
<widget class="QWidget" name="page" >
<layout class="QGridLayout" >
<item row="0" column="0" >
<layout class="QVBoxLayout" >
<item>
<layout class="QVBoxLayout" >
<item>
<widget class="QLabel" name="label" >
@ -54,7 +54,50 @@
</item>
</layout>
</item>
<item row="2" column="0" >
<item>
<widget class="QCheckBox" name="roman_numerals" >
<property name="text" >
<string>Use &amp;Roman numerals for series number</string>
</property>
<property name="checked" >
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" >
<item>
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>Default network &amp;timeout:</string>
</property>
<property name="buddy" >
<cstring>timeout</cstring>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="timeout" >
<property name="toolTip" >
<string>Set the default timeout for network fetches (i.e. anytime libprs500 foes out to the internet to get information)</string>
</property>
<property name="suffix" >
<string> seconds</string>
</property>
<property name="minimum" >
<number>2</number>
</property>
<property name="maximum" >
<number>120</number>
</property>
<property name="value" >
<number>5</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="dirs_box" >
<property name="title" >
<string>Frequently used directories</string>
@ -149,29 +192,19 @@
</layout>
</widget>
</item>
<item row="3" column="0" >
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" >
<size>
<width>20</width>
<height>40</height>
<width>372</width>
<height>16</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0" >
<widget class="QCheckBox" name="roman_numerals" >
<property name="text" >
<string>Use &amp;Roman numerals for series number</string>
</property>
<property name="checked" >
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2" />

View File

@ -75,11 +75,12 @@ class Matches(QAbstractTableModel):
class FetchMetadata(QDialog, Ui_FetchMetadata):
def __init__(self, parent, isbn, title, author, publisher):
def __init__(self, parent, isbn, title, author, publisher, timeout):
QDialog.__init__(self, parent)
Ui_FetchMetadata.__init__(self)
self.setupUi(self)
self.timeout = timeout
QObject.connect(self.fetch, SIGNAL('clicked()'), self.fetch_metadata)
self.key.setText(QSettings().value('isbndb.com key', QVariant('')).toString())
@ -126,7 +127,7 @@ class FetchMetadata(QDialog, Ui_FetchMetadata):
handler.setFormatter(logging.Formatter('[%(levelname)s] %(filename)s:%(lineno)s: %(message)s'))
self.logger.addHandler(handler)
books = create_books(opts, args, self.logger)
books = create_books(opts, args, self.logger, self.timeout)
self.model = Matches(books)

View File

@ -18,7 +18,7 @@ add/remove formats
'''
import os
from PyQt4.QtCore import SIGNAL, QObject, QCoreApplication, Qt
from PyQt4.QtCore import SIGNAL, QObject, QCoreApplication, Qt, QVariant, QSettings
from PyQt4.QtGui import QPixmap, QListWidgetItem, QErrorMessage, QDialog
@ -149,7 +149,7 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog):
self.fetch_cover)
QObject.connect(self.tag_editor_button, SIGNAL('clicked()'),
self.edit_tags)
self.timeout = float(QSettings().value('network timeout', QVariant(5)).toInt()[0])
self.title.setText(db.title(row))
isbn = db.isbn(self.id)
if not isbn:
@ -234,7 +234,7 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog):
QCoreApplication.instance().processEvents()
try:
login(d.username(), d.password(), force=False)
cover_data = cover_from_isbn(isbn)[0]
cover_data = cover_from_isbn(isbn, timeout=self.timeout)[0]
pix = QPixmap()
pix.loadFromData(cover_data)
@ -260,7 +260,7 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog):
author = qstring_to_unicode(self.authors.text()).split(',')[0]
publisher = qstring_to_unicode(self.publisher.text())
if isbn or title or author or publisher:
d = FetchMetadata(self, isbn, title, author, publisher)
d = FetchMetadata(self, isbn, title, author, publisher, self.timeout)
d.exec_()
if d.result() == QDialog.Accepted:
book = d.selected_book()