diff --git a/src/calibre/devices/usbms/cli.py b/src/calibre/devices/usbms/cli.py index e8b98c8dae..33fd9e752d 100644 --- a/src/calibre/devices/usbms/cli.py +++ b/src/calibre/devices/usbms/cli.py @@ -29,7 +29,7 @@ class CLI(object): def get_file(self, path, outfile, end_session=True): path = self.munge_path(path) with open(path, 'rb') as src: - shutil.copyfileobj(src, outfile, 10*1024*1024) + shutil.copyfileobj(src, outfile) def put_file(self, infile, path, replace_file=False, end_session=True): path = self.munge_path(path) @@ -44,10 +44,8 @@ class CLI(object): d = os.path.dirname(path) if not os.path.exists(d): os.makedirs(d) - dest = open(path, 'wb') - shutil.copyfileobj(infile, dest, 10*1024*1024) - dest.flush() - dest.close() + with open(path, 'wb') as dest: + shutil.copyfileobj(infile, dest) if close: infile.close() diff --git a/src/calibre/ebooks/lrf/output.py b/src/calibre/ebooks/lrf/output.py index f7f85b9c42..cbac0351b2 100644 --- a/src/calibre/ebooks/lrf/output.py +++ b/src/calibre/ebooks/lrf/output.py @@ -38,7 +38,10 @@ class LRFOptions(object): self.use_metadata_cover = True self.output = output self.ignore_tables = opts.linearize_tables - self.base_font_size = opts.base_font_size + if opts.disable_font_rescaling: + self.base_font_size = 0 + else: + self.base_font_size = opts.base_font_size self.blank_after_para = opts.insert_blank_line self.use_spine = True self.font_delta = 0 diff --git a/src/calibre/ebooks/metadata/isbndb.py b/src/calibre/ebooks/metadata/isbndb.py index 38c6e68d3c..b54bab7f98 100644 --- a/src/calibre/ebooks/metadata/isbndb.py +++ b/src/calibre/ebooks/metadata/isbndb.py @@ -4,12 +4,13 @@ __copyright__ = '2008, Kovid Goyal ' Interface to isbndb.com. My key HLLXQX2A. ''' -import sys, re, socket -from urllib import urlopen, quote +import sys, re +from urllib import quote from calibre.utils.config import OptionParser from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.BeautifulSoup import BeautifulStoneSoup +from calibre import browser BASE_URL = 'http://isbndb.com/api/books.xml?access_key=%(key)s&page_number=1&results=subjects,authors,texts&' @@ -20,28 +21,24 @@ def fetch_metadata(url, max=100, timeout=5.): books = [] page_number = 1 total_results = sys.maxint - timeout = socket.getdefaulttimeout() - socket.setdefaulttimeout(timeout) - try: - while len(books) < total_results and max > 0: - try: - raw = urlopen(url).read() - except Exception, err: - raise ISBNDBError('Could not fetch ISBNDB metadata. Error: '+str(err)) - soup = BeautifulStoneSoup(raw, - convertEntities=BeautifulStoneSoup.XML_ENTITIES) - book_list = soup.find('booklist') - if book_list is None: - errmsg = soup.find('errormessage').string - raise ISBNDBError('Error fetching metadata: '+errmsg) - total_results = int(book_list['total_results']) - np = '&page_number=%s&'%(page_number+1) - url = re.sub(r'\&page_number=\d+\&', np, url) - books.extend(book_list.findAll('bookdata')) - max -= 1 - return books - finally: - socket.setdefaulttimeout(timeout) + br = browser() + while len(books) < total_results and max > 0: + try: + raw = br.open(url, timeout=timeout).read() + except Exception, err: + raise ISBNDBError('Could not fetch ISBNDB metadata. Error: '+str(err)) + soup = BeautifulStoneSoup(raw, + convertEntities=BeautifulStoneSoup.XML_ENTITIES) + book_list = soup.find('booklist') + if book_list is None: + errmsg = soup.find('errormessage').string + raise ISBNDBError('Error fetching metadata: '+errmsg) + total_results = int(book_list['total_results']) + np = '&page_number=%s&'%(page_number+1) + url = re.sub(r'\&page_number=\d+\&', np, url) + books.extend(book_list.findAll('bookdata')) + max -= 1 + return books class ISBNDBMetadata(MetaInformation): diff --git a/src/calibre/ebooks/metadata/library_thing.py b/src/calibre/ebooks/metadata/library_thing.py index 0f46c72c75..d10d80bc61 100644 --- a/src/calibre/ebooks/metadata/library_thing.py +++ b/src/calibre/ebooks/metadata/library_thing.py @@ -38,17 +38,16 @@ def cover_from_isbn(isbn, timeout=5., username=None, password=None): global browser if browser is None: browser = _browser() - _timeout = socket.getdefaulttimeout() - socket.setdefaulttimeout(timeout) src = None try: - return browser.open(OPENLIBRARY%isbn).read(), 'jpg' + return browser.open(OPENLIBRARY%isbn, timeout=timeout).read(), 'jpg' except: pass # Cover not found if username and password: login(username, password, force=False) try: - src = browser.open('http://www.librarything.com/isbn/'+isbn).read().decode('utf-8', 'replace') + src = browser.open('http://www.librarything.com/isbn/'+isbn, + timeout=timeout).read().decode('utf-8', 'replace') except Exception, err: if isinstance(getattr(err, 'args', [None])[0], socket.timeout): err = LibraryThingError(_('LibraryThing.com timed out. Try again later.')) @@ -66,8 +65,6 @@ def cover_from_isbn(isbn, timeout=5., username=None, password=None): url = re.sub(r'_S[XY]\d+', '', url['src']) cover_data = browser.open(url).read() return cover_data, url.rpartition('.')[-1] - finally: - socket.setdefaulttimeout(_timeout) def option_parser(): parser = OptionParser(usage=\ diff --git a/src/calibre/gui2/main_window.py b/src/calibre/gui2/main_window.py index 5988ff2026..bdec636d8e 100644 --- a/src/calibre/gui2/main_window.py +++ b/src/calibre/gui2/main_window.py @@ -84,5 +84,5 @@ class MainWindow(QMainWindow): msg = '%s:'%type.__name__ + unicode(str(value), 'utf8', 'replace') error_dialog(self, _('ERROR: Unhandled exception'), msg, det_msg=fe, show=True) - except: + except BaseException: pass diff --git a/src/calibre/utils/ipc/server.py b/src/calibre/utils/ipc/server.py index 448797eba4..f8bed4ce2d 100644 --- a/src/calibre/utils/ipc/server.py +++ b/src/calibre/utils/ipc/server.py @@ -192,8 +192,6 @@ class Server(Thread): if len(self.pool) + len(self.workers) < self.pool_size: try: self.pool.append(self.launch_worker()) - except CriticalError: - raise except Exception: pass diff --git a/src/calibre/web/fetch/simple.py b/src/calibre/web/fetch/simple.py index 238ab343a9..fdf4e81095 100644 --- a/src/calibre/web/fetch/simple.py +++ b/src/calibre/web/fetch/simple.py @@ -105,9 +105,8 @@ class RecursiveFetcher(object): if not os.path.exists(self.base_dir): os.makedirs(self.base_dir) self.log = log - self.default_timeout = socket.getdefaulttimeout() - socket.setdefaulttimeout(options.timeout) self.verbose = options.verbose + self.timeout = options.timeout self.encoding = options.encoding self.browser = options.browser if hasattr(options, 'browser') else browser() self.max_recursions = options.max_recursions @@ -194,7 +193,7 @@ class RecursiveFetcher(object): url = urlparse.urlunparse(purl) with self.browser_lock: try: - with closing(self.browser.open(url)) as f: + with closing(self.browser.open(url, timeout=self.timeout)) as f: data = response(f.read()+f.read()) data.newurl = f.geturl() except urllib2.URLError, err: @@ -204,7 +203,7 @@ class RecursiveFetcher(object): getattr(getattr(err, 'args', [None])[0], 'errno', None) == -2: # Connection reset by peer or Name or service not know self.log.debug('Temporary error, retrying in 1 second') time.sleep(1) - with closing(self.browser.open(url)) as f: + with closing(self.browser.open(url, timeout=self.timeout)) as f: data = response(f.read()+f.read()) data.newurl = f.geturl() else: @@ -450,11 +449,6 @@ class RecursiveFetcher(object): print return res - def __del__(self): - dt = getattr(self, 'default_timeout', None) - if dt is not None: - socket.setdefaulttimeout(dt) - def option_parser(usage=_('%prog URL\n\nWhere URL is for example http://google.com')): parser = OptionParser(usage=usage) parser.add_option('-d', '--base-dir',