diff --git a/setup/gui.py b/setup/gui.py
index 1c9c5aefd9..4b6a86126e 100644
--- a/setup/gui.py
+++ b/setup/gui.py
@@ -58,7 +58,7 @@ class GUI(Command):
for s in sources:
files.append('%s'%s)
manifest = '\n\n%s\n\n'%'\n'.join(sorted(files))
- with open('images.qrc', 'wb') as f:
+ with open('images.qrc', 'w') as f:
f.write(manifest)
finally:
os.chdir(cwd)
diff --git a/setup/publish.py b/setup/publish.py
index d1edbf9d3e..07c589bf3b 100644
--- a/setup/publish.py
+++ b/setup/publish.py
@@ -241,9 +241,9 @@ class Manual(Command):
def serve_manual(self, root):
os.chdir(root)
- from polyglot.http_server import BaseHTTPServer, SimpleHTTPRequestHandler
+ from polyglot.http_server import HTTPServer, SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
- ServerClass = BaseHTTPServer.HTTPServer
+ ServerClass = HTTPServer
Protocol = "HTTP/1.0"
server_address = ('127.0.0.1', 8000)
diff --git a/setup/resources.py b/setup/resources.py
index 3d63baf68a..a859735148 100644
--- a/setup/resources.py
+++ b/setup/resources.py
@@ -90,12 +90,12 @@ class Coffee(Command): # {{{
updated = {}
for arcname in todo:
name = arcname.rpartition('.')[0]
- print ('\t%sCompiling %s'%(time.strftime('[%H:%M:%S] ') if
+ print('\t%sCompiling %s'%(time.strftime('[%H:%M:%S] ') if
timestamp else '', name))
src, sig = src_files[arcname]
js, errors = compile_coffeescript(open(src, 'rb').read(), filename=src)
if errors:
- print ('\n\tCompilation of %s failed'%name)
+ print('\n\tCompilation of %s failed'%name)
for line in errors:
print(line, file=sys.stderr)
if ignore_errors:
@@ -105,8 +105,8 @@ class Coffee(Command): # {{{
else:
if opts.show_js:
self.show_js(js)
- print ('#'*80)
- print ('#'*80)
+ print('#'*80)
+ print('#'*80)
zi = zipfile.ZipInfo()
zi.filename = arcname
zi.date_time = time.localtime()[:6]
@@ -168,7 +168,7 @@ class Kakasi(Command): # {{{
def mkitaiji(self, src, dst):
dic = {}
for line in open(src, "r"):
- line = line.decode("utf-8").strip()
+ line = line.strip()
if line.startswith(';;'): # skip comment
continue
if re.match(r"^$",line):
@@ -182,7 +182,7 @@ class Kakasi(Command): # {{{
def mkkanadict(self, src, dst):
dic = {}
for line in open(src, "r"):
- line = line.decode("utf-8").strip()
+ line = line.strip()
if line.startswith(';;'): # skip comment
continue
if re.match(r"^$",line):
@@ -194,7 +194,7 @@ class Kakasi(Command): # {{{
f.write(msgpack_dumps(dic))
def parsekdict(self, line):
- line = line.decode("utf-8").strip()
+ line = line.strip()
if line.startswith(';;'): # skip comment
return
(yomi, kanji) = line.split(' ')
diff --git a/src/calibre/customize/profiles.py b/src/calibre/customize/profiles.py
index 10057f1cc1..2c7fead69f 100644
--- a/src/calibre/customize/profiles.py
+++ b/src/calibre/customize/profiles.py
@@ -4,9 +4,8 @@ __license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal '
__docformat__ = 'restructuredtext en'
-from itertools import izip
-
from calibre.customize import Plugin as _Plugin
+from polyglot.builtins import zip
FONT_SIZES = [('xx-small', 1),
('x-small', None),
@@ -31,7 +30,7 @@ class Plugin(_Plugin):
fsizes = list(self.fsizes)
self.fkey = list(self.fsizes)
self.fsizes = []
- for (name, num), size in izip(FONT_SIZES, fsizes):
+ for (name, num), size in zip(FONT_SIZES, fsizes):
self.fsizes.append((name, num, float(size)))
self.fnames = dict((name, sz) for name, _, sz in self.fsizes if name)
self.fnums = dict((num, sz) for _, num, sz in self.fsizes if num)
diff --git a/src/calibre/db/view.py b/src/calibre/db/view.py
index d4811434c0..fc773ac680 100644
--- a/src/calibre/db/view.py
+++ b/src/calibre/db/view.py
@@ -9,8 +9,7 @@ __docformat__ = 'restructuredtext en'
import weakref, operator, numbers
from functools import partial
-from itertools import izip, imap
-from polyglot.builtins import map, unicode_type, range
+from polyglot.builtins import map, unicode_type, range, zip
from calibre.ebooks.metadata import title_sort
from calibre.utils.config_base import tweaks, prefs
@@ -374,7 +373,7 @@ class View(object):
self.marked_ids = dict.fromkeys(id_dict, u'true')
else:
# Ensure that all the items in the dict are text
- self.marked_ids = dict(izip(id_dict.iterkeys(), imap(unicode_type,
+ self.marked_ids = dict(zip(id_dict.iterkeys(), map(unicode_type,
id_dict.itervalues())))
# This invalidates all searches in the cache even though the cache may
# be shared by multiple views. This is not ideal, but...
diff --git a/src/calibre/devices/mtp/driver.py b/src/calibre/devices/mtp/driver.py
index 9f6383d546..725f497baa 100644
--- a/src/calibre/devices/mtp/driver.py
+++ b/src/calibre/devices/mtp/driver.py
@@ -9,7 +9,6 @@ __docformat__ = 'restructuredtext en'
import json, traceback, posixpath, importlib, os
from io import BytesIO
-from itertools import izip
from calibre import prints
from calibre.constants import iswindows, numeric_version
@@ -18,7 +17,7 @@ from calibre.devices.mtp.base import debug
from calibre.devices.mtp.defaults import DeviceDefaults
from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory
from calibre.utils.filenames import shorten_components_to
-from polyglot.builtins import unicode_type
+from polyglot.builtins import unicode_type, zip
BASE = importlib.import_module('calibre.devices.mtp.%s.driver'%(
'windows' if iswindows else 'unix')).MTP_DEVICE
@@ -421,7 +420,7 @@ class MTP_DEVICE(BASE):
routing = {fmt:dest for fmt,dest in self.get_pref('rules')}
- for infile, fname, mi in izip(files, names, metadata):
+ for infile, fname, mi in zip(files, names, metadata):
path = self.create_upload_path(prefix, mi, fname, routing)
if path and self.is_folder_ignored(storage, path):
raise MTPInvalidSendPathError('/'.join(path))
@@ -456,7 +455,7 @@ class MTP_DEVICE(BASE):
i, total = 0, len(mtp_files)
self.report_progress(0, _('Adding books to device metadata listing...'))
- for x, mi in izip(mtp_files, metadata):
+ for x, mi in zip(mtp_files, metadata):
mtp_file, bl_idx = x
bl = booklists[bl_idx]
book = Book(mtp_file.storage_id, '/'.join(mtp_file.mtp_relpath),
diff --git a/src/calibre/ebooks/conversion/plugins/html_input.py b/src/calibre/ebooks/conversion/plugins/html_input.py
index e04b82b221..9f257516d9 100644
--- a/src/calibre/ebooks/conversion/plugins/html_input.py
+++ b/src/calibre/ebooks/conversion/plugins/html_input.py
@@ -9,7 +9,6 @@ __docformat__ = 'restructuredtext en'
import re, tempfile, os
from functools import partial
-from itertools import izip
from calibre.constants import islinux, isbsd
from calibre.customize.conversion import (InputFormatPlugin,
@@ -17,7 +16,7 @@ from calibre.customize.conversion import (InputFormatPlugin,
from calibre.utils.localization import get_lang
from calibre.utils.filenames import ascii_filename
from calibre.utils.imghdr import what
-from polyglot.builtins import unicode_type
+from polyglot.builtins import unicode_type, zip
def sanitize_file_name(x):
@@ -216,7 +215,7 @@ class HTMLInput(InputFormatPlugin):
use = titles
if len(titles) > len(set(titles)):
use = headers
- for title, item in izip(use, self.oeb.spine):
+ for title, item in zip(use, self.oeb.spine):
if not item.linear:
continue
toc.add(title, item.href)
diff --git a/src/calibre/ebooks/lit/writer.py b/src/calibre/ebooks/lit/writer.py
index ecc69ab748..fd292f4a03 100644
--- a/src/calibre/ebooks/lit/writer.py
+++ b/src/calibre/ebooks/lit/writer.py
@@ -8,7 +8,7 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift '
from struct import pack
-from itertools import izip, count, chain
+from itertools import count, chain
import io
import time
import random
@@ -30,7 +30,7 @@ import calibre
from calibre import plugins
msdes, msdeserror = plugins['msdes']
import calibre.ebooks.lit.mssha1 as mssha1
-from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes, range
+from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes, range, zip
from polyglot.urllib import urldefrag, unquote
__all__ = ['LitWriter']
@@ -406,7 +406,7 @@ class LitWriter(object):
1, CCHUNK_SIZE, 0x20000, ULL_NEG1, 1))
cchunk = io.BytesIO()
last = 0
- for i, dcount in izip(count(), dcounts):
+ for i, dcount in zip(count(), dcounts):
cchunk.write(decint(last))
cchunk.write(decint(dcount))
cchunk.write(decint(i))
@@ -510,8 +510,7 @@ class LitWriter(object):
data.write(pack(' 1:
ichunk = io.BytesIO()
- for cid, (content, quickref, dcount, name) in izip(count(), ddata):
+ for cid, (content, quickref, dcount, name) in zip(count(), ddata):
dchunk = io.BytesIO()
prev = cid - 1 if cid > 0 else ULL_NEG1
next = cid + 1 if cid < cidmax else ULL_NEG1
diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py
index f05de87a89..ba6bf15571 100644
--- a/src/calibre/ebooks/metadata/__init__.py
+++ b/src/calibre/ebooks/metadata/__init__.py
@@ -135,7 +135,7 @@ def get_title_sort_pat(lang=None):
_ignore_starts = u'\'"'+u''.join(codepoint_to_chr(x) for x in
- range(0x2018, 0x201e)+[0x2032, 0x2033])
+ list(range(0x2018, 0x201e))+[0x2032, 0x2033])
def title_sort(title, order=None, lang=None):
diff --git a/src/calibre/ebooks/mobi/debug/mobi8.py b/src/calibre/ebooks/mobi/debug/mobi8.py
index 5a736c7002..0b17d5817e 100644
--- a/src/calibre/ebooks/mobi/debug/mobi8.py
+++ b/src/calibre/ebooks/mobi/debug/mobi8.py
@@ -9,7 +9,6 @@ __copyright__ = '2012, Kovid Goyal '
__docformat__ = 'restructuredtext en'
import sys, os, struct, textwrap
-from itertools import izip
from calibre import CurrentDir
from calibre.ebooks.mobi.debug.containers import ContainerHeader
@@ -20,6 +19,7 @@ from calibre.ebooks.mobi.utils import read_font_record, decode_tbs, RECORD_SIZE
from calibre.ebooks.mobi.debug import format_bytes
from calibre.ebooks.mobi.reader.headers import NULL_INDEX
from calibre.utils.imghdr import what
+from polyglot.builtins import zip
class FDST(object):
@@ -36,7 +36,7 @@ class FDST(object):
if rest:
raise ValueError('FDST record has trailing data: '
'%s'%format_bytes(rest))
- self.sections = tuple(izip(secs[::2], secs[1::2]))
+ self.sections = tuple(zip(secs[::2], secs[1::2]))
def __str__(self):
ans = ['FDST record']
@@ -96,14 +96,14 @@ class MOBIFile(object):
self.read_tbs()
def print_header(self, f=sys.stdout):
- print (str(self.mf.palmdb).encode('utf-8'), file=f)
- print (file=f)
- print ('Record headers:', file=f)
+ print(str(self.mf.palmdb).encode('utf-8'), file=f)
+ print(file=f)
+ print('Record headers:', file=f)
for i, r in enumerate(self.mf.records):
- print ('%6d. %s'%(i, r.header), file=f)
+ print('%6d. %s'%(i, r.header), file=f)
- print (file=f)
- print (str(self.mf.mobi8_header).encode('utf-8'), file=f)
+ print(file=f)
+ print(str(self.mf.mobi8_header).encode('utf-8'), file=f)
def read_fdst(self):
self.fdst = None
@@ -202,7 +202,7 @@ class MOBIFile(object):
resource_index = len(container.resources)
elif sig == b'\xa0\xa0\xa0\xa0' and len(payload) == 4:
if container is None:
- print ('Found an end of container record with no container, ignoring')
+ print('Found an end of container record with no container, ignoring')
else:
container.resources.append(None)
continue
@@ -287,7 +287,7 @@ class MOBIFile(object):
except:
calculated_bytes = b'failed to calculate tbs bytes'
if calculated_bytes != otbs:
- print ('WARNING: TBS mismatch for record %d'%i)
+ print('WARNING: TBS mismatch for record %d'%i)
desc.append('WARNING: TBS mismatch!')
desc.append('Calculated sequences: %r'%calculated_sequences)
desc.append('')
@@ -340,5 +340,3 @@ def inspect_mobi(mobi_file, ddir):
part.dump(os.path.join(ddir, 'files'))
f.dump_flows(os.path.join(ddir, 'flows'))
-
-
diff --git a/src/calibre/ebooks/mobi/reader/mobi8.py b/src/calibre/ebooks/mobi/reader/mobi8.py
index 23e7c6d46b..74b3447bd7 100644
--- a/src/calibre/ebooks/mobi/reader/mobi8.py
+++ b/src/calibre/ebooks/mobi/reader/mobi8.py
@@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en'
import struct, re, os
from collections import namedtuple
-from itertools import repeat, izip
+from itertools import repeat
from uuid import uuid4
from lxml import etree
@@ -24,7 +24,7 @@ from calibre.ebooks.metadata.toc import TOC
from calibre.ebooks.mobi.utils import read_font_record
from calibre.ebooks.oeb.parse_utils import parse_html
from calibre.ebooks.oeb.base import XPath, XHTML, xml2text
-from polyglot.builtins import range
+from polyglot.builtins import range, zip
from polyglot.urllib import urldefrag
Part = namedtuple('Part',
@@ -125,7 +125,7 @@ class Mobi8Reader(object):
sec_start, num_sections = struct.unpack_from(b'>LL', header, 4)
secs = struct.unpack_from(b'>%dL' % (num_sections*2),
header, sec_start)
- self.flow_table = tuple(izip(secs[::2], secs[1::2]))
+ self.flow_table = tuple(zip(secs[::2], secs[1::2]))
self.files = []
if self.header.skelidx != NULL_INDEX:
diff --git a/src/calibre/ebooks/oeb/display/test-cfi/run_rapydscript.py b/src/calibre/ebooks/oeb/display/test-cfi/run_rapydscript.py
index a31da7fbac..43ec62a2e9 100644
--- a/src/calibre/ebooks/oeb/display/test-cfi/run_rapydscript.py
+++ b/src/calibre/ebooks/oeb/display/test-cfi/run_rapydscript.py
@@ -8,8 +8,8 @@ __copyright__ = '2011, Kovid Goyal '
__docformat__ = 'restructuredtext en'
import os, shutil, tempfile
-import SocketServer
+from polyglot import socketserver
from polyglot.http_server import SimpleHTTPRequestHandler
@@ -29,7 +29,7 @@ def run_devel_server():
js.write(compile_pyj(f.read()).encode('utf-8'))
PORT = 8000
Handler = SimpleHTTPRequestHandler
- httpd = SocketServer.TCPServer(("", PORT), Handler)
+ httpd = socketserver.TCPServer(("", PORT), Handler)
print('Serving CFI test at http://localhost:%d' % PORT)
try:
httpd.serve_forever()
diff --git a/src/calibre/ebooks/oeb/reader.py b/src/calibre/ebooks/oeb/reader.py
index 4ba114924a..4c2bc15fb8 100644
--- a/src/calibre/ebooks/oeb/reader.py
+++ b/src/calibre/ebooks/oeb/reader.py
@@ -8,7 +8,6 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift '
import sys, os, uuid, copy, re, io
-from itertools import izip
from collections import defaultdict
from lxml import etree
@@ -28,7 +27,7 @@ from calibre.utils.localization import get_lang
from calibre.ptempfile import TemporaryDirectory
from calibre.constants import __appname__, __version__
from calibre import guess_type, xml_replace_entities
-from polyglot.builtins import unicode_type
+from polyglot.builtins import unicode_type, zip
from polyglot.urllib import unquote, urldefrag, urlparse
__all__ = ['OEBReader']
@@ -541,7 +540,7 @@ class OEBReader(object):
use = titles
if len(titles) > len(set(titles)):
use = headers
- for title, item in izip(use, self.oeb.spine):
+ for title, item in zip(use, self.oeb.spine):
if not item.linear:
continue
toc.add(title, item.href)
diff --git a/src/calibre/ebooks/pdf/render/fonts.py b/src/calibre/ebooks/pdf/render/fonts.py
index c31996eefc..ef3d31e27e 100644
--- a/src/calibre/ebooks/pdf/render/fonts.py
+++ b/src/calibre/ebooks/pdf/render/fonts.py
@@ -8,10 +8,10 @@ __copyright__ = '2012, Kovid Goyal '
__docformat__ = 'restructuredtext en'
import re
-from itertools import izip, groupby
+from itertools import groupby
from operator import itemgetter
from collections import Counter, OrderedDict
-from polyglot.builtins import map
+from polyglot.builtins import map, zip
from calibre import as_unicode
from calibre.ebooks.pdf.render.common import (Array, String, Stream,
@@ -194,7 +194,7 @@ class Font(object):
def write_widths(self, objects):
glyphs = sorted(self.used_glyphs|{0})
- widths = {g:self.metrics.pdf_scale(w) for g, w in izip(glyphs,
+ widths = {g:self.metrics.pdf_scale(w) for g, w in zip(glyphs,
self.metrics.glyph_widths(glyphs))}
counter = Counter()
for g, w in widths.iteritems():
diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py
index d16d949c43..1f5325d8d5 100644
--- a/src/calibre/gui2/__init__.py
+++ b/src/calibre/gui2/__init__.py
@@ -1288,7 +1288,7 @@ def build_forms(srcdir, info=None, summary=False, check_for_migration=False):
if force_compile or not os.path.exists(compiled_form) or os.stat(form).st_mtime > os.stat(compiled_form).st_mtime:
if not summary:
info('\tCompiling form', form)
- buf = io.BytesIO()
+ buf = io.StringIO()
compileUi(form, buf)
dat = buf.getvalue()
dat = dat.replace('import images_rc', '')
@@ -1298,7 +1298,7 @@ def build_forms(srcdir, info=None, summary=False, check_for_migration=False):
dat = dat.replace('_("d MMM yyyy")', '"d MMM yyyy"')
dat = pat.sub(sub, dat)
- open(compiled_form, 'wb').write(dat)
+ open(compiled_form, 'w').write(dat)
num += 1
if num:
info('Compiled %d forms' % num)
diff --git a/src/calibre/gui2/tag_browser/view.py b/src/calibre/gui2/tag_browser/view.py
index b0aa64a641..5f438a3ba0 100644
--- a/src/calibre/gui2/tag_browser/view.py
+++ b/src/calibre/gui2/tag_browser/view.py
@@ -9,7 +9,6 @@ __docformat__ = 'restructuredtext en'
import os, re
from functools import partial
-from itertools import izip
from PyQt5.Qt import (
QStyledItemDelegate, Qt, QTreeView, pyqtSignal, QSize, QIcon, QApplication,
@@ -25,7 +24,7 @@ from calibre.gui2.tag_browser.model import (TagTreeItem, TAG_SEARCH_STATES,
from calibre.gui2 import config, gprefs, choose_files, pixmap_to_data, rating_font, empty_index
from calibre.utils.icu import sort_key
from calibre.utils.serialize import json_loads
-from polyglot.builtins import unicode_type, range
+from polyglot.builtins import unicode_type, range, zip
class TagDelegate(QStyledItemDelegate): # {{{
@@ -229,7 +228,7 @@ class TagsView(QTreeView): # {{{
expanded_categories.append(category.category_key)
states = [c.tag.state for c in category.child_tags()]
names = [(c.tag.name, c.tag.category) for c in category.child_tags()]
- state_map[category.category_key] = dict(izip(names, states))
+ state_map[category.category_key] = dict(zip(names, states))
return expanded_categories, state_map
def reread_collapse_parameters(self):
diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py
index fe70f3033d..cb93518123 100644
--- a/src/calibre/gui2/tweak_book/widgets.py
+++ b/src/calibre/gui2/tweak_book/widgets.py
@@ -7,7 +7,6 @@ __license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal '
import os, textwrap, unicodedata
-from itertools import izip
from collections import OrderedDict
from PyQt5.Qt import (
@@ -28,7 +27,7 @@ from calibre.gui2.widgets2 import Dialog as BaseDialog, HistoryComboBox
from calibre.utils.icu import primary_sort_key, sort_key, primary_contains, numeric_sort_key
from calibre.utils.matcher import get_char, Matcher
from calibre.gui2.complete2 import EditWithComplete
-from polyglot.builtins import unicode_type
+from polyglot.builtins import unicode_type, zip
ROOT = QModelIndex()
PARAGRAPH_SEPARATOR = '\u2029'
@@ -78,7 +77,7 @@ class InsertTag(Dialog): # {{{
def test(cls):
d = cls()
if d.exec_() == d.Accepted:
- print (d.tag)
+ print(d.tag)
# }}}
@@ -348,7 +347,7 @@ class Results(QWidget):
[(p.setTextFormat(Qt.RichText), p.setTextOption(self.text_option)) for p in prefixes]
self.maxwidth = max([x.size().width() for x in prefixes])
self.results = tuple((prefix, self.make_text(text, positions), text)
- for prefix, (text, positions) in izip(prefixes, results.iteritems()))
+ for prefix, (text, positions) in zip(prefixes, results.iteritems()))
else:
self.results = ()
self.current_result = -1
@@ -476,7 +475,7 @@ class QuickOpen(Dialog):
items = get_items_from_dir(os.getcwdu(), lambda x:not x.endswith('.pyc'))
d = cls(items)
d.exec_()
- print (d.selected_result)
+ print(d.selected_result)
# }}}
@@ -1038,7 +1037,7 @@ class FilterCSS(Dialog): # {{{
def test(cls):
d = cls()
if d.exec_() == d.Accepted:
- print (d.filtered_properties)
+ print(d.filtered_properties)
# }}}
diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py
index 05eba9972b..07aa609ae7 100644
--- a/src/calibre/library/caches.py
+++ b/src/calibre/library/caches.py
@@ -6,8 +6,8 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal '
__docformat__ = 'restructuredtext en'
-import itertools, time, traceback, locale
-from itertools import repeat, izip, imap
+import time, traceback, locale
+from itertools import repeat
from datetime import timedelta
from threading import Thread
@@ -20,7 +20,7 @@ from calibre.db.search import CONTAINS_MATCH, EQUALS_MATCH, REGEXP_MATCH, _match
from calibre.ebooks.metadata import title_sort, author_to_author_sort
from calibre.ebooks.metadata.opf2 import metadata_to_opf
from calibre import prints, force_unicode
-from polyglot.builtins import unicode_type, string_or_bytes
+from polyglot.builtins import map, unicode_type, string_or_bytes, zip
class MetadataBackup(Thread): # {{{
@@ -863,7 +863,7 @@ class ResultCache(SearchQueryParser): # {{{
self.search_restriction_book_count = len(self._map)
return list(self._map)
matches = self.parse(q)
- tmap = list(itertools.repeat(False, len(self._data)))
+ tmap = list(repeat(False, len(self._data)))
for x in matches:
tmap[x] = True
rv = [x for x in self._map if tmap[x]]
@@ -917,7 +917,7 @@ class ResultCache(SearchQueryParser): # {{{
self.marked_ids_dict = dict.fromkeys(id_dict, u'true')
else:
# Ensure that all the items in the dict are text
- self.marked_ids_dict = dict(izip(id_dict.iterkeys(), imap(unicode_type,
+ self.marked_ids_dict = dict(zip(id_dict.iterkeys(), map(unicode_type,
id_dict.itervalues())))
# Set the values in the cache
@@ -1039,7 +1039,7 @@ class ResultCache(SearchQueryParser): # {{{
db.initialize_template_cache()
temp = db.conn.get('SELECT * FROM meta2')
- self._data = list(itertools.repeat(None, temp[-1][0]+2)) if temp else []
+ self._data = list(repeat(None, temp[-1][0]+2)) if temp else []
for r in temp:
self._data[r[0]] = CacheRow(db, self.composites, r,
self.series_col, self.series_sort_col)
@@ -1099,7 +1099,7 @@ class ResultCache(SearchQueryParser): # {{{
if only_ids is None:
self._map.sort(key=keyg)
- tmap = list(itertools.repeat(False, len(self._data)))
+ tmap = list(repeat(False, len(self._data)))
for x in self._map_filtered:
tmap[x] = True
self._map_filtered = [x for x in self._map if tmap[x]]
diff --git a/src/calibre/srv/http_response.py b/src/calibre/srv/http_response.py
index 4d51ea1e21..4873182849 100644
--- a/src/calibre/srv/http_response.py
+++ b/src/calibre/srv/http_response.py
@@ -9,7 +9,7 @@ __copyright__ = '2015, Kovid Goyal '
import os, httplib, hashlib, uuid, struct, repr as reprlib
from collections import namedtuple
from io import BytesIO, DEFAULT_BUFFER_SIZE
-from itertools import chain, repeat, izip_longest
+from itertools import chain, repeat
from operator import itemgetter
from functools import wraps
@@ -32,11 +32,13 @@ MULTIPART_SEPARATOR = uuid.uuid4().hex.decode('ascii')
COMPRESSIBLE_TYPES = {'application/json', 'application/javascript', 'application/xml', 'application/oebps-package+xml'}
if is_py3:
import zlib
+ from itertools import zip_longest
else:
zlib, zlib2_err = plugins['zlib2']
if zlib2_err:
raise RuntimeError('Failed to load the zlib2 module with error: ' + zlib2_err)
del zlib2_err
+ from itertools import izip_longest as zip_longest
def header_list_to_file(buf): # {{{
@@ -709,7 +711,7 @@ class HTTPConnection(HTTPRequest):
size = sum(map(len, range_parts)) + sum(r.size + 4 for r in ranges)
outheaders.set('Content-Length', '%d' % size, replace_all=True)
outheaders.set('Content-Type', 'multipart/byteranges; boundary=' + MULTIPART_SEPARATOR, replace_all=True)
- output.ranges = izip_longest(ranges, range_parts)
+ output.ranges = zip_longest(ranges, range_parts)
request.status_code = httplib.PARTIAL_CONTENT
return output
diff --git a/src/calibre/srv/opts.py b/src/calibre/srv/opts.py
index 1216b06c6f..75d14ab56e 100644
--- a/src/calibre/srv/opts.py
+++ b/src/calibre/srv/opts.py
@@ -7,13 +7,17 @@ __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal '
import errno, os, numbers
-from itertools import izip_longest
from collections import namedtuple, OrderedDict
from operator import attrgetter
from functools import partial
from calibre.constants import config_dir
from calibre.utils.lock import ExclusiveFile
+from polyglot.builtins import is_py3
+if is_py3:
+ from itertools import zip_longest
+else:
+ from itertools import izip_longest as zip_longest
Option = namedtuple('Option', 'name default longdoc shortdoc choices')
@@ -193,7 +197,7 @@ options = []
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
- return izip_longest(*args, fillvalue=fillvalue)
+ return zip_longest(*args, fillvalue=fillvalue)
for shortdoc, name, default, doc in grouper(4, raw_options):
diff --git a/src/calibre/srv/routes.py b/src/calibre/srv/routes.py
index 23fadadd0f..aff8b42d10 100644
--- a/src/calibre/srv/routes.py
+++ b/src/calibre/srv/routes.py
@@ -7,13 +7,12 @@ __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal '
import httplib, sys, inspect, re, time, numbers, json as jsonlib, textwrap
-from itertools import izip
from operator import attrgetter
from calibre.srv.errors import HTTPSimpleResponse, HTTPNotFound, RouteError
from calibre.srv.utils import http_date
from calibre.utils.serialize import msgpack_dumps, json_dumps, MSGPACK_MIME
-from polyglot.builtins import unicode_type, range
+from polyglot.builtins import unicode_type, range, zip
from polyglot.urllib import quote as urlquote
default_methods = frozenset(('HEAD', 'GET'))
@@ -171,7 +170,7 @@ class Route(object):
def matches(self, path):
args_map = self.defaults.copy()
num = 0
- for component, (name, matched) in izip(path, self.matchers):
+ for component, (name, matched) in zip(path, self.matchers):
num += 1
if matched is True:
args_map[name] = component
diff --git a/src/calibre/utils/fonts/sfnt/head.py b/src/calibre/utils/fonts/sfnt/head.py
index f21bd0450e..82ace75472 100644
--- a/src/calibre/utils/fonts/sfnt/head.py
+++ b/src/calibre/utils/fonts/sfnt/head.py
@@ -7,11 +7,11 @@ __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal '
__docformat__ = 'restructuredtext en'
-from itertools import izip
from struct import unpack_from, pack, calcsize
from calibre.utils.fonts.sfnt import UnknownTable, DateTimeProperty, FixedProperty
from calibre.utils.fonts.sfnt.errors import UnsupportedFont
+from polyglot.builtins import zip
class HeadTable(UnknownTable):
@@ -47,7 +47,7 @@ class HeadTable(UnknownTable):
self._fmt = ('>%s'%(''.join(field_types[1::2]))).encode('ascii')
self._fields = field_types[0::2]
- for f, val in izip(self._fields, unpack_from(self._fmt, self.raw)):
+ for f, val in zip(self._fields, unpack_from(self._fmt, self.raw)):
setattr(self, f, val)
def update(self):
@@ -85,7 +85,7 @@ class HorizontalHeader(UnknownTable):
self._fmt = ('>%s'%(''.join(field_types[1::2]))).encode('ascii')
self._fields = field_types[0::2]
- for f, val in izip(self._fields, unpack_from(self._fmt, self.raw)):
+ for f, val in zip(self._fields, unpack_from(self._fmt, self.raw)):
setattr(self, f, val)
raw = hmtx.raw
@@ -149,7 +149,7 @@ class OS2Table(UnknownTable):
self._fmt = ('>%s'%(''.join(field_types[1::2]))).encode('ascii')
self._fields = field_types[0::2]
- for f, val in izip(self._fields, unpack_from(self._fmt, self.raw)):
+ for f, val in zip(self._fields, unpack_from(self._fmt, self.raw)):
setattr(self, f, val)
def zero_fstype(self):
@@ -168,4 +168,3 @@ class PostTable(UnknownTable):
return
(self._version, self._italic_angle, self.underline_position,
self.underline_thickness) = unpack_from(b'>llhh', self.raw)
-
diff --git a/src/calibre/utils/fonts/sfnt/maxp.py b/src/calibre/utils/fonts/sfnt/maxp.py
index 03c8b1793a..a52a7324cb 100644
--- a/src/calibre/utils/fonts/sfnt/maxp.py
+++ b/src/calibre/utils/fonts/sfnt/maxp.py
@@ -7,11 +7,11 @@ __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal '
__docformat__ = 'restructuredtext en'
-from itertools import izip
from struct import unpack_from, pack
from calibre.utils.fonts.sfnt import UnknownTable, FixedProperty
from calibre.utils.fonts.sfnt.errors import UnsupportedFont
+from polyglot.builtins import zip
class MaxpTable(UnknownTable):
@@ -39,12 +39,9 @@ class MaxpTable(UnknownTable):
self._fmt = b'>lH' + b'H'*(len(self.fields)-2)
vals = unpack_from(self._fmt, self.raw)
- for f, val in izip(self.fields, vals):
+ for f, val in zip(self.fields, vals):
setattr(self, f, val)
def update(self):
vals = [getattr(self, f) for f in self._fields]
self.raw = pack(self._fmt, *vals)
-
-
-
diff --git a/src/calibre/utils/matcher.py b/src/calibre/utils/matcher.py
index 091f736c51..4044fc3a13 100644
--- a/src/calibre/utils/matcher.py
+++ b/src/calibre/utils/matcher.py
@@ -13,8 +13,7 @@ from operator import itemgetter
from collections import OrderedDict
from itertools import islice
-from itertools import izip
-from polyglot.builtins import map, unicode_type, range
+from polyglot.builtins import map, unicode_type, range, zip
from calibre import detect_ncpus as cpu_count, as_unicode
from calibre.constants import plugins, filesystem_encoding
@@ -270,7 +269,7 @@ class CScorer(object):
def __call__(self, query):
scores, positions = self.m.calculate_scores(query)
- for score, pos in izip(scores, positions):
+ for score, pos in zip(scores, positions):
yield score, pos
diff --git a/src/calibre/utils/serve_coffee.py b/src/calibre/utils/serve_coffee.py
index 887df3a323..7bab56f4ed 100644
--- a/src/calibre/utils/serve_coffee.py
+++ b/src/calibre/utils/serve_coffee.py
@@ -13,14 +13,11 @@ A coffeescript compiler and a simple web server that automatically serves
coffeescript files as javascript.
'''
import sys, traceback, io
-if sys.version_info.major > 2:
- print('This script is not Python 3 compatible. Run it with Python 2',
- file=sys.stderr)
- raise SystemExit(1)
-
-import time, os, sys, re, SocketServer
+import time, os, sys, re
from threading import Lock, local
-from polyglot.http_server import BaseHTTPServer, SimpleHTTPRequestHandler
+
+from polyglot import socketserver
+from polyglot.http_server import HTTPServer, SimpleHTTPRequestHandler
# Compiler {{{
@@ -255,7 +252,7 @@ class Handler(HTTPRequestHandler): # {{{
# }}}
-class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): # {{{
+class Server(socketserver.ThreadingMixIn, HTTPServer): # {{{
daemon_threads = True
def handle_error(self, request, client_address):
@@ -264,10 +261,10 @@ class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): # {{{
The default is to print a traceback and continue.
"""
- print ('-'*40)
- print ('Exception happened during processing of request', request)
+ print('-'*40)
+ print('Exception happened during processing of request', request)
traceback.print_exc() # XXX But this goes to stderr!
- print ('-'*40)
+ print('-'*40)
# }}}
@@ -323,9 +320,9 @@ def main():
from pygments.lexers import JavascriptLexer
from pygments.formatters import TerminalFormatter
from pygments import highlight
- print (highlight(ans, JavascriptLexer(), TerminalFormatter()))
+ print(highlight(ans, JavascriptLexer(), TerminalFormatter()))
else:
- print (ans.encode(sys.stdout.encoding or 'utf-8'))
+ print(ans.encode(sys.stdout.encoding or 'utf-8'))
else:
serve(port=args.port, host=args.host)
diff --git a/src/calibre/utils/terminal.py b/src/calibre/utils/terminal.py
index b4a28420d2..83e04b8bf0 100644
--- a/src/calibre/utils/terminal.py
+++ b/src/calibre/utils/terminal.py
@@ -8,10 +8,9 @@ __copyright__ = '2012, Kovid Goyal '
__docformat__ = 'restructuredtext en'
import os, sys, re
-from itertools import izip
from calibre.constants import iswindows
-from polyglot.builtins import range
+from polyglot.builtins import range, zip
if iswindows:
import ctypes.wintypes
@@ -31,7 +30,7 @@ def fmt(code):
RATTRIBUTES = dict(
- izip(range(1, 9), (
+ zip(range(1, 9), (
'bold',
'dark',
'',
@@ -46,7 +45,7 @@ ATTRIBUTES = {v:fmt(k) for k, v in RATTRIBUTES.iteritems()}
del ATTRIBUTES['']
RBACKGROUNDS = dict(
- izip(range(41, 48), (
+ zip(range(41, 48), (
'red',
'green',
'yellow',
@@ -59,7 +58,7 @@ RBACKGROUNDS = dict(
BACKGROUNDS = {v:fmt(k) for k, v in RBACKGROUNDS.iteritems()}
RCOLORS = dict(
- izip(range(31, 38), (
+ zip(range(31, 38), (
'red',
'green',
'yellow',
@@ -170,10 +169,10 @@ class Detect(object):
# to use raster fonts (the default). In this case
# rather than failing, write an informative error
# message and the asciized version of the text.
- print ('Non-ASCII text detected. You must set your Console\'s font to'
+ print('Non-ASCII text detected. You must set your Console\'s font to'
' Lucida Console or Consolas or some other TrueType font to see this text', file=self.stream, end=' -- ')
from calibre.utils.filenames import ascii_text
- print (ascii_text(t + text), file=self.stream, end='')
+ print(ascii_text(t + text), file=self.stream, end='')
continue
if not ignore_errors:
raise ctypes.WinError(err)
diff --git a/src/polyglot/http_server.py b/src/polyglot/http_server.py
index e49d886569..319ec9a29c 100644
--- a/src/polyglot/http_server.py
+++ b/src/polyglot/http_server.py
@@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera
from polyglot.builtins import is_py3
if is_py3:
- from http.server import BaseHTTPServer, SimpleHTTPRequestHandler
+ from http.server import HTTPServer, SimpleHTTPRequestHandler
else:
- import BaseHTTPServer # noqa
+ from BaseHTTPServer import HTTPServer # noqa
from SimpleHTTPServer import SimpleHTTPRequestHandler # noqa
diff --git a/src/polyglot/socketserver.py b/src/polyglot/socketserver.py
new file mode 100755
index 0000000000..243cbac5cd
--- /dev/null
+++ b/src/polyglot/socketserver.py
@@ -0,0 +1,10 @@
+#!/usr/bin/env python2
+# vim:fileencoding=utf-8
+# License: GPL v3 Copyright: 2019, Eli Schwartz
+
+from polyglot.builtins import is_py3
+
+if is_py3:
+ from socketserver import TCPServer, ThreadingMixIn # noqa
+else:
+ from SocketServer import TCPServer, ThreadingMixIn # noqa