From 6ad22b392bfc7038d3e643194baaad32f1dce85f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 13 Mar 2019 09:21:07 +0530 Subject: [PATCH] pep8 and misc fixes --- src/calibre/db/lazy.py | 3 ++ src/calibre/devices/udisks.py | 1 + src/calibre/devices/utils.py | 4 +-- src/calibre/ebooks/chardet.py | 1 + .../ebooks/conversion/plugins/epub_input.py | 4 ++- src/calibre/ebooks/conversion/utils.py | 8 +++-- src/calibre/ebooks/docx/container.py | 3 +- src/calibre/ebooks/docx/tables.py | 1 + src/calibre/ebooks/htmlz/oeb2html.py | 2 +- src/calibre/ebooks/lrf/lrfparser.py | 1 + src/calibre/ebooks/metadata/mobi.py | 4 +-- src/calibre/ebooks/metadata/rtf.py | 2 +- src/calibre/ebooks/pdb/ereader/reader132.py | 4 +-- src/calibre/ebooks/txt/markdownml.py | 4 +-- src/calibre/gui2/dbus_export/menu2.py | 5 +++- src/calibre/gui2/dbus_export/utils.py | 2 ++ .../gui2/device_drivers/configwidget.py | 6 +++- src/calibre/gui2/dialogs/authors_edit.py | 1 + .../gui2/dialogs/device_category_editor.py | 8 ++--- src/calibre/gui2/dialogs/opml.py | 7 +++-- src/calibre/gui2/dialogs/plugin_updater.py | 2 +- src/calibre/gui2/dialogs/progress.py | 1 + src/calibre/gui2/dnd.py | 3 +- src/calibre/gui2/init.py | 29 ++++++++++--------- src/calibre/gui2/library/models.py | 6 ++-- src/calibre/gui2/library/views.py | 8 ++--- src/calibre/gui2/proceed.py | 1 + src/calibre/gui2/search_restriction_mixin.py | 2 +- src/calibre/gui2/tweak_book/search.py | 2 +- src/calibre/gui2/wizard/send_email.py | 10 +++---- src/calibre/library/save_to_disk.py | 21 +++++++------- src/calibre/spell/dictionary.py | 2 +- src/calibre/utils/fonts/scanner.py | 2 ++ src/calibre/utils/html2text.py | 6 ++-- src/calibre/utils/open_with/osx.py | 2 +- src/calibre/utils/soupparser.py | 2 +- 36 files changed, 99 insertions(+), 71 deletions(-) diff --git a/src/calibre/db/lazy.py b/src/calibre/db/lazy.py index 79c552d8db..ffa71f8612 100644 --- a/src/calibre/db/lazy.py +++ b/src/calibre/db/lazy.py @@ -106,6 +106,7 @@ class FormatsList(MutableBase, MutableSequence): # }}} + # Lazy metadata getters {{{ ga = object.__getattribute__ sa = object.__setattr__ @@ -223,6 +224,7 @@ def has_cover_getter(dbref, book_id, cache): cache['has_cover'] = ret = _('Yes') if db.field_for('cover', book_id, default_value=False) else '' return ret + fmt_custom = lambda x:list(x) if isinstance(x, tuple) else x @@ -276,6 +278,7 @@ def user_categories_getter(proxy_metadata): ret = cache['user_categories'] = db.user_categories_for_books((book_id,), {book_id:proxy_metadata})[book_id] return ret + getters = { 'title':simple_getter('title', _('Unknown')), 'title_sort':simple_getter('sort', _('Unknown')), diff --git a/src/calibre/devices/udisks.py b/src/calibre/devices/udisks.py index 7240444f2a..1d90d0f779 100644 --- a/src/calibre/devices/udisks.py +++ b/src/calibre/devices/udisks.py @@ -10,6 +10,7 @@ import os, re from polyglot.builtins import unicode_type + def node_mountpoint(node): def de_mangle(raw): diff --git a/src/calibre/devices/utils.py b/src/calibre/devices/utils.py index 506527c668..2240874393 100644 --- a/src/calibre/devices/utils.py +++ b/src/calibre/devices/utils.py @@ -59,11 +59,11 @@ def build_template_regexp(template): try: template = template.rpartition('/')[2] - return re.compile(re.sub('{([^}]*)}', f, template) + '([_\d]*$)') + return re.compile(re.sub('{([^}]*)}', f, template) + r'([_\d]*$)') except: prints(u'Failed to parse template: %r'%template) template = u'{title} - {authors}' - return re.compile(re.sub('{([^}]*)}', f, template) + '([_\d]*$)') + return re.compile(re.sub('{([^}]*)}', f, template) + r'([_\d]*$)') def create_upload_path(mdata, fname, template, sanitize, diff --git a/src/calibre/ebooks/chardet.py b/src/calibre/ebooks/chardet.py index 2d0dd7efe2..6d08cb61fd 100644 --- a/src/calibre/ebooks/chardet.py +++ b/src/calibre/ebooks/chardet.py @@ -61,6 +61,7 @@ def substitute_entites(raw): from calibre import xml_entity_to_unicode return ENTITY_PATTERN.sub(xml_entity_to_unicode, raw) + _CHARSET_ALIASES = {"macintosh" : "mac-roman", "x-sjis" : "shift-jis"} diff --git a/src/calibre/ebooks/conversion/plugins/epub_input.py b/src/calibre/ebooks/conversion/plugins/epub_input.py index a0216ac9f3..a94e02150e 100644 --- a/src/calibre/ebooks/conversion/plugins/epub_input.py +++ b/src/calibre/ebooks/conversion/plugins/epub_input.py @@ -368,7 +368,9 @@ class EPUBInput(InputFormatPlugin): def add_from_li(li, parent): href = text = None for x in li.iterchildren(XHTML('a'), XHTML('span')): - text = etree.tostring(x, method='text', encoding=unicode_type, with_tail=False).strip() or ' '.join(x.xpath('descendant-or-self::*/@title')).strip() + text = etree.tostring( + x, method='text', encoding=unicode_type, with_tail=False).strip() or ' '.join( + x.xpath('descendant-or-self::*/@title')).strip() href = x.get('href') if href: if href.startswith('#'): diff --git a/src/calibre/ebooks/conversion/utils.py b/src/calibre/ebooks/conversion/utils.py index cdae26aed9..e7f3b99dbb 100644 --- a/src/calibre/ebooks/conversion/utils.py +++ b/src/calibre/ebooks/conversion/utils.py @@ -309,9 +309,11 @@ class HeuristicProcessor(object): if float(self.chapters_with_title) / float(hits) > .5: title_req = True strict_title = False - self.log.debug(unicode_type(type_name)+" had "+unicode_type(hits)+" hits - "+unicode_type(self.chapters_no_title)+" chapters with no title, "+ - unicode_type(self.chapters_with_title)+" chapters with titles, "+ - unicode_type(float(self.chapters_with_title) / float(hits))+" percent. ") + self.log.debug( + unicode_type(type_name)+" had "+unicode_type(hits)+ + " hits - "+unicode_type(self.chapters_no_title)+" chapters with no title, "+ + unicode_type(self.chapters_with_title)+" chapters with titles, "+ + unicode_type(float(self.chapters_with_title) / float(hits))+" percent. ") if type_name == 'common': analysis_result.append([chapter_type, n_lookahead_req, strict_title, ignorecase, title_req, log_message, type_name]) elif self.min_chapters <= hits < max_chapters or self.min_chapters < 3 > hits: diff --git a/src/calibre/ebooks/docx/container.py b/src/calibre/ebooks/docx/container.py index 8ba8b2ff83..06913667fc 100644 --- a/src/calibre/ebooks/docx/container.py +++ b/src/calibre/ebooks/docx/container.py @@ -264,6 +264,7 @@ class DOCX(object): except EnvironmentError: pass + if __name__ == '__main__': d = DOCX(sys.argv[-1], extract=False) - print (d.metadata) + print(d.metadata) diff --git a/src/calibre/ebooks/docx/tables.py b/src/calibre/ebooks/docx/tables.py index b2cb4b2346..056869aa6a 100644 --- a/src/calibre/ebooks/docx/tables.py +++ b/src/calibre/ebooks/docx/tables.py @@ -95,6 +95,7 @@ def read_indent(parent, dest, XPath, get): ans = _read_width(cs, get) setattr(dest, 'indent', ans) + border_edges = ('left', 'top', 'right', 'bottom', 'insideH', 'insideV') diff --git a/src/calibre/ebooks/htmlz/oeb2html.py b/src/calibre/ebooks/htmlz/oeb2html.py index 5797d31ae4..7a77f4c64e 100644 --- a/src/calibre/ebooks/htmlz/oeb2html.py +++ b/src/calibre/ebooks/htmlz/oeb2html.py @@ -272,7 +272,7 @@ class OEB2HTMLInlineCSSizer(OEB2HTML): # as a page break and remove all other page break types that might be set. style_a = 'page-break-before: always; %s' % re.sub('page-break-[^:]+:[^;]+;?', '', style_a) # Remove unnecessary spaces. - style_a = re.sub('\s{2,}', ' ', style_a).strip() + style_a = re.sub(r'\s{2,}', ' ', style_a).strip() tags.append(tag) # Remove attributes we won't want. diff --git a/src/calibre/ebooks/lrf/lrfparser.py b/src/calibre/ebooks/lrf/lrfparser.py index 1760de269a..4915bba235 100644 --- a/src/calibre/ebooks/lrf/lrfparser.py +++ b/src/calibre/ebooks/lrf/lrfparser.py @@ -168,5 +168,6 @@ def main(args=sys.argv, logger=None): logger.info(_('LRS written to ')+opts.out) return 0 + if __name__ == '__main__': sys.exit(main()) diff --git a/src/calibre/ebooks/metadata/mobi.py b/src/calibre/ebooks/metadata/mobi.py index 26dcbe4793..3c64bf07c4 100644 --- a/src/calibre/ebooks/metadata/mobi.py +++ b/src/calibre/ebooks/metadata/mobi.py @@ -335,8 +335,8 @@ class MetadataUpdater(object): self.original_exth_records.pop(rec[0]) if self.type != "BOOKMOBI": - raise MobiError("Setting metadata only supported for MOBI files of type 'BOOK'.\n" - "\tThis is a %r file of type %r" % (self.type[0:4], self.type[4:8])) + raise MobiError("Setting metadata only supported for MOBI files of type 'BOOK'.\n" + "\tThis is a %r file of type %r" % (self.type[0:4], self.type[4:8])) recs = [] added_501 = False diff --git a/src/calibre/ebooks/metadata/rtf.py b/src/calibre/ebooks/metadata/rtf.py index 31b36ac850..084f0d8ae9 100644 --- a/src/calibre/ebooks/metadata/rtf.py +++ b/src/calibre/ebooks/metadata/rtf.py @@ -19,7 +19,7 @@ publisher_pat = re.compile(r'\{\\info.*?\{\\manager(.*?)(? 0: html += '

%s

' % _('Footnotes') footnoteids = re.findall( - '\w+(?=\x00)', self.section_data(self.header_record.footnote_offset).decode('cp1252' if self.encoding is None else self.encoding)) + '\\w+(?=\x00)', self.section_data(self.header_record.footnote_offset).decode('cp1252' if self.encoding is None else self.encoding)) for fid, i in enumerate(range(self.header_record.footnote_offset + 1, self.header_record.footnote_offset + self.header_record.footnote_count)): self.log.debug('Extracting footnote page %i' % i) if fid < len(footnoteids): @@ -141,7 +141,7 @@ class Reader132(FormatReader): if self.header_record.sidebar_count > 0: html += '

%s

' % _('Sidebar') sidebarids = re.findall( - '\w+(?=\x00)', self.section_data(self.header_record.sidebar_offset).decode('cp1252' if self.encoding is None else self.encoding)) + '\\w+(?=\x00)', self.section_data(self.header_record.sidebar_offset).decode('cp1252' if self.encoding is None else self.encoding)) for sid, i in enumerate(range(self.header_record.sidebar_offset + 1, self.header_record.sidebar_offset + self.header_record.sidebar_count)): self.log.debug('Extracting sidebar page %i' % i) if sid < len(sidebarids): diff --git a/src/calibre/ebooks/txt/markdownml.py b/src/calibre/ebooks/txt/markdownml.py index 7cef2b734b..6f121de4c1 100644 --- a/src/calibre/ebooks/txt/markdownml.py +++ b/src/calibre/ebooks/txt/markdownml.py @@ -77,8 +77,8 @@ class MarkdownMLizer(OEB2HTML): text = re.sub('(?msu)\n{7,}', '\n' * 6, text) # Remove blank lines at beginning and end of document. - text = re.sub('^\s*', '', text) - text = re.sub('\s*$', '\n\n', text) + text = re.sub(r'^\s*', '', text) + text = re.sub(r'\s*$', '\n\n', text) return text diff --git a/src/calibre/gui2/dbus_export/menu2.py b/src/calibre/gui2/dbus_export/menu2.py index 08ee421557..a110f3b94b 100644 --- a/src/calibre/gui2/dbus_export/menu2.py +++ b/src/calibre/gui2/dbus_export/menu2.py @@ -23,7 +23,10 @@ from polyglot.builtins import unicode_type def add_window_properties_for_menu(widget, object_path, bus): op = unicode_type(object_path) - set_X_window_properties(widget.effectiveWinId(), _UNITY_OBJECT_PATH=op, _GTK_UNIQUE_BUS_NAME=unicode_type(bus.get_unique_name()), _GTK_MENUBAR_OBJECT_PATH=op) + set_X_window_properties( + widget.effectiveWinId(), _UNITY_OBJECT_PATH=op, + _GTK_UNIQUE_BUS_NAME=unicode_type(bus.get_unique_name()), + _GTK_MENUBAR_OBJECT_PATH=op) class DBusMenu(QObject): diff --git a/src/calibre/gui2/dbus_export/utils.py b/src/calibre/gui2/dbus_export/utils.py index 3a47eda50f..0c5bd16d65 100644 --- a/src/calibre/gui2/dbus_export/utils.py +++ b/src/calibre/gui2/dbus_export/utils.py @@ -20,6 +20,7 @@ def log(*args, **kw): print('DBusExport:', *args, **kw) kw['file'].flush() + from calibre.ptempfile import PersistentTemporaryDirectory @@ -60,6 +61,7 @@ class IconCache(object): # dir to decide whether it should look for new icons in the theme dir. os.utime(self.icon_theme_path, None) + _icon_cache = None diff --git a/src/calibre/gui2/device_drivers/configwidget.py b/src/calibre/gui2/device_drivers/configwidget.py index 38c2b55e1e..61458c0871 100644 --- a/src/calibre/gui2/device_drivers/configwidget.py +++ b/src/calibre/gui2/device_drivers/configwidget.py @@ -136,7 +136,11 @@ class ConfigWidget(QWidget, Ui_ConfigWidget): self.columns.setCurrentRow(idx+1) def format_map(self): - formats = [unicode_type(self.columns.item(i).data(Qt.UserRole) or '') for i in range(self.columns.count()) if self.columns.item(i).checkState()==Qt.Checked] + formats = [ + unicode_type(self.columns.item(i).data(Qt.UserRole) or '') + for i in range(self.columns.count()) + if self.columns.item(i).checkState()==Qt.Checked + ] return formats def use_subdirs(self): diff --git a/src/calibre/gui2/dialogs/authors_edit.py b/src/calibre/gui2/dialogs/authors_edit.py index e020d071af..5cc479b348 100644 --- a/src/calibre/gui2/dialogs/authors_edit.py +++ b/src/calibre/gui2/dialogs/authors_edit.py @@ -197,6 +197,7 @@ class AuthorsEdit(QDialog): authors[la] = author self.author.setText('') + if __name__ == '__main__': app = QApplication([]) d = AuthorsEdit(['kovid goyal', 'divok layog', 'other author'], ['kovid goyal', 'other author']) diff --git a/src/calibre/gui2/dialogs/device_category_editor.py b/src/calibre/gui2/dialogs/device_category_editor.py index 9cb9cd1230..38f17849ae 100644 --- a/src/calibre/gui2/dialogs/device_category_editor.py +++ b/src/calibre/gui2/dialogs/device_category_editor.py @@ -85,10 +85,10 @@ class DeviceCategoryEditor(QDialog, Ui_DeviceCategoryEditor): def finish_editing(self, item): if not item.text(): - error_dialog(self, _('Item is blank'), - _('An item cannot be set to nothing. Delete it instead.')).exec_() - item.setText(item.previous_text()) - return + error_dialog(self, _('Item is blank'), + _('An item cannot be set to nothing. Delete it instead.')).exec_() + item.setText(item.previous_text()) + return if item.text() != item.initial_text(): id_ = int(item.data(Qt.UserRole)) self.to_rename[id_] = unicode_type(item.text()) diff --git a/src/calibre/gui2/dialogs/opml.py b/src/calibre/gui2/dialogs/opml.py index 0f35b81b3d..7f7efc8a22 100644 --- a/src/calibre/gui2/dialogs/opml.py +++ b/src/calibre/gui2/dialogs/opml.py @@ -139,10 +139,11 @@ class ImportOPML(QDialog): QDialog.accept(self) + if __name__ == '__main__': import sys for group in import_opml(open(sys.argv[-1], 'rb').read()): - print (group.title) + print(group.title) for title, url in group.feeds: - print ('\t%s - %s' % (title, url)) - print () + print('\t%s - %s' % (title, url)) + print() diff --git a/src/calibre/gui2/dialogs/plugin_updater.py b/src/calibre/gui2/dialogs/plugin_updater.py index 18355b16b4..34616b56c8 100644 --- a/src/calibre/gui2/dialogs/plugin_updater.py +++ b/src/calibre/gui2/dialogs/plugin_updater.py @@ -843,7 +843,7 @@ class PluginUpdaterDialog(SizePersistedDialog): if heading_node.text_content().lower().find('version history') != -1: div_node = spoiler_node.xpath('div')[0] text = html.tostring(div_node, method='html', encoding=unicode_type) - return re.sub('', '
', text) + return re.sub(r'', '
', text) except: if DEBUG: prints('======= MobileRead Parse Error =======') diff --git a/src/calibre/gui2/dialogs/progress.py b/src/calibre/gui2/dialogs/progress.py index bc50921431..239f8d4c88 100644 --- a/src/calibre/gui2/dialogs/progress.py +++ b/src/calibre/gui2/dialogs/progress.py @@ -169,6 +169,7 @@ class BlockingBusy(QDialog): def reject(self): pass # Cannot cancel this dialog + if __name__ == '__main__': from PyQt5.Qt import QTimer app = QApplication([]) diff --git a/src/calibre/gui2/dnd.py b/src/calibre/gui2/dnd.py index 2ff0668146..59dd40f5e4 100644 --- a/src/calibre/gui2/dnd.py +++ b/src/calibre/gui2/dnd.py @@ -28,6 +28,7 @@ def image_extensions(): image_extensions.ans = [bytes(x).decode('utf-8') for x in QImageReader.supportedImageFormats()] return image_extensions.ans + # This is present for compatibility with old plugins, do not use IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'gif', 'png', 'bmp'] @@ -181,7 +182,7 @@ def dnd_has_extension(md, extensions, allow_all_extensions=False): f = unicode_type(f) raw = data_as_string(f, md) prints(f, len(raw), repr(raw[:300]), '\n') - print () + print() if has_firefox_ext(md, extensions): return True urls = urls_from_md(md) diff --git a/src/calibre/gui2/init.py b/src/calibre/gui2/init.py index e03c35b7a2..769781fbcd 100644 --- a/src/calibre/gui2/init.py +++ b/src/calibre/gui2/init.py @@ -7,23 +7,24 @@ __docformat__ = 'restructuredtext en' import functools -from PyQt5.Qt import (Qt, QApplication, QStackedWidget, QMenu, QTimer, - QSizePolicy, QStatusBar, QLabel, QFont, QAction, QTabBar, QStyle, - QVBoxLayout, QWidget, QSplitter, QToolButton, QIcon, QPainter, QStyleOption) +from PyQt5.Qt import ( + QAction, QApplication, QFont, QIcon, QLabel, QMenu, QPainter, QSizePolicy, + QSplitter, QStackedWidget, QStatusBar, QStyle, QStyleOption, Qt, QTabBar, QTimer, + QToolButton, QVBoxLayout, QWidget +) +from calibre.constants import __appname__, get_version, isosx +from calibre.customize.ui import find_plugin +from calibre.gui2 import config, error_dialog, gprefs, is_widescreen, open_url +from calibre.gui2.book_details import BookDetails +from calibre.gui2.layout_menu import LayoutMenu +from calibre.gui2.library.alternate_views import GridView +from calibre.gui2.library.views import BooksView, DeviceBooksView +from calibre.gui2.notify import get_notifier +from calibre.gui2.tag_browser.ui import TagBrowserWidget +from calibre.gui2.widgets import LayoutButton, Splitter from calibre.utils.config import prefs from calibre.utils.icu import sort_key -from calibre.constants import (__appname__, preferred_encoding, - get_version) -from calibre.gui2 import config, is_widescreen, gprefs, error_dialog, open_url -from calibre.gui2.library.views import BooksView, DeviceBooksView -from calibre.gui2.library.alternate_views import GridView -from calibre.gui2.widgets import Splitter, LayoutButton -from calibre.gui2.tag_browser.ui import TagBrowserWidget -from calibre.gui2.book_details import BookDetails -from calibre.gui2.notify import get_notifier -from calibre.gui2.layout_menu import LayoutMenu -from calibre.customize.ui import find_plugin from calibre.utils.localization import localize_website_link from polyglot.builtins import unicode_type diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index e5d3f028ec..c11d8be97c 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -778,7 +778,7 @@ class BooksModel(QAbstractTableModel): # {{{ def func(idx): val = fffunc(field_obj, idfunc(idx), default_value=0) or 0 - if val is 0: + if val == 0: return None ans = u'%.1f' % (val * sz_mult) return (u'<0.1' if ans == u'0.0' else ans) @@ -1027,8 +1027,8 @@ class BooksModel(QAbstractTableModel): # {{{ return (self.headers[self.column_map[section]]) return None if DEBUG and role == Qt.ToolTipRole and orientation == Qt.Vertical: - col = self.db.field_metadata['uuid']['rec_index'] - return (_('This book\'s UUID is "{0}"').format(self.db.data[section][col])) + col = self.db.field_metadata['uuid']['rec_index'] + return (_('This book\'s UUID is "{0}"').format(self.db.data[section][col])) if role == Qt.DisplayRole: # orientation is vertical return (section+1) diff --git a/src/calibre/gui2/library/views.py b/src/calibre/gui2/library/views.py index dca3c9e050..484084094f 100644 --- a/src/calibre/gui2/library/views.py +++ b/src/calibre/gui2/library/views.py @@ -436,10 +436,10 @@ class BooksView(QTableView): # {{{ m = ans.addMenu(_('Change text alignment for %s') % name) al = self._model.alignment_map.get(col, 'left') for x, t in (('left', _('Left')), ('right', _('Right')), ('center', _('Center'))): - a = m.addAction(t, partial(handler, action='align_'+x)) - if al == x: - a.setCheckable(True) - a.setChecked(True) + a = m.addAction(t, partial(handler, action='align_'+x)) + if al == x: + a.setCheckable(True) + a.setChecked(True) if not isinstance(view, DeviceBooksView): col_font = self._model.styled_columns.get(col) m = ans.addMenu(_('Change font style for %s') % name) diff --git a/src/calibre/gui2/proceed.py b/src/calibre/gui2/proceed.py index d06211c6a0..c7d99846cc 100644 --- a/src/calibre/gui2/proceed.py +++ b/src/calibre/gui2/proceed.py @@ -418,5 +418,6 @@ def main(): QTimer.singleShot(10, doit) app.exec_() + if __name__ == '__main__': main() diff --git a/src/calibre/gui2/search_restriction_mixin.py b/src/calibre/gui2/search_restriction_mixin.py index b6c4bd5c7d..cfb6d57409 100644 --- a/src/calibre/gui2/search_restriction_mixin.py +++ b/src/calibre/gui2/search_restriction_mixin.py @@ -302,7 +302,7 @@ class CreateVirtualLibrary(QDialog): # {{{ _('The search found no books, so the virtual library ' 'will be empty. Do you really want to use that search?'), default_yes=False): - return + return self.library_name = n self.library_search = v diff --git a/src/calibre/gui2/tweak_book/search.py b/src/calibre/gui2/tweak_book/search.py index b45f566b2c..fb4cc483df 100644 --- a/src/calibre/gui2/tweak_book/search.py +++ b/src/calibre/gui2/tweak_book/search.py @@ -34,7 +34,7 @@ from calibre.gui2.tweak_book.widgets import BusyCursor from calibre.gui2.widgets2 import FlowLayout, HistoryComboBox from calibre.utils.icu import primary_contains from calibre.ebooks.conversion.search_replace import REGEX_FLAGS, compile_regular_expression -src/calibre/gui2/tweak_book/preferences.py +from polyglot.builtins import unicode_type # The search panel {{{ diff --git a/src/calibre/gui2/wizard/send_email.py b/src/calibre/gui2/wizard/send_email.py index b77cb68c70..e3e60a0fd4 100644 --- a/src/calibre/gui2/wizard/send_email.py +++ b/src/calibre/gui2/wizard/send_email.py @@ -281,11 +281,11 @@ class SendEmail(QWidget, Ui_Form): _('You must either set both the username and password for ' 'the mail server or no username and no password at all.')).exec_() return False - if not (username and password) and not question_dialog(self, - _('Are you sure?'), - _('No username and password set for mailserver. Most ' - ' mailservers need a username and password. Are you sure?')): - return False + if not (username and password) and not question_dialog( + self, _('Are you sure?'), + _('No username and password set for mailserver. Most ' + ' mailservers need a username and password. Are you sure?')): + return False conf = smtp_prefs() conf.set('from_', from_) conf.set('relay_host', host if host else None) diff --git a/src/calibre/library/save_to_disk.py b/src/calibre/library/save_to_disk.py index cfb774c186..930c275a17 100644 --- a/src/calibre/library/save_to_disk.py +++ b/src/calibre/library/save_to_disk.py @@ -437,18 +437,17 @@ def update_serialized_metadata(book, common_data=None): plugboard_cache = common_data from calibre.customize.ui import apply_null_metadata with apply_null_metadata: + fmts = [fp.rpartition(os.extsep)[-1] for fp in book['fmts']] + mi, cdata = read_serialized_metadata(book) - fmts = [fp.rpartition(os.extsep)[-1] for fp in book['fmts']] - mi, cdata = read_serialized_metadata(book) + def report_error(fmt, tb): + result.append((fmt, tb)) - def report_error(fmt, tb): - result.append((fmt, tb)) - - for fmt, fmtpath in zip(fmts, book['fmts']): - try: - with lopen(fmtpath, 'r+b') as stream: - update_metadata(mi, fmt, stream, (), cdata, error_report=report_error, plugboard_cache=plugboard_cache) - except Exception: - report_error(fmt, traceback.format_exc()) + for fmt, fmtpath in zip(fmts, book['fmts']): + try: + with lopen(fmtpath, 'r+b') as stream: + update_metadata(mi, fmt, stream, (), cdata, error_report=report_error, plugboard_cache=plugboard_cache) + except Exception: + report_error(fmt, traceback.format_exc()) return result diff --git a/src/calibre/spell/dictionary.py b/src/calibre/spell/dictionary.py index 6845a5cc3d..a49ee8a04c 100644 --- a/src/calibre/spell/dictionary.py +++ b/src/calibre/spell/dictionary.py @@ -177,7 +177,7 @@ class Dictionaries(object): def __init__(self): self.remove_hyphenation = re.compile('[\u2010-]+') - self.negative_pat = re.compile('-[.\d+]') + self.negative_pat = re.compile(r'-[.\d+]') self.fix_punctuation_pat = re.compile(r'''[:.]''') self.dictionaries = {} self.word_cache = {} diff --git a/src/calibre/utils/fonts/scanner.py b/src/calibre/utils/fonts/scanner.py index 2b1130ae24..16a3fa504e 100644 --- a/src/calibre/utils/fonts/scanner.py +++ b/src/calibre/utils/fonts/scanner.py @@ -398,6 +398,7 @@ class FontScanner(Thread): prints() prints() + font_scanner = FontScanner() font_scanner.start() @@ -407,5 +408,6 @@ def force_rescan(): font_scanner.force_rescan() font_scanner.run() + if __name__ == '__main__': font_scanner.dump_fonts() diff --git a/src/calibre/utils/html2text.py b/src/calibre/utils/html2text.py index 6541e26f78..c3d386fad9 100644 --- a/src/calibre/utils/html2text.py +++ b/src/calibre/utils/html2text.py @@ -121,8 +121,8 @@ def fixattrs(attrs): def onlywhite(line): """Return true if the line does only consist of whitespace characters.""" for c in line: - if c is not ' ' and c is not ' ': - return c is ' ' + if c != ' ' and c != ' ': + return c == ' ' return line @@ -136,7 +136,7 @@ def optwrap(text): newlines = 0 for para in text.split("\n"): if len(para) > 0: - if para[0] is not ' ' and para[0] is not '-' and para[0] is not '*': + if para[0] != ' ' and para[0] != '-' and para[0] != '*': for line in wrap(para, BODY_WIDTH): result += line + "\n" result += "\n" diff --git a/src/calibre/utils/open_with/osx.py b/src/calibre/utils/open_with/osx.py index 6b02aef6d3..68f5a66862 100644 --- a/src/calibre/utils/open_with/osx.py +++ b/src/calibre/utils/open_with/osx.py @@ -329,7 +329,7 @@ def get_icon(path, pixmap_to_data=None, as_data=False, size=64): from PyQt5.Qt import QImage, Qt names.sort(key=numeric_sort_key) for name in names: - m = re.search('(\d+)x\d+', name) + m = re.search(r'(\d+)x\d+', name) if m is not None and int(m.group(1)) >= size: ans = QImage(os.path.join(iconset, name)) if not ans.isNull(): diff --git a/src/calibre/utils/soupparser.py b/src/calibre/utils/soupparser.py index 6de014b0f5..8d798385a3 100644 --- a/src/calibre/utils/soupparser.py +++ b/src/calibre/utils/soupparser.py @@ -118,7 +118,7 @@ except ImportError: from htmlentitydefs import name2codepoint import re -handle_entities = re.compile("&(\w+);").sub +handle_entities = re.compile(r"&(\w+);").sub def unescape(string):