py3: misc fixes

Connecting to a folder device and converting LRF files now work
This commit is contained in:
Kovid Goyal 2019-04-16 09:31:42 +05:30
parent 4f1a6c16c6
commit 4e2556ee63
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
8 changed files with 21 additions and 24 deletions

View File

@ -554,7 +554,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
else: else:
res[k] = v res[k] = v
from calibre.utils.config import to_json from calibre.utils.config import to_json
return json.dumps([op, res], encoding='utf-8', default=to_json) return json.dumps([op, res], default=to_json)
# Network functions # Network functions

View File

@ -287,7 +287,7 @@ class USBMS(CLI, Device):
# Remove books that are no longer in the filesystem. Cache contains # Remove books that are no longer in the filesystem. Cache contains
# indices into the booklist if book not in filesystem, None otherwise # indices into the booklist if book not in filesystem, None otherwise
# Do the operation in reverse order so indices remain valid # Do the operation in reverse order so indices remain valid
for idx in sorted(itervalues(bl_cache), reverse=True): for idx in sorted(itervalues(bl_cache), reverse=True, key=lambda x: -1 if x is None else x):
if idx is not None: if idx is not None:
need_sync = True need_sync = True
del bl[idx] del bl[idx]

View File

@ -12,6 +12,7 @@ from copy import deepcopy, copy
from lxml import etree from lxml import etree
from calibre import guess_type from calibre import guess_type
from polyglot.builtins import as_bytes
class Canvas(etree.XSLTExtension): class Canvas(etree.XSLTExtension):
@ -21,7 +22,7 @@ class Canvas(etree.XSLTExtension):
self.styles = styles self.styles = styles
self.text_block = text_block self.text_block = text_block
self.log = log self.log = log
self.processed = set([]) self.processed = set()
def execute(self, context, self_node, input_node, output_parent): def execute(self, context, self_node, input_node, output_parent):
cid = input_node.get('objid', None) cid = input_node.get('objid', None)
@ -298,7 +299,7 @@ class Styles(etree.XSLTExtension):
return '\n\t'.join(ans) return '\n\t'.join(ans)
with open(name, 'wb') as f: with open(name, 'wb') as f:
f.write(self.CSS) f.write(as_bytes(self.CSS))
for (w, sel) in [(self.text_styles, 'ts'), (self.block_styles, for (w, sel) in [(self.text_styles, 'ts'), (self.block_styles,
'bs')]: 'bs')]:
for i, s in enumerate(w): for i, s in enumerate(w):
@ -306,7 +307,7 @@ class Styles(etree.XSLTExtension):
continue continue
rsel = '.%s%d'%(sel, i) rsel = '.%s%d'%(sel, i)
s = join(s) s = join(s)
f.write(rsel + ' {\n\t' + s + '\n}\n\n') f.write(as_bytes(rsel + ' {\n\t' + s + '\n}\n\n'))
def execute(self, context, self_node, input_node, output_parent): def execute(self, context, self_node, input_node, output_parent):
if input_node.tag == 'TextStyle': if input_node.tag == 'TextStyle':
@ -400,7 +401,3 @@ class Styles(etree.XSLTExtension):
if ans not in self.text_styles: if ans not in self.text_styles:
self.text_styles.append(ans) self.text_styles.append(ans)
return self.text_styles.index(ans) return self.text_styles.index(ans)

View File

@ -3,6 +3,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
'''''' ''''''
import sys, array, os, re, codecs, logging import sys, array, os, re, codecs, logging
from itertools import chain
from calibre import setup_cli_handlers from calibre import setup_cli_handlers
from calibre.utils.config import OptionParser from calibre.utils.config import OptionParser
@ -10,7 +11,7 @@ from calibre.utils.filenames import ascii_filename
from calibre.ebooks.lrf.meta import LRFMetaFile from calibre.ebooks.lrf.meta import LRFMetaFile
from calibre.ebooks.lrf.objects import get_object, PageTree, StyleObject, \ from calibre.ebooks.lrf.objects import get_object, PageTree, StyleObject, \
Font, Text, TOCObject, BookAttr, ruby_tags Font, Text, TOCObject, BookAttr, ruby_tags
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, itervalues
class LRFDocument(LRFMetaFile): class LRFDocument(LRFMetaFile):
@ -45,7 +46,7 @@ class LRFDocument(LRFMetaFile):
self.objects = {} self.objects = {}
self._file.seek(self.object_index_offset) self._file.seek(self.object_index_offset)
obj_array = array.array("I", self._file.read(4*4*self.number_of_objects)) obj_array = array.array("I", self._file.read(4*4*self.number_of_objects))
if ord(array.array("i",[1]).tostring()[0])==0: # big-endian if ord(array.array("i",[1]).tostring()[0:1])==0: # big-endian
obj_array.byteswap() obj_array.byteswap()
for i in range(self.number_of_objects): for i in range(self.number_of_objects):
if not self.keep_parsing: if not self.keep_parsing:
@ -77,7 +78,7 @@ class LRFDocument(LRFMetaFile):
yield pt yield pt
def write_files(self): def write_files(self):
for obj in self.image_map.values() + self.font_map.values(): for obj in chain(itervalues(self.image_map), itervalues(self.font_map)):
open(obj.file, 'wb').write(obj.stream) open(obj.file, 'wb').write(obj.stream)
def to_xml(self, write_files=True): def to_xml(self, write_files=True):

View File

@ -47,11 +47,7 @@ class field(object):
obj.pack(val, start=self._start, fmt=self._fmt) obj.pack(val, start=self._start, fmt=self._fmt)
def __repr__(self): def __repr__(self):
typ = "" typ = {DWORD: 'unsigned int', 'QWORD': 'unsigned long long', BYTE: 'unsigned char', WORD: 'unsigned short'}.get(self._fmt, '')
if self._fmt == DWORD:
typ = "unsigned int"
if self._fmt == QWORD:
typ = "unsigned long long"
return "An " + typ + " stored in " + \ return "An " + typ + " stored in " + \
str(struct.calcsize(self._fmt)) + \ str(struct.calcsize(self._fmt)) + \
" bytes starting at byte " + str(self._start) " bytes starting at byte " + str(self._start)
@ -63,17 +59,17 @@ class versioned_field(field):
field.__init__(self, start=start, fmt=fmt) field.__init__(self, start=start, fmt=fmt)
self.vfield, self.version = vfield, version self.vfield, self.version = vfield, version
def enabled(self): def enabled(self, obj):
return self.vfield > self.version return self.vfield.__get__(obj) > self.version
def __get__(self, obj, typ=None): def __get__(self, obj, typ=None):
if self.enabled(): if self.enabled(obj):
return field.__get__(self, obj, typ=typ) return field.__get__(self, obj, typ=typ)
else: else:
return None return None
def __set__(self, obj, val): def __set__(self, obj, val):
if not self.enabled(): if not self.enabled(obj):
raise LRFException("Trying to set disabled field") raise LRFException("Trying to set disabled field")
else: else:
field.__set__(self, obj, val) field.__set__(self, obj, val)

View File

@ -176,7 +176,7 @@ class LRFStream(LRFObject):
self.stream = zlib.decompress(self.stream[4:]) self.stream = zlib.decompress(self.stream[4:])
if len(self.stream) != decomp_size: if len(self.stream) != decomp_size:
raise LRFParseError("Stream decompressed size is wrong!") raise LRFParseError("Stream decompressed size is wrong!")
if stream.read(2) != '\x06\xF5': if stream.read(2) != b'\x06\xF5':
print("Warning: corrupted end-of-stream tag at %08X; skipping it"%(stream.tell()-2)) print("Warning: corrupted end-of-stream tag at %08X; skipping it"%(stream.tell()-2))
self.end_stream(None, None) self.end_stream(None, None)
@ -850,7 +850,7 @@ class Text(LRFStream):
# Is there some text before a tag? # Is there some text before a tag?
def find_first_tag(start): def find_first_tag(start):
pos = self.stream.find('\xf5', start) pos = self.stream.find(b'\xf5', start)
if pos == -1: if pos == -1:
return -1 return -1
try: try:

View File

@ -132,7 +132,7 @@ class JsonCodec(object):
self.field_metadata = field_metadata or FieldMetadata() self.field_metadata = field_metadata or FieldMetadata()
def encode_to_file(self, file_, booklist): def encode_to_file(self, file_, booklist):
data = json.dumps(self.encode_booklist_metadata(booklist), indent=2, encoding='utf-8') data = json.dumps(self.encode_booklist_metadata(booklist), indent=2)
if not isinstance(data, bytes): if not isinstance(data, bytes):
data = data.encode('utf-8') data = data.encode('utf-8')
file_.write(data) file_.write(data)

View File

@ -126,6 +126,9 @@ class BaseJob(object):
def is_running(self): def is_running(self):
return self.is_started and not self.is_finished return self.is_started and not self.is_finished
def __hash__(self):
return id(self)
def __eq__(self, other): def __eq__(self, other):
return self is other return self is other