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 ## 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 ## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or ## 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): class ISBNDBError(Exception):
pass pass
def fetch_metadata(url, max=100): def fetch_metadata(url, max=100, timeout=5.):
books = [] books = []
page_number = 1 page_number = 1
total_results = sys.maxint total_results = sys.maxint
timeout = socket.getdefaulttimeout() timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(2.) socket.setdefaulttimeout(timeout)
try: try:
while len(books) < total_results and max > 0: while len(books) < total_results and max > 0:
try: try:
@ -123,7 +123,7 @@ key is the account key you generate after signing up for a free account from isb
return parser return parser
def create_books(opts, args, logger=None): def create_books(opts, args, logger=None, timeout=5.):
if logger is None: if logger is None:
level = logging.DEBUG if opts.verbose else logging.INFO level = logging.DEBUG if opts.verbose else logging.INFO
logger = logging.getLogger('isbndb') logger = logging.getLogger('isbndb')
@ -137,7 +137,7 @@ def create_books(opts, args, logger=None):
logger.info('ISBNDB query: '+url) 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): def main(args=sys.argv):
parser = option_parser() parser = option_parser()

View File

@ -39,6 +39,7 @@ class ConfigDialog(QDialog, Ui_Dialog):
dirs = settings.value('frequently used directories', QVariant(QStringList())).toStringList() dirs = settings.value('frequently used directories', QVariant(QStringList())).toStringList()
rn = bool(settings.value('use roman numerals for series number', rn = bool(settings.value('use roman numerals for series number',
QVariant(True)).toBool()) QVariant(True)).toBool())
self.timeout.setValue(settings.value('network timeout', QVariant(5)).toInt()[0])
self.roman_numerals.setChecked(rn) self.roman_numerals.setChecked(rn)
self.directory_list.addItems(dirs) self.directory_list.addItems(dirs)
self.connect(self.add_button, SIGNAL('clicked(bool)'), self.add_dir) 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) self.directory_list.takeItem(idx)
def accept(self): 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()) path = qstring_to_unicode(self.location.text())
if not path or not os.path.exists(path) or not os.path.isdir(path): 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.')) 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: else:
self.database_location = os.path.abspath(path) 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())] 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('frequently used directories', QVariant(self.directories))
settings.setValue('use roman numerals for series number', QVariant(self.roman_numerals.isChecked()))
QDialog.accept(self) QDialog.accept(self)

View File

@ -19,8 +19,8 @@
<item row="0" column="0" > <item row="0" column="0" >
<widget class="QStackedWidget" name="stackedWidget" > <widget class="QStackedWidget" name="stackedWidget" >
<widget class="QWidget" name="page" > <widget class="QWidget" name="page" >
<layout class="QGridLayout" > <layout class="QVBoxLayout" >
<item row="0" column="0" > <item>
<layout class="QVBoxLayout" > <layout class="QVBoxLayout" >
<item> <item>
<widget class="QLabel" name="label" > <widget class="QLabel" name="label" >
@ -54,7 +54,50 @@
</item> </item>
</layout> </layout>
</item> </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" > <widget class="QGroupBox" name="dirs_box" >
<property name="title" > <property name="title" >
<string>Frequently used directories</string> <string>Frequently used directories</string>
@ -149,29 +192,19 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="3" column="0" > <item>
<spacer> <spacer>
<property name="orientation" > <property name="orientation" >
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
<property name="sizeHint" > <property name="sizeHint" >
<size> <size>
<width>20</width> <width>372</width>
<height>40</height> <height>16</height>
</size> </size>
</property> </property>
</spacer> </spacer>
</item> </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> </layout>
</widget> </widget>
<widget class="QWidget" name="page_2" /> <widget class="QWidget" name="page_2" />

View File

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

View File

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