mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-31 14:33:54 -04:00
py3: port a bunch of places where individual byes are accessed from a bytestring
This commit is contained in:
parent
1b0ce5c17f
commit
36ff1b15b8
@ -43,8 +43,8 @@ class TCRCompressor(object):
|
|||||||
possible_codes.append(single_code.pop())
|
possible_codes.append(single_code.pop())
|
||||||
|
|
||||||
for code in possible_codes:
|
for code in possible_codes:
|
||||||
self.coded_txt = self.coded_txt.replace(code, code[0])
|
self.coded_txt = self.coded_txt.replace(code, code[0:1])
|
||||||
self.codes[ord(code[0])] = b'%s%s' % (self.codes[ord(code[0])], self.codes[ord(code[1])])
|
self.codes[ord(code[0:1])] = b'%s%s' % (self.codes[ord(code[0:1])], self.codes[ord(code[1:2])])
|
||||||
|
|
||||||
def _free_unused_codes(self):
|
def _free_unused_codes(self):
|
||||||
'''
|
'''
|
||||||
|
@ -84,9 +84,9 @@ class DjvuChunk(object):
|
|||||||
if not res.strip(b'\0'):
|
if not res.strip(b'\0'):
|
||||||
raise ValueError('TXTz block is completely null')
|
raise ValueError('TXTz block is completely null')
|
||||||
l = 0
|
l = 0
|
||||||
for x in res[:3]:
|
for x in bytearray(res[:3]):
|
||||||
l <<= 8
|
l <<= 8
|
||||||
l += ord(x)
|
l += x
|
||||||
if verbose > 0 and out:
|
if verbose > 0 and out:
|
||||||
print(l, file=out)
|
print(l, file=out)
|
||||||
txtout.write(res[3:3+l])
|
txtout.write(res[3:3+l])
|
||||||
@ -94,9 +94,9 @@ class DjvuChunk(object):
|
|||||||
if txtout and self.type == b'TXTa':
|
if txtout and self.type == b'TXTa':
|
||||||
res = self.buf[self.datastart: self.dataend]
|
res = self.buf[self.datastart: self.dataend]
|
||||||
l = 0
|
l = 0
|
||||||
for x in res[:3]:
|
for x in bytearray(res[:3]):
|
||||||
l <<= 8
|
l <<= 8
|
||||||
l += ord(x)
|
l += x
|
||||||
if verbose > 0 and out:
|
if verbose > 0 and out:
|
||||||
print(l, file=out)
|
print(l, file=out)
|
||||||
txtout.write(res[3:3+l])
|
txtout.write(res[3:3+l])
|
||||||
|
@ -871,7 +871,7 @@ class LitFile(object):
|
|||||||
for i in range(1, nentries + 1):
|
for i in range(1, nentries + 1):
|
||||||
if len(data) <= 1:
|
if len(data) <= 1:
|
||||||
break
|
break
|
||||||
size, data = ord(data[0]), data[1:]
|
size, data = ord(data[0:1]), data[1:]
|
||||||
if size == 0 or len(data) < size:
|
if size == 0 or len(data) < size:
|
||||||
break
|
break
|
||||||
tags[i], data = data[:size], data[size:]
|
tags[i], data = data[:size], data[size:]
|
||||||
|
@ -17,7 +17,7 @@ from calibre.utils.config_base import tweaks
|
|||||||
from calibre.utils.date import parse_only_date
|
from calibre.utils.date import parse_only_date
|
||||||
from calibre.utils.localization import canonicalize_lang
|
from calibre.utils.localization import canonicalize_lang
|
||||||
from calibre.utils.imghdr import identify
|
from calibre.utils.imghdr import identify
|
||||||
from polyglot.builtins import unicode_type, filter
|
from polyglot.builtins import unicode_type, filter, map
|
||||||
from polyglot.binary import as_base64_bytes, from_base64_bytes
|
from polyglot.binary import as_base64_bytes, from_base64_bytes
|
||||||
|
|
||||||
|
|
||||||
@ -62,7 +62,11 @@ COVER_KEY = "cover_image_base64"
|
|||||||
|
|
||||||
|
|
||||||
def hexs(string, sep=' '):
|
def hexs(string, sep=' '):
|
||||||
return sep.join('%02x' % ord(b) for b in string)
|
if isinstance(string, bytes):
|
||||||
|
string = bytearray(string)
|
||||||
|
else:
|
||||||
|
string = map(ord, string)
|
||||||
|
return sep.join('%02x' % b for b in string)
|
||||||
|
|
||||||
|
|
||||||
class PackedData(object):
|
class PackedData(object):
|
||||||
|
@ -277,9 +277,9 @@ class MetadataUpdater(object):
|
|||||||
offset += consumed
|
offset += consumed
|
||||||
self.md_header['tag'] = self.data[offset:offset+taglen]
|
self.md_header['tag'] = self.data[offset:offset+taglen]
|
||||||
offset += taglen
|
offset += taglen
|
||||||
self.md_header['flags'] = ord(self.data[offset])
|
self.md_header['flags'] = ord(self.data[offset:offset+1])
|
||||||
offset += 1
|
offset += 1
|
||||||
self.md_header['num_recs'] = ord(self.data[offset])
|
self.md_header['num_recs'] = ord(self.data[offset:offset+1])
|
||||||
offset += 1
|
offset += 1
|
||||||
# print "self.md_header: %s" % self.md_header
|
# print "self.md_header: %s" % self.md_header
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ class SecondaryIndexHeader(object): # {{{
|
|||||||
raise ValueError('TAGX last entry is not EOF')
|
raise ValueError('TAGX last entry is not EOF')
|
||||||
|
|
||||||
idxt0_pos = self.header_length+self.tagx_header_length
|
idxt0_pos = self.header_length+self.tagx_header_length
|
||||||
num = ord(raw[idxt0_pos])
|
num = ord(raw[idxt0_pos:idxt0_pos+1])
|
||||||
count_pos = idxt0_pos+1+num
|
count_pos = idxt0_pos+1+num
|
||||||
self.last_entry = raw[idxt0_pos+1:count_pos]
|
self.last_entry = raw[idxt0_pos+1:count_pos]
|
||||||
self.ncx_count, = struct.unpack(b'>H', raw[count_pos:count_pos+2])
|
self.ncx_count, = struct.unpack(b'>H', raw[count_pos:count_pos+2])
|
||||||
|
@ -496,11 +496,11 @@ class Reader(FormatReader):
|
|||||||
html += u'<p>'
|
html += u'<p>'
|
||||||
paragraph_open = True
|
paragraph_open = True
|
||||||
|
|
||||||
c = ord(d[offset])
|
c = ord(d[offset:offset+1])
|
||||||
# PHTML "functions"
|
# PHTML "functions"
|
||||||
if c == 0x0:
|
if c == 0x0:
|
||||||
offset += 1
|
offset += 1
|
||||||
c = ord(d[offset])
|
c = ord(d[offset:offset+1])
|
||||||
# Page link begins
|
# Page link begins
|
||||||
# 2 Bytes
|
# 2 Bytes
|
||||||
# record ID
|
# record ID
|
||||||
|
@ -333,7 +333,7 @@ class SearchFilter(SearchQueryParser):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
SearchQueryParser.__init__(self, locations=self.USABLE_LOCATIONS)
|
SearchQueryParser.__init__(self, locations=self.USABLE_LOCATIONS)
|
||||||
self.srs = set([])
|
self.srs = set()
|
||||||
# remove joiner words surrounded by space or at string boundaries
|
# remove joiner words surrounded by space or at string boundaries
|
||||||
self.joiner_pat = re.compile(r'(^|\s)(and|not|or|a|the|is|of)(\s|$)', re.IGNORECASE)
|
self.joiner_pat = re.compile(r'(^|\s)(and|not|or|a|the|is|of)(\s|$)', re.IGNORECASE)
|
||||||
self.punctuation_table = {ord(x):' ' for x in string.punctuation}
|
self.punctuation_table = {ord(x):' ' for x in string.punctuation}
|
||||||
|
@ -41,7 +41,7 @@ def unpickle_binary_string(data):
|
|||||||
sz, = struct.unpack_from('<i', data, offset)
|
sz, = struct.unpack_from('<i', data, offset)
|
||||||
offset += struct.calcsize('<i')
|
offset += struct.calcsize('<i')
|
||||||
elif which == SHORT_BINSTRING:
|
elif which == SHORT_BINSTRING:
|
||||||
sz = ord(data[offset])
|
sz = ord(data[offset:offset+1])
|
||||||
offset += 1
|
offset += 1
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
@ -36,11 +36,11 @@ class ByteCode(dict):
|
|||||||
return b0 - 139, index
|
return b0 - 139, index
|
||||||
|
|
||||||
def read_small_int1(self, b0, data, index):
|
def read_small_int1(self, b0, data, index):
|
||||||
b1 = ord(data[index])
|
b1 = ord(data[index:index+1])
|
||||||
return (b0-247)*256 + b1 + 108, index+1
|
return (b0-247)*256 + b1 + 108, index+1
|
||||||
|
|
||||||
def read_small_int2(self, b0, data, index):
|
def read_small_int2(self, b0, data, index):
|
||||||
b1 = ord(data[index])
|
b1 = ord(data[index:index+1])
|
||||||
return -(b0-251)*256 - b1 - 108, index+1
|
return -(b0-251)*256 - b1 - 108, index+1
|
||||||
|
|
||||||
def read_short_int(self, b0, data, index):
|
def read_short_int(self, b0, data, index):
|
||||||
@ -61,7 +61,7 @@ class ByteCode(dict):
|
|||||||
def read_real_number(self, b0, data, index):
|
def read_real_number(self, b0, data, index):
|
||||||
number = ''
|
number = ''
|
||||||
while True:
|
while True:
|
||||||
b = ord(data[index])
|
b = ord(data[index:index+1])
|
||||||
index = index + 1
|
index = index + 1
|
||||||
nibble0 = (b & 0xf0) >> 4
|
nibble0 = (b & 0xf0) >> 4
|
||||||
nibble1 = b & 0x0f
|
nibble1 = b & 0x0f
|
||||||
@ -144,7 +144,7 @@ class Dict(ByteCode):
|
|||||||
self.stack = []
|
self.stack = []
|
||||||
index = 0
|
index = 0
|
||||||
while index < len(data):
|
while index < len(data):
|
||||||
b0 = ord(data[index])
|
b0 = ord(data[index:index+1])
|
||||||
index += 1
|
index += 1
|
||||||
handler = getattr(self, self.operand_encoding[b0])
|
handler = getattr(self, self.operand_encoding[b0])
|
||||||
value, index = handler(b0, data, index)
|
value, index = handler(b0, data, index)
|
||||||
@ -153,7 +153,7 @@ class Dict(ByteCode):
|
|||||||
|
|
||||||
def do_operator(self, b0, data, index):
|
def do_operator(self, b0, data, index):
|
||||||
if b0 == 12:
|
if b0 == 12:
|
||||||
op = (b0, ord(data[index]))
|
op = (b0, ord(data[index:index+1]))
|
||||||
index += 1
|
index += 1
|
||||||
else:
|
else:
|
||||||
op = b0
|
op = b0
|
||||||
|
@ -18,7 +18,7 @@ from css_selectors.errors import SelectorSyntaxError, ExpressionError
|
|||||||
from polyglot.builtins import unicode_type, codepoint_to_chr, range
|
from polyglot.builtins import unicode_type, codepoint_to_chr, range
|
||||||
|
|
||||||
|
|
||||||
utab = {c:c+32 for c in range(ord('A'), ord('Z')+1)}
|
utab = {c:c+32 for c in range(ord(u'A'), ord(u'Z')+1)}
|
||||||
|
|
||||||
if sys.version_info.major < 3:
|
if sys.version_info.major < 3:
|
||||||
tab = string.maketrans(string.ascii_uppercase, string.ascii_lowercase)
|
tab = string.maketrans(string.ascii_uppercase, string.ascii_lowercase)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user