Various fixes for the last py3 merge

This commit is contained in:
Kovid Goyal 2019-07-25 08:52:35 +05:30
parent c84d019ef7
commit b8b5c4e14e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
11 changed files with 40 additions and 38 deletions

View File

@ -9,8 +9,7 @@ __docformat__ = 'restructuredtext en'
import os, re import os, re
from calibre.utils.date import isoformat, now from calibre.utils.date import isoformat, now
from calibre import guess_type from calibre import guess_type
from polyglot.builtins import iteritems, filter from polyglot.builtins import iteritems
filter
def meta_info_to_oeb_metadata(mi, m, log, override_input_metadata=False): def meta_info_to_oeb_metadata(mi, m, log, override_input_metadata=False):

View File

@ -60,7 +60,7 @@ def unipmlcode(char):
val = ord(char.encode('cp1252')) val = ord(char.encode('cp1252'))
if val in A_CHARS: if val in A_CHARS:
return '\\a%i' % val return '\\a%i' % val
except: except Exception:
pass pass
val = ord(char) val = ord(char)
if val in U_CHARS: if val in U_CHARS:

View File

@ -190,7 +190,7 @@ class PMLMLizer(object):
text = text.replace(r'\Q="%s"' % unused, '') text = text.replace(r'\Q="%s"' % unused, '')
# Remove \Cn tags that are within \x and \Xn tags # Remove \Cn tags that are within \x and \Xn tags
text = re.sub(unicode_type(r'(?msu)(?P<t>\\(x|X[0-4]))(?P<a>.*?)(?P<c>\\C[0-4]\s*=\s*"[^"]*")(?P<b>.*?)(?P=t)'), r'\g<t>\g<a>\g<b>\g<t>', text) text = re.sub(r'(?msu)(?P<t>\\(x|X[0-4]))(?P<a>.*?)(?P<c>\\C[0-4]\s*=\s*"[^"]*")(?P<b>.*?)(?P=t)', r'\g<t>\g<a>\g<b>\g<t>', text)
# Replace bad characters. # Replace bad characters.
text = text.replace('\xc2', '') text = text.replace('\xc2', '')
@ -326,7 +326,7 @@ class PMLMLizer(object):
for s in STYLES: for s in STYLES:
style_tag = s[1].get(style[s[0]], None) style_tag = s[1].get(style[s[0]], None)
if style_tag and style_tag not in tag_stack+tags: if style_tag and style_tag not in tag_stack+tags:
text.append('r\%s' % style_tag) text.append(r'\%s' % style_tag)
tags.append(style_tag) tags.append(style_tag)
# margin left # margin left

View File

@ -122,7 +122,7 @@ class RBWriter(object):
for item in manifest: for item in manifest:
if item.media_type in OEB_RASTER_IMAGES: if item.media_type in OEB_RASTER_IMAGES:
try: try:
data = '' data = b''
im = Image.open(io.BytesIO(item.data)).convert('L') im = Image.open(io.BytesIO(item.data)).convert('L')
data = io.BytesIO() data = io.BytesIO()

View File

@ -1,10 +1,10 @@
from __future__ import absolute_import, division, print_function, unicode_literals from __future__ import absolute_import, division, print_function, unicode_literals
def save_to_file(text, filename): def save_to_file(text, filename):
f = open(filename, 'wt') with open(filename, 'wb') as f:
f.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />') f.write(b'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />')
f.write(text.encode('utf-8')) f.write(text.encode('utf-8'))
f.close()
uids = {} uids = {}

View File

@ -21,7 +21,7 @@ class tokenDelimitatorStart():
pass pass
def toRTF(self): def toRTF(self):
return b'{' return '{'
def __repr__(self): def __repr__(self):
return '{' return '{'
@ -33,7 +33,7 @@ class tokenDelimitatorEnd():
pass pass
def toRTF(self): def toRTF(self):
return b'}' return '}'
def __repr__(self): def __repr__(self):
return '}' return '}'

View File

@ -86,10 +86,10 @@ def txt2rtf(text):
if val == 160: if val == 160:
buf.write(r'\~') buf.write(r'\~')
elif val <= 127: elif val <= 127:
buf.write(unicode_type(x)) buf.write(x)
else: else:
# python2 and ur'\u' does not work # python2 and ur'\u' does not work
c = unicode_type('\\u{0:d}?'.format(val)) c = '\\u{0:d}?'.format(val)
buf.write(c) buf.write(c)
return buf.getvalue() return buf.getvalue()
@ -175,8 +175,8 @@ class RTFMLizer(object):
src = item.href src = item.href
try: try:
data, width, height = self.image_to_hexstring(item.data) data, width, height = self.image_to_hexstring(item.data)
except: except Exception:
self.log.warn('Image %s is corrupted, ignoring'%item.href) self.log.exception('Image %s is corrupted, ignoring'%item.href)
repl = '\n\n' repl = '\n\n'
else: else:
repl = '\n\n{\\*\\shppict{\\pict\\jpegblip\\picw%i\\pich%i \n%s\n}}\n\n' % (width, height, data) repl = '\n\n{\\*\\shppict{\\pict\\jpegblip\\picw%i\\pich%i \n%s\n}}\n\n' % (width, height, data)
@ -188,8 +188,8 @@ class RTFMLizer(object):
width, height = identify(data)[1:] width, height = identify(data)[1:]
raw_hex = '' raw_hex = ''
for char in data: for char in bytearray(data):
raw_hex += hex(ord(char)).replace('0x', '').rjust(2, '0') raw_hex += hex(char).replace('0x', '').rjust(2, '0')
# Images must be broken up so that they are no longer than 129 chars # Images must be broken up so that they are no longer than 129 chars
# per line # per line
@ -202,7 +202,7 @@ class RTFMLizer(object):
col += 1 col += 1
hex_string += char hex_string += char
return (hex_string, width, height) return hex_string, width, height
def clean_text(self, text): def clean_text(self, text):
# Remove excessive newlines # Remove excessive newlines

View File

@ -245,7 +245,7 @@ class CBDialog(QDialog):
geom = gprefs.get('cover_browser_dialog_geometry', bytearray('')) geom = gprefs.get('cover_browser_dialog_geometry', bytearray(''))
geom = QByteArray(geom) geom = QByteArray(geom)
if not self.restoreGeometry(geom): if not self.restoreGeometry(geom):
h, w = available_height()-60, available_width()//1.5 h, w = available_height()-60, int(available_width()/1.5)
self.resize(w, h) self.resize(w, h)
self.action_fs_toggle = a = QAction(self) self.action_fs_toggle = a = QAction(self)
self.addAction(a) self.addAction(a)
@ -459,7 +459,7 @@ def test():
app = QApplication([]) app = QApplication([])
w = QMainWindow() w = QMainWindow()
cf = CoverFlow() cf = CoverFlow()
cf.resize(available_width()//1.5, available_height()-60) cf.resize(int(available_width()/1.5), available_height()-60)
w.resize(cf.size()+QSize(30, 20)) w.resize(cf.size()+QSize(30, 20))
model = DummyImageList() model = DummyImageList()
cf.setImages(model) cf.setImages(model)
@ -480,7 +480,7 @@ if __name__ == '__main__':
app = QApplication([]) app = QApplication([])
w = QMainWindow() w = QMainWindow()
cf = CoverFlow() cf = CoverFlow()
cf.resize(available_width()//1.5, available_height()-60) cf.resize(int(available_width()/1.5), available_height()-60)
w.resize(cf.size()+QSize(30, 20)) w.resize(cf.size()+QSize(30, 20))
path = sys.argv[1] path = sys.argv[1]
model = FileSystemImages(sys.argv[1]) model = FileSystemImages(sys.argv[1])

View File

@ -731,7 +731,7 @@ def populate_metadata_page(layout, db, book_id, bulk=False, two_column=False, pa
count = len(cols) count = len(cols)
layout_rows_for_comments = 9 layout_rows_for_comments = 9
if two_column: if two_column:
turnover_point = ((count-comments_not_in_tweak+1) + comments_in_tweak*(layout_rows_for_comments-1))/2 turnover_point = ((count-comments_not_in_tweak+1) + int(comments_in_tweak*(layout_rows_for_comments-1))/2)
else: else:
# Avoid problems with multi-line widgets # Avoid problems with multi-line widgets
turnover_point = count + 1000 turnover_point = count + 1000
@ -758,7 +758,7 @@ def populate_metadata_page(layout, db, book_id, bulk=False, two_column=False, pa
column = 0 column = 0
row = max_row row = max_row
base_row = row base_row = row
turnover_point = row + (comments_not_in_tweak * layout_rows_for_comments)/2 turnover_point = row + int((comments_not_in_tweak * layout_rows_for_comments)/2)
comments_not_in_tweak = 0 comments_not_in_tweak = 0
l = QGridLayout() l = QGridLayout()

View File

@ -11,12 +11,12 @@ import sys
import time import time
from threading import Thread from threading import Thread
from polyglot.builtins import reraise, unicode_type, string_or_bytes
from PyQt5.Qt import QEventLoop from PyQt5.Qt import QEventLoop
from calibre import force_unicode from calibre import force_unicode
from calibre.constants import filesystem_encoding, preferred_encoding, DEBUG from calibre.constants import DEBUG, filesystem_encoding, ispy3, preferred_encoding
from calibre.utils.config import dynamic from calibre.utils.config import dynamic
from polyglot.builtins import getenv, reraise, string_or_bytes, unicode_type
def dialog_name(name, title): def dialog_name(name, title):
@ -29,20 +29,20 @@ def get_winid(widget=None):
def detect_desktop_environment(): def detect_desktop_environment():
de = os.environ.get('XDG_CURRENT_DESKTOP') de = getenv('XDG_CURRENT_DESKTOP')
if de: if de:
return de.decode('utf-8', 'replace').upper().split(':', 1)[0] return de.upper().split(':', 1)[0]
if os.environ.get('KDE_FULL_SESSION') == 'true': if getenv('KDE_FULL_SESSION') == 'true':
return 'KDE' return 'KDE'
if os.environ.get('GNOME_DESKTOP_SESSION_ID'): if getenv('GNOME_DESKTOP_SESSION_ID'):
return 'GNOME' return 'GNOME'
ds = os.environ.get('DESKTOP_SESSION') ds = getenv('DESKTOP_SESSION')
if ds and ds.upper() in {b'GNOME', b'XFCE'}: if ds and ds.upper() in {'GNOME', 'XFCE'}:
return ds.decode('utf-8').upper() return ds.upper()
def is_executable_present(name): def is_executable_present(name):
PATH = os.environ.get('PATH') or b'' PATH = getenv('PATH') or ''
for path in PATH.split(os.pathsep): for path in PATH.split(os.pathsep):
if os.access(os.path.join(path, name), os.X_OK): if os.access(os.path.join(path, name), os.X_OK):
return True return True
@ -107,7 +107,7 @@ def decode_output(raw):
def run(cmd): def run(cmd):
from calibre.gui2 import sanitize_env_vars from calibre.gui2 import sanitize_env_vars
ecmd = list(map(encode_arg, cmd)) ecmd = cmd if ispy3 else list(map(encode_arg, cmd))
if DEBUG: if DEBUG:
try: try:
print(ecmd) print(ecmd)
@ -125,7 +125,10 @@ def run(cmd):
def kdialog_supports_desktopfile(): def kdialog_supports_desktopfile():
ans = getattr(kdialog_supports_desktopfile, 'ans', None) ans = getattr(kdialog_supports_desktopfile, 'ans', None)
if ans is None: if ans is None:
raw = subprocess.check_output(['kdialog', '--help']) try:
raw = subprocess.check_output(['kdialog', '--help'])
except EnvironmentError:
raw = b'--desktopfile'
ans = kdialog_supports_desktopfile.ans = b'--desktopfile' in raw ans = kdialog_supports_desktopfile.ans = b'--desktopfile' in raw
return ans return ans

View File

@ -112,7 +112,7 @@ class Customize(QFrame):
Qt.Key_Shift, Qt.Key_Control, Qt.Key_Alt, Qt.Key_Meta, Qt.Key_Shift, Qt.Key_Control, Qt.Key_Alt, Qt.Key_Meta,
Qt.Key_AltGr, Qt.Key_CapsLock, Qt.Key_NumLock, Qt.Key_ScrollLock): Qt.Key_AltGr, Qt.Key_CapsLock, Qt.Key_NumLock, Qt.Key_ScrollLock):
return QWidget.keyPressEvent(self, ev) return QWidget.keyPressEvent(self, ev)
sequence = QKeySequence(code|(int(ev.modifiers())&~Qt.KeypadModifier)) sequence = QKeySequence(code|(int(ev.modifiers()) & (~Qt.KeypadModifier)))
setattr(self, 'shortcut%d'%which, sequence) setattr(self, 'shortcut%d'%which, sequence)
self.clear_button(which) self.clear_button(which)
self.capture = 0 self.capture = 0
@ -237,7 +237,7 @@ class Shortcuts(QAbstractListModel):
def get_match(self, event_or_sequence, ignore=tuple()): def get_match(self, event_or_sequence, ignore=tuple()):
q = event_or_sequence q = event_or_sequence
if isinstance(q, QKeyEvent): if isinstance(q, QKeyEvent):
q = QKeySequence(q.key()|(int(q.modifiers())&~Qt.KeypadModifier)) q = QKeySequence(q.key()|(int(q.modifiers()) & (~Qt.KeypadModifier)))
for key in self.order: for key in self.order:
if key not in ignore: if key not in ignore:
for seq in self.get_sequences(key): for seq in self.get_sequences(key):