From c18eb0cb8c8a5b506b7d0f594c79ff87478ac468 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Fri, 14 Jun 2019 02:25:15 -0400 Subject: [PATCH 1/9] standardize on one is_py3 representation --- src/calibre/ebooks/lrf/objects.py | 41 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/calibre/ebooks/lrf/objects.py b/src/calibre/ebooks/lrf/objects.py index 1695bc2697..caf226f5d3 100644 --- a/src/calibre/ebooks/lrf/objects.py +++ b/src/calibre/ebooks/lrf/objects.py @@ -5,10 +5,9 @@ __copyright__ = '2008, Kovid Goyal ' import struct, array, zlib, io, collections, re from calibre.ebooks.lrf import LRFParseError, PRS500_PROFILE -from calibre.constants import ispy3 from calibre import entity_to_unicode, prepare_string_for_xml from calibre.ebooks.lrf.tags import Tag -from polyglot.builtins import unicode_type, string_or_bytes +from polyglot.builtins import is_py3, unicode_type, string_or_bytes ruby_tags = { 0xF575: ['rubyAlignAndAdjust', 'W'], @@ -209,7 +208,7 @@ class StyleObject(object): s += '/>\n' return s - if not ispy3: + if not is_py3: __unicode__ = __str__ def as_dict(self): @@ -257,7 +256,7 @@ class Color(object): def __str__(self): return '0x%02x%02x%02x%02x'%(self.a, self.r, self.g, self.b) - if not ispy3: + if not is_py3: __unicode__ = __str__ def __len__(self): @@ -290,7 +289,7 @@ class PageDiv(EmptyPageElement): return '\n\n'%\ (self.pain, self.spacesize, self.linewidth, self.color) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -308,7 +307,7 @@ class RuledLine(EmptyPageElement): return '\n\n'%\ (self.linelength, self.linetype, self.linewidth, self.linecolor) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -320,7 +319,7 @@ class Wait(EmptyPageElement): def __str__(self): return '\n\n'%(self.time) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -334,7 +333,7 @@ class Locate(EmptyPageElement): def __str__(self): return '\n\n'%(self.pos) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -347,7 +346,7 @@ class BlockSpace(EmptyPageElement): return '\n\n'%\ (self.xspace, self.yspace) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -451,7 +450,7 @@ class Page(LRFStream): s += '\n\n' return s - if not ispy3: + if not is_py3: __unicode__ = __str__ def to_html(self): @@ -642,7 +641,7 @@ class Block(LRFStream, TextCSS): return s return s.rstrip() + ' />\n' - if not ispy3: + if not is_py3: __unicode__ = __str__ def to_html(self): @@ -723,7 +722,7 @@ class Text(LRFStream): s += '%s="%s" '%(name, val) return s.rstrip() + (' />' if self.self_closing else '>') - if not ispy3: + if not is_py3: __unicode__ = __str__ def to_html(self): @@ -923,7 +922,7 @@ class Text(LRFStream): raise LRFParseError('Malformed text stream %s'%([i.name for i in open_containers if isinstance(i, Text.TextTag)],)) return s - if not ispy3: + if not is_py3: __unicode__ = __str__ def to_html(self): @@ -974,7 +973,7 @@ class Image(LRFObject): return '\n'%\ (self.id, self.x0, self.y0, self.x1, self.y1, self.xsize, self.ysize, self.refstream) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -987,7 +986,7 @@ class PutObj(EmptyPageElement): def __str__(self): return ''%(self.x1, self.y1, self.refobj) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -1038,7 +1037,7 @@ class Canvas(LRFStream): s += '\n'%(self.__class__.__name__,) return s - if not ispy3: + if not is_py3: __unicode__ = __str__ def __iter__(self): @@ -1078,7 +1077,7 @@ class ImageStream(LRFStream): return '\n'%\ (self.id, self.encoding, self.file) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -1170,7 +1169,7 @@ class Button(LRFObject): s += '\n' return s - if not ispy3: + if not is_py3: __unicode__ = __str__ refpage = property(fget=lambda self : self.jump_action(2)[0]) @@ -1244,7 +1243,7 @@ class BookAttr(StyleObject, LRFObject): s += '\n' return s - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -1260,7 +1259,7 @@ class TocLabel(object): def __str__(self): return '%s\n'%(self.refpage, self.refobject, self.label) - if not ispy3: + if not is_py3: __unicode__ = __str__ @@ -1290,7 +1289,7 @@ class TOCObject(LRFStream): s += unicode_type(i) return s + '\n' - if not ispy3: + if not is_py3: __unicode__ = __str__ From 824f909627724f81dffc11a950c319f7adb7ccff Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Fri, 14 Jun 2019 02:28:36 -0400 Subject: [PATCH 2/9] do not shadow builtins as variable names --- src/calibre/ebooks/lrf/objects.py | 4 ++-- src/calibre/ebooks/metadata/odt.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/calibre/ebooks/lrf/objects.py b/src/calibre/ebooks/lrf/objects.py index caf226f5d3..b8b276d3a4 100644 --- a/src/calibre/ebooks/lrf/objects.py +++ b/src/calibre/ebooks/lrf/objects.py @@ -97,8 +97,8 @@ class LRFContentObject(LRFObject): tag_map = {} - def __init__(self, bytes, objects): - self.stream = bytes if hasattr(bytes, 'read') else io.BytesIO(bytes) + def __init__(self, byts, objects): + self.stream = byts if hasattr(byts, 'read') else io.BytesIO(byts) length = self.stream_size() self.objects = objects self._contents = [] diff --git a/src/calibre/ebooks/metadata/odt.py b/src/calibre/ebooks/metadata/odt.py index bdad22aebf..5583d25bae 100644 --- a/src/calibre/ebooks/metadata/odt.py +++ b/src/calibre/ebooks/metadata/odt.py @@ -56,13 +56,13 @@ fields = { } -def normalize(str): +def normalize(s): """ The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. """ - return whitespace.sub(' ', str).strip() + return whitespace.sub(' ', s).strip() class MetaCollector: @@ -75,9 +75,9 @@ class MetaCollector: self._content = [] self.dowrite = True - def write(self, str): + def write(self, s): if self.dowrite: - self._content.append(str) + self._content.append(s) def content(self): return ''.join(self._content) From 1f794c4cd2a2d64d5e55091aaa5bd58e9259ccfa Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Thu, 13 Jun 2019 17:10:05 -0400 Subject: [PATCH 3/9] py3: more work on universal __future__s --- src/calibre/ebooks/lrf/__init__.py | 2 + src/calibre/ebooks/lrf/html/__init__.py | 2 + src/calibre/ebooks/lrf/html/color_map.py | 182 +++++++++--------- src/calibre/ebooks/lrf/html/convert_from.py | 65 ++++--- src/calibre/ebooks/lrf/html/table.py | 13 +- src/calibre/ebooks/lrf/html/table_as_image.py | 4 +- src/calibre/ebooks/lrf/input.py | 19 +- src/calibre/ebooks/lrf/lrs/__init__.py | 5 +- src/calibre/ebooks/lrf/lrs/convert_from.py | 7 +- src/calibre/ebooks/lrf/objects.py | 26 +-- src/calibre/ebooks/lrf/pylrs/__init__.py | 2 + src/calibre/ebooks/lrf/pylrs/elements.py | 20 +- src/calibre/ebooks/lrf/pylrs/pylrf.py | 8 +- src/calibre/ebooks/lrf/pylrs/pylrfopt.py | 4 +- src/calibre/ebooks/lrf/pylrs/pylrs.py | 80 ++++---- src/calibre/ebooks/lrf/tags.py | 6 +- src/calibre/ebooks/metadata/__init__.py | 49 +++-- src/calibre/ebooks/metadata/archive.py | 6 +- src/calibre/ebooks/metadata/book/__init__.py | 1 + src/calibre/ebooks/metadata/book/base.py | 33 ++-- .../ebooks/metadata/book/json_codec.py | 3 +- src/calibre/ebooks/metadata/cli.py | 3 +- src/calibre/ebooks/metadata/epub.py | 2 +- src/calibre/ebooks/metadata/extz.py | 1 + src/calibre/ebooks/metadata/fb2.py | 2 + src/calibre/ebooks/metadata/kdl.py | 4 +- src/calibre/ebooks/metadata/lit.py | 2 + src/calibre/ebooks/metadata/meta.py | 7 +- src/calibre/ebooks/metadata/mobi.py | 7 +- src/calibre/ebooks/metadata/odt.py | 51 +++-- src/calibre/ebooks/metadata/pdb.py | 2 +- src/calibre/ebooks/metadata/pdf.py | 7 +- src/calibre/ebooks/metadata/rar.py | 4 +- src/calibre/ebooks/metadata/rb.py | 9 +- src/calibre/ebooks/metadata/snb.py | 2 +- .../ebooks/metadata/sources/__init__.py | 4 +- src/calibre/ebooks/metadata/sources/amazon.py | 42 ++-- src/calibre/ebooks/metadata/sources/google.py | 4 +- src/calibre/ebooks/metadata/toc.py | 11 +- src/calibre/ebooks/metadata/txt.py | 1 + src/calibre/ebooks/metadata/xisbn.py | 2 +- src/calibre/ebooks/metadata/zip.py | 3 +- 42 files changed, 366 insertions(+), 341 deletions(-) diff --git a/src/calibre/ebooks/lrf/__init__.py b/src/calibre/ebooks/lrf/__init__.py index 6c696b16ed..6be9e69e13 100644 --- a/src/calibre/ebooks/lrf/__init__.py +++ b/src/calibre/ebooks/lrf/__init__.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' """ diff --git a/src/calibre/ebooks/lrf/html/__init__.py b/src/calibre/ebooks/lrf/html/__init__.py index 9e110971bf..cb4d4686eb 100644 --- a/src/calibre/ebooks/lrf/html/__init__.py +++ b/src/calibre/ebooks/lrf/html/__init__.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' """ diff --git a/src/calibre/ebooks/lrf/html/color_map.py b/src/calibre/ebooks/lrf/html/color_map.py index 9c74c61018..ce4af2d71d 100644 --- a/src/calibre/ebooks/lrf/html/color_map.py +++ b/src/calibre/ebooks/lrf/html/color_map.py @@ -1,99 +1,101 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' import re NAME_MAP = { - u'aliceblue': u'#F0F8FF', - u'antiquewhite': u'#FAEBD7', - u'aqua': u'#00FFFF', - u'aquamarine': u'#7FFFD4', - u'azure': u'#F0FFFF', - u'beige': u'#F5F5DC', - u'bisque': u'#FFE4C4', - u'black': u'#000000', - u'blanchedalmond': u'#FFEBCD', - u'blue': u'#0000FF', - u'brown': u'#A52A2A', - u'burlywood': u'#DEB887', - u'cadetblue': u'#5F9EA0', - u'chartreuse': u'#7FFF00', - u'chocolate': u'#D2691E', - u'coral': u'#FF7F50', - u'crimson': u'#DC143C', - u'cyan': u'#00FFFF', - u'darkblue': u'#00008B', - u'darkgoldenrod': u'#B8860B', - u'darkgreen': u'#006400', - u'darkkhaki': u'#BDB76B', - u'darkmagenta': u'#8B008B', - u'darkolivegreen': u'#556B2F', - u'darkorange': u'#FF8C00', - u'darkorchid': u'#9932CC', - u'darkred': u'#8B0000', - u'darksalmon': u'#E9967A', - u'darkslateblue': u'#483D8B', - u'darkslategrey': u'#2F4F4F', - u'darkviolet': u'#9400D3', - u'deeppink': u'#FF1493', - u'dodgerblue': u'#1E90FF', - u'firebrick': u'#B22222', - u'floralwhite': u'#FFFAF0', - u'forestgreen': u'#228B22', - u'fuchsia': u'#FF00FF', - u'gainsboro': u'#DCDCDC', - u'ghostwhite': u'#F8F8FF', - u'gold': u'#FFD700', - u'goldenrod': u'#DAA520', - u'indianred ': u'#CD5C5C', - u'indigo ': u'#4B0082', - u'khaki': u'#F0E68C', - u'lavenderblush': u'#FFF0F5', - u'lawngreen': u'#7CFC00', - u'lightblue': u'#ADD8E6', - u'lightcoral': u'#F08080', - u'lightgoldenrodyellow': u'#FAFAD2', - u'lightgray': u'#D3D3D3', - u'lightgrey': u'#D3D3D3', - u'lightskyblue': u'#87CEFA', - u'lightslategrey': u'#778899', - u'lightsteelblue': u'#B0C4DE', - u'lime': u'#87CEFA', - u'linen': u'#FAF0E6', - u'magenta': u'#FF00FF', - u'maroon': u'#800000', - u'mediumaquamarine': u'#66CDAA', - u'mediumblue': u'#0000CD', - u'mediumorchid': u'#BA55D3', - u'mediumpurple': u'#9370D8', - u'mediumseagreen': u'#3CB371', - u'mediumslateblue': u'#7B68EE', - u'midnightblue': u'#191970', - u'moccasin': u'#FFE4B5', - u'navajowhite': u'#FFDEAD', - u'navy': u'#000080', - u'oldlace': u'#FDF5E6', - u'olive': u'#808000', - u'orange': u'#FFA500', - u'orangered': u'#FF4500', - u'orchid': u'#DA70D6', - u'paleturquoise': u'#AFEEEE', - u'papayawhip': u'#FFEFD5', - u'peachpuff': u'#FFDAB9', - u'powderblue': u'#B0E0E6', - u'rosybrown': u'#BC8F8F', - u'royalblue': u'#4169E1', - u'saddlebrown': u'#8B4513', - u'sandybrown': u'#8B4513', - u'seashell': u'#FFF5EE', - u'sienna': u'#A0522D', - u'silver': u'#C0C0C0', - u'skyblue': u'#87CEEB', - u'slategrey': u'#708090', - u'snow': u'#FFFAFA', - u'springgreen': u'#00FF7F', - u'violet': u'#EE82EE', - u'yellowgreen': u'#9ACD32' + 'aliceblue': '#F0F8FF', + 'antiquewhite': '#FAEBD7', + 'aqua': '#00FFFF', + 'aquamarine': '#7FFFD4', + 'azure': '#F0FFFF', + 'beige': '#F5F5DC', + 'bisque': '#FFE4C4', + 'black': '#000000', + 'blanchedalmond': '#FFEBCD', + 'blue': '#0000FF', + 'brown': '#A52A2A', + 'burlywood': '#DEB887', + 'cadetblue': '#5F9EA0', + 'chartreuse': '#7FFF00', + 'chocolate': '#D2691E', + 'coral': '#FF7F50', + 'crimson': '#DC143C', + 'cyan': '#00FFFF', + 'darkblue': '#00008B', + 'darkgoldenrod': '#B8860B', + 'darkgreen': '#006400', + 'darkkhaki': '#BDB76B', + 'darkmagenta': '#8B008B', + 'darkolivegreen': '#556B2F', + 'darkorange': '#FF8C00', + 'darkorchid': '#9932CC', + 'darkred': '#8B0000', + 'darksalmon': '#E9967A', + 'darkslateblue': '#483D8B', + 'darkslategrey': '#2F4F4F', + 'darkviolet': '#9400D3', + 'deeppink': '#FF1493', + 'dodgerblue': '#1E90FF', + 'firebrick': '#B22222', + 'floralwhite': '#FFFAF0', + 'forestgreen': '#228B22', + 'fuchsia': '#FF00FF', + 'gainsboro': '#DCDCDC', + 'ghostwhite': '#F8F8FF', + 'gold': '#FFD700', + 'goldenrod': '#DAA520', + 'indianred ': '#CD5C5C', + 'indigo ': '#4B0082', + 'khaki': '#F0E68C', + 'lavenderblush': '#FFF0F5', + 'lawngreen': '#7CFC00', + 'lightblue': '#ADD8E6', + 'lightcoral': '#F08080', + 'lightgoldenrodyellow': '#FAFAD2', + 'lightgray': '#D3D3D3', + 'lightgrey': '#D3D3D3', + 'lightskyblue': '#87CEFA', + 'lightslategrey': '#778899', + 'lightsteelblue': '#B0C4DE', + 'lime': '#87CEFA', + 'linen': '#FAF0E6', + 'magenta': '#FF00FF', + 'maroon': '#800000', + 'mediumaquamarine': '#66CDAA', + 'mediumblue': '#0000CD', + 'mediumorchid': '#BA55D3', + 'mediumpurple': '#9370D8', + 'mediumseagreen': '#3CB371', + 'mediumslateblue': '#7B68EE', + 'midnightblue': '#191970', + 'moccasin': '#FFE4B5', + 'navajowhite': '#FFDEAD', + 'navy': '#000080', + 'oldlace': '#FDF5E6', + 'olive': '#808000', + 'orange': '#FFA500', + 'orangered': '#FF4500', + 'orchid': '#DA70D6', + 'paleturquoise': '#AFEEEE', + 'papayawhip': '#FFEFD5', + 'peachpuff': '#FFDAB9', + 'powderblue': '#B0E0E6', + 'rosybrown': '#BC8F8F', + 'royalblue': '#4169E1', + 'saddlebrown': '#8B4513', + 'sandybrown': '#8B4513', + 'seashell': '#FFF5EE', + 'sienna': '#A0522D', + 'silver': '#C0C0C0', + 'skyblue': '#87CEEB', + 'slategrey': '#708090', + 'snow': '#FFFAFA', + 'springgreen': '#00FF7F', + 'violet': '#EE82EE', + 'yellowgreen': '#9ACD32' } hex_pat = re.compile(r'#(\d{2})(\d{2})(\d{2})') diff --git a/src/calibre/ebooks/lrf/html/convert_from.py b/src/calibre/ebooks/lrf/html/convert_from.py index 41ec5cb6ce..be4feb2aeb 100644 --- a/src/calibre/ebooks/lrf/html/convert_from.py +++ b/src/calibre/ebooks/lrf/html/convert_from.py @@ -1,4 +1,5 @@ -from __future__ import print_function +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' """ @@ -106,7 +107,7 @@ class HTMLConverter(object): re.IGNORECASE), lambda m: '
'), # Replace entities - (re.compile(u'&(\\S+?);'), partial(entity_to_unicode, + (re.compile(r'&(\S+?);'), partial(entity_to_unicode, exceptions=['lt', 'gt', 'amp', 'quot'])), # Remove comments from within style tags as they can mess up BeatifulSoup (re.compile(r'()', re.IGNORECASE|re.DOTALL), @@ -397,7 +398,7 @@ class HTMLConverter(object): def parse_css(self, style): """ Parse the contents of a