py3: More unicode_literals and str() porting

This commit is contained in:
Kovid Goyal 2019-05-25 13:10:48 +05:30
parent 297f8fc41a
commit d7cb0787fd
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
7 changed files with 42 additions and 40 deletions

View File

@ -1,10 +1,7 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'

View File

@ -1,9 +1,8 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'

View File

@ -1,4 +1,5 @@
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2011, Timothy Legge <timlegge@gmail.com> and Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2011, Timothy Legge <timlegge@gmail.com> and Kovid Goyal <kovid@kovidgoyal.net>'

View File

@ -1,3 +1,4 @@
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2010-2012, , Timothy Legge <timlegge at gmail.com> and David Forrester <davidfor@internode.on.net>' __copyright__ = '2010-2012, , Timothy Legge <timlegge at gmail.com> and David Forrester <davidfor@internode.on.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
@ -5,7 +6,7 @@ __docformat__ = 'restructuredtext en'
import os, time, sys import os, time, sys
from functools import cmp_to_key from functools import cmp_to_key
from calibre.constants import preferred_encoding, DEBUG from calibre.constants import preferred_encoding, DEBUG, ispy3
from calibre import isbytestring, force_unicode from calibre import isbytestring, force_unicode
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
@ -112,6 +113,9 @@ class Book(Book_):
return super(Book,self).__unicode__() + u"\n" + ans return super(Book,self).__unicode__() + u"\n" + ans
if ispy3:
__str__ = __unicode__
class ImageWrapper(object): class ImageWrapper(object):

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import print_function, division from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2010-2012, Timothy Legge <timlegge@gmail.com>, Kovid Goyal <kovid@kovidgoyal.net> and David Forrester <davidfor@internode.on.net>' __copyright__ = '2010-2012, Timothy Legge <timlegge@gmail.com>, Kovid Goyal <kovid@kovidgoyal.net> and David Forrester <davidfor@internode.on.net>'
@ -109,7 +109,7 @@ class KOBO(USBMS):
SUPPORTS_ANNOTATIONS = True SUPPORTS_ANNOTATIONS = True
# "kepubs" do not have an extension. The name looks like a GUID. Using an empty string seems to work. # "kepubs" do not have an extension. The name looks like a GUID. Using an empty string seems to work.
VIRTUAL_BOOK_EXTENSIONS = frozenset(['kobo', '']) VIRTUAL_BOOK_EXTENSIONS = frozenset(('kobo', ''))
EXTRA_CUSTOMIZATION_MESSAGE = [ EXTRA_CUSTOMIZATION_MESSAGE = [
_('The Kobo supports several collections including ')+ 'Read, Closed, Im_Reading. ' + _( _('The Kobo supports several collections including ')+ 'Read, Closed, Im_Reading. ' + _(
@ -197,9 +197,9 @@ class KOBO(USBMS):
# Determine the firmware version # Determine the firmware version
try: try:
with lopen(self.normalize_path(self._main_prefix + '.kobo/version'), 'rb') as f: with lopen(self.normalize_path(self._main_prefix + '.kobo/version'), 'rb') as f:
fwversion = f.readline().split(',')[2] fwversion = f.readline().split(b',')[2]
fwversion = tuple((int(x) for x in fwversion.split('.'))) fwversion = tuple((int(x) for x in fwversion.split(b'.')))
except: except Exception:
debug_print("Kobo::get_firmware_version - didn't get firmware version from file'") debug_print("Kobo::get_firmware_version - didn't get firmware version from file'")
fwversion = (0,0,0) fwversion = (0,0,0)
@ -372,7 +372,7 @@ class KOBO(USBMS):
try: try:
cursor.execute(query) cursor.execute(query)
except Exception as e: except Exception as e:
err = str(e) err = unicode_type(e)
if not (any_in(err, '___ExpirationStatus', 'FavouritesIndex', 'Accessibility', 'IsDownloaded')): if not (any_in(err, '___ExpirationStatus', 'FavouritesIndex', 'Accessibility', 'IsDownloaded')):
raise raise
query= ('select Title, Attribution, DateCreated, ContentID, MimeType, ContentType, ' query= ('select Title, Attribution, DateCreated, ContentID, MimeType, ContentType, '
@ -478,13 +478,13 @@ class KOBO(USBMS):
cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\', ___PercentRead=0, ___ExpirationStatus=3 ' cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\', ___PercentRead=0, ___ExpirationStatus=3 '
'where BookID is Null and ContentID =?',t) 'where BookID is Null and ContentID =?',t)
except Exception as e: except Exception as e:
if 'no such column' not in str(e): if 'no such column' not in unicode_type(e):
raise raise
try: try:
cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\', ___PercentRead=0 ' cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\', ___PercentRead=0 '
'where BookID is Null and ContentID =?',t) 'where BookID is Null and ContentID =?',t)
except Exception as e: except Exception as e:
if 'no such column' not in str(e): if 'no such column' not in unicode_type(e):
raise raise
cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\' ' cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\' '
'where BookID is Null and ContentID =?',t) 'where BookID is Null and ContentID =?',t)
@ -524,7 +524,7 @@ class KOBO(USBMS):
path = self.normalize_path(path) path = self.normalize_path(path)
# print "Delete file normalized path: " + path # print "Delete file normalized path: " + path
extension = os.path.splitext(path)[1] extension = os.path.splitext(path)[1]
ContentType = self.get_content_type_from_extension(extension) if extension != '' else self.get_content_type_from_path(path) ContentType = self.get_content_type_from_extension(extension) if extension else self.get_content_type_from_path(path)
ContentID = self.contentid_from_path(path, ContentType) ContentID = self.contentid_from_path(path, ContentType)
@ -550,7 +550,7 @@ class KOBO(USBMS):
try: try:
# print "removed" # print "removed"
os.removedirs(os.path.dirname(path)) os.removedirs(os.path.dirname(path))
except: except Exception:
pass pass
self.report_progress(1.0, _('Removing books from device...')) self.report_progress(1.0, _('Removing books from device...'))
@ -827,7 +827,7 @@ class KOBO(USBMS):
cursor.execute(query) cursor.execute(query)
except Exception as e: except Exception as e:
debug_print(' Database Exception: Unable to reset Shortlist list') debug_print(' Database Exception: Unable to reset Shortlist list')
if 'no such column' not in str(e): if 'no such column' not in unicode_type(e):
raise raise
finally: finally:
cursor.close() cursor.close()
@ -841,7 +841,7 @@ class KOBO(USBMS):
cursor.execute('update content set FavouritesIndex=1 where BookID is Null and ContentID = ?', t) cursor.execute('update content set FavouritesIndex=1 where BookID is Null and ContentID = ?', t)
except Exception as e: except Exception as e:
debug_print(' Database Exception: Unable set book as Shortlist') debug_print(' Database Exception: Unable set book as Shortlist')
if 'no such column' not in str(e): if 'no such column' not in unicode_type(e):
raise raise
finally: finally:
cursor.close() cursor.close()
@ -905,17 +905,17 @@ class KOBO(USBMS):
book.device_collections.append(category) book.device_collections.append(category)
extension = os.path.splitext(book.path)[1] extension = os.path.splitext(book.path)[1]
ContentType = self.get_content_type_from_extension(extension) if extension != '' else self.get_content_type_from_path(book.path) ContentType = self.get_content_type_from_extension(extension) if extension else self.get_content_type_from_path(book.path)
ContentID = self.contentid_from_path(book.path, ContentType) ContentID = self.contentid_from_path(book.path, ContentType)
if category in list(readstatuslist.keys()): if category in tuple(readstatuslist):
# Manage ReadStatus # Manage ReadStatus
self.set_readstatus(connection, ContentID, readstatuslist.get(category)) self.set_readstatus(connection, ContentID, readstatuslist.get(category))
elif category == 'Shortlist' and self.dbversion >= 14: elif category == 'Shortlist' and self.dbversion >= 14:
# Manage FavouritesIndex/Shortlist # Manage FavouritesIndex/Shortlist
self.set_favouritesindex(connection, ContentID) self.set_favouritesindex(connection, ContentID)
elif category in list(accessibilitylist.keys()): elif category in tuple(accessibilitylist):
# Do not manage the Accessibility List # Do not manage the Accessibility List
pass pass
else: # No collections else: # No collections
@ -1181,7 +1181,7 @@ class KOBO(USBMS):
with closing(self.device_database_connection()) as connection: with closing(self.device_database_connection()) as connection:
for id in path_map: for id in path_map:
extension = os.path.splitext(path_map[id])[1] extension = os.path.splitext(path_map[id])[1]
ContentType = self.get_content_type_from_extension(extension) if extension != '' else self.get_content_type_from_path(path_map[id]) ContentType = self.get_content_type_from_extension(extension) if extension else self.get_content_type_from_path(path_map[id])
ContentID = self.contentid_from_path(path_map[id], ContentType) ContentID = self.contentid_from_path(path_map[id], ContentType)
debug_print("get_annotations - ContentID: ", ContentID, "ContentType: ", ContentType) debug_print("get_annotations - ContentID: ", ContentID, "ContentType: ", ContentType)
@ -1769,7 +1769,7 @@ class KOBOTOUCH(KOBO):
debug_print('KoboTouch:update_booklist - book file does not exist. ContentID="%s"'%ContentID) debug_print('KoboTouch:update_booklist - book file does not exist. ContentID="%s"'%ContentID)
except Exception as e: except Exception as e:
debug_print("KoboTouch:update_booklist - exception creating book: '%s'"%str(e)) debug_print("KoboTouch:update_booklist - exception creating book: '%s'"%unicode_type(e))
debug_print(" prefix: ", prefix, "lpath: ", lpath, "title: ", title, "authors: ", authors, debug_print(" prefix: ", prefix, "lpath: ", lpath, "title: ", title, "authors: ", authors,
"MimeType: ", MimeType, "DateCreated: ", DateCreated, "ContentType: ", ContentType, "ImageID: ", ImageID) "MimeType: ", MimeType, "DateCreated: ", DateCreated, "ContentType: ", ContentType, "ImageID: ", ImageID)
raise raise
@ -1909,7 +1909,7 @@ class KOBOTOUCH(KOBO):
try: try:
cursor.execute(query) cursor.execute(query)
except Exception as e: except Exception as e:
err = str(e) err = unicode_type(e)
if not (any_in(err, '___ExpirationStatus', 'FavouritesIndex', 'Accessibility', 'IsDownloaded', 'Series', 'ExternalId')): if not (any_in(err, '___ExpirationStatus', 'FavouritesIndex', 'Accessibility', 'IsDownloaded', 'Series', 'ExternalId')):
raise raise
query= ('SELECT Title, Attribution, DateCreated, ContentID, MimeType, ContentType, ' query= ('SELECT Title, Attribution, DateCreated, ContentID, MimeType, ContentType, '
@ -2022,7 +2022,7 @@ class KOBOTOUCH(KOBO):
if len(ImageID) > 0: if len(ImageID) > 0:
path = self.images_path(prefix, ImageID) path = self.images_path(prefix, ImageID)
for ending in self.cover_file_endings().keys(): for ending in self.cover_file_endings():
fpath = path + ending fpath = path + ending
if os.path.exists(fpath): if os.path.exists(fpath):
if show_debug: if show_debug:
@ -2118,7 +2118,7 @@ class KOBOTOUCH(KOBO):
cursor.close() cursor.close()
except Exception as e: except Exception as e:
debug_print('KoboTouch:upload_books - Exception: %s'%str(e)) debug_print('KoboTouch:upload_books - Exception: %s'%unicode_type(e))
return result return result
@ -2217,7 +2217,7 @@ class KOBOTOUCH(KOBO):
debug_print("KoboTouch:commit_container: removing container temp files.") debug_print("KoboTouch:commit_container: removing container temp files.")
try: try:
shutil.rmtree(container.root) shutil.rmtree(container.root)
except: except Exception:
pass pass
def delete_via_sql(self, ContentID, ContentType): def delete_via_sql(self, ContentID, ContentType):
@ -2262,7 +2262,7 @@ class KOBOTOUCH(KOBO):
debug_print('KoboTouch:delete_via_sql: finished SQL') debug_print('KoboTouch:delete_via_sql: finished SQL')
debug_print('KoboTouch:delete_via_sql: After SQL, no exception') debug_print('KoboTouch:delete_via_sql: After SQL, no exception')
except Exception as e: except Exception as e:
debug_print('KoboTouch:delete_via_sql - Database Exception: %s'%str(e)) debug_print('KoboTouch:delete_via_sql - Database Exception: %s'%unicode_type(e))
debug_print('KoboTouch:delete_via_sql: imageId="%s"'%imageId) debug_print('KoboTouch:delete_via_sql: imageId="%s"'%imageId)
if imageId is None: if imageId is None:
@ -2287,7 +2287,7 @@ class KOBOTOUCH(KOBO):
try: try:
os.removedirs(os.path.dirname(path)) os.removedirs(os.path.dirname(path))
except: except Exception:
pass pass
def contentid_from_path(self, path, ContentType): def contentid_from_path(self, path, ContentType):
@ -2301,7 +2301,7 @@ class KOBOTOUCH(KOBO):
ContentID = os.path.splitext(path)[0] ContentID = os.path.splitext(path)[0]
# Remove the prefix on the file. it could be either # Remove the prefix on the file. it could be either
ContentID = ContentID.replace(self._main_prefix, '') ContentID = ContentID.replace(self._main_prefix, '')
elif extension == '': elif not extension:
ContentID = path ContentID = path
ContentID = ContentID.replace(self._main_prefix + self.normalize_path('.kobo/kepub/'), '') ContentID = ContentID.replace(self._main_prefix + self.normalize_path('.kobo/kepub/'), '')
else: else:
@ -2432,7 +2432,7 @@ class KOBOTOUCH(KOBO):
if book.contentID is None: if book.contentID is None:
debug_print(' Do not know ContentID - Title="%s, Authors=%s"'%(book.title, book.author)) debug_print(' Do not know ContentID - Title="%s, Authors=%s"'%(book.title, book.author))
extension = os.path.splitext(book.path)[1] extension = os.path.splitext(book.path)[1]
ContentType = self.get_content_type_from_extension(extension) if extension != '' else self.get_content_type_from_path(book.path) ContentType = self.get_content_type_from_extension(extension) if extension else self.get_content_type_from_path(book.path)
book.contentID = self.contentid_from_path(book.path, ContentType) book.contentID = self.contentid_from_path(book.path, ContentType)
if category in self.ignore_collections_names: if category in self.ignore_collections_names:
@ -2446,7 +2446,7 @@ class KOBOTOUCH(KOBO):
debug_print(' Setting bookshelf on device') debug_print(' Setting bookshelf on device')
self.set_bookshelf(connection, book, category) self.set_bookshelf(connection, book, category)
category_added = True category_added = True
elif category in list(readstatuslist.keys()): elif category in readstatuslist:
debug_print("KoboTouch:update_device_database_collections - about to set_readstatus - category='%s'"%(category, )) debug_print("KoboTouch:update_device_database_collections - about to set_readstatus - category='%s'"%(category, ))
# Manage ReadStatus # Manage ReadStatus
self.set_readstatus(connection, book.contentID, readstatuslist.get(category)) self.set_readstatus(connection, book.contentID, readstatuslist.get(category))
@ -2461,7 +2461,7 @@ class KOBOTOUCH(KOBO):
debug_print(' and about to set it - %s'%book.title) debug_print(' and about to set it - %s'%book.title)
self.set_favouritesindex(connection, book.contentID) self.set_favouritesindex(connection, book.contentID)
category_added = True category_added = True
elif category in list(accessibilitylist.keys()): elif category in accessibilitylist:
# Do not manage the Accessibility List # Do not manage the Accessibility List
pass pass
@ -2560,7 +2560,7 @@ class KOBOTOUCH(KOBO):
try: try:
self._upload_cover(path, filename, metadata, filepath, self.upload_grayscale, self.keep_cover_aspect) self._upload_cover(path, filename, metadata, filepath, self.upload_grayscale, self.keep_cover_aspect)
except Exception as e: except Exception as e:
debug_print('KoboTouch: FAILED to upload cover=%s Exception=%s'%(filepath, str(e))) debug_print('KoboTouch: FAILED to upload cover=%s Exception=%s'%(filepath, unicode_type(e)))
def imageid_from_contentid(self, ContentID): def imageid_from_contentid(self, ContentID):
ImageID = ContentID.replace('/', '_') ImageID = ContentID.replace('/', '_')
@ -2638,7 +2638,7 @@ class KOBOTOUCH(KOBO):
# Get ContentID for Selected Book # Get ContentID for Selected Book
extension = os.path.splitext(filepath)[1] extension = os.path.splitext(filepath)[1]
ContentType = self.get_content_type_from_extension(extension) if extension != '' else self.get_content_type_from_path(filepath) ContentType = self.get_content_type_from_extension(extension) if extension else self.get_content_type_from_path(filepath)
ContentID = self.contentid_from_path(filepath, ContentType) ContentID = self.contentid_from_path(filepath, ContentType)
try: try:
@ -2694,7 +2694,7 @@ class KOBOTOUCH(KOBO):
f.write(data) f.write(data)
fsync(f) fsync(f)
except Exception as e: except Exception as e:
err = str(e) err = unicode_type(e)
debug_print("KoboTouch:_upload_cover - Exception string: %s"%err) debug_print("KoboTouch:_upload_cover - Exception string: %s"%err)
raise raise
@ -3008,7 +3008,7 @@ class KOBOTOUCH(KOBO):
plugboard = None plugboard = None
if self.plugboard_func and not series_only: if self.plugboard_func and not series_only:
if book.contentID.endswith('.kepub.epub') or os.path.splitext(book.contentID)[1] == "": if book.contentID.endswith('.kepub.epub') or not os.path.splitext(book.contentID)[1]:
extension = 'kepub' extension = 'kepub'
else: else:
extension = os.path.splitext(book.contentID)[1][1:] extension = os.path.splitext(book.contentID)[1][1:]

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'