mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-11-19 04:53:05 -05:00
Only check if name is present in container once
Apparently on some windows systems the check fails when run in a thread for mysterious reasons. I cant replicate the failure, but no real harm in this, maybe a very tiny performance decrease at worst.
This commit is contained in:
parent
051c25f1fa
commit
9d363b30a8
@ -71,7 +71,7 @@ def decode_url(x):
|
|||||||
return decode_component(parts[0]), (parts[1] if len(parts) > 1 else '')
|
return decode_component(parts[0]), (parts[1] if len(parts) > 1 else '')
|
||||||
|
|
||||||
|
|
||||||
def create_link_replacer(container, link_uid, changed):
|
def create_link_replacer(container, link_uid, changed, present_names):
|
||||||
resource_template = link_uid + '|{}|'
|
resource_template = link_uid + '|{}|'
|
||||||
|
|
||||||
def link_replacer(base, url):
|
def link_replacer(base, url):
|
||||||
@ -94,7 +94,7 @@ def create_link_replacer(container, link_uid, changed):
|
|||||||
url, frag = purl.path, purl.fragment
|
url, frag = purl.path, purl.fragment
|
||||||
name = container.href_to_name(url, base)
|
name = container.href_to_name(url, base)
|
||||||
if name:
|
if name:
|
||||||
if container.has_name_and_is_not_empty(name):
|
if name in present_names:
|
||||||
frag = urlunquote(frag)
|
frag = urlunquote(frag)
|
||||||
url = resource_template.format(encode_url(name, frag))
|
url = resource_template.format(encode_url(name, frag))
|
||||||
else:
|
else:
|
||||||
@ -258,12 +258,12 @@ def create_cover_page(container, input_fmt, is_comic, book_metadata=None):
|
|||||||
return raster_cover_name, titlepage_name
|
return raster_cover_name, titlepage_name
|
||||||
|
|
||||||
|
|
||||||
def transform_style_sheet(container, name, link_uid, virtualize_resources, virtualized_names):
|
def transform_style_sheet(container, name, link_uid, virtualize_resources, virtualized_names, present_names):
|
||||||
changed = False
|
changed = False
|
||||||
link_replacer = None
|
link_replacer = None
|
||||||
if virtualize_resources:
|
if virtualize_resources:
|
||||||
changed_names = set()
|
changed_names = set()
|
||||||
link_replacer = partial(create_link_replacer(container, link_uid, changed_names), name)
|
link_replacer = partial(create_link_replacer(container, link_uid, changed_names, present_names), name)
|
||||||
raw = container.raw_data(name, decode=True)
|
raw = container.raw_data(name, decode=True)
|
||||||
nraw = transform_properties(raw, is_declaration=False, url_callback=link_replacer)
|
nraw = transform_properties(raw, is_declaration=False, url_callback=link_replacer)
|
||||||
if virtualize_resources:
|
if virtualize_resources:
|
||||||
@ -282,10 +282,10 @@ def transform_style_sheet(container, name, link_uid, virtualize_resources, virtu
|
|||||||
f.write(raw.encode('utf-8'))
|
f.write(raw.encode('utf-8'))
|
||||||
|
|
||||||
|
|
||||||
def transform_svg_image(container, name, link_uid, virtualize_resources, virtualized_names):
|
def transform_svg_image(container, name, link_uid, virtualize_resources, virtualized_names, present_names):
|
||||||
if not virtualize_resources:
|
if not virtualize_resources:
|
||||||
return
|
return
|
||||||
link_replacer = create_link_replacer(container, link_uid, set())
|
link_replacer = create_link_replacer(container, link_uid, set(), present_names)
|
||||||
xlink = XLINK('href')
|
xlink = XLINK('href')
|
||||||
altered = False
|
altered = False
|
||||||
xlink_xpath = XPath('//*[@xl:href]')
|
xlink_xpath = XPath('//*[@xl:href]')
|
||||||
@ -326,7 +326,7 @@ def parse_smil_time(x):
|
|||||||
return seconds
|
return seconds
|
||||||
|
|
||||||
|
|
||||||
def transform_smil(container, name, link_uid, virtualize_resources, virtualized_names, smil_map):
|
def transform_smil(container, name, link_uid, virtualize_resources, virtualized_names, smil_map, present_names):
|
||||||
root = container.parsed(name)
|
root = container.parsed(name)
|
||||||
text_tag, audio_tag = SMIL('text'), SMIL('audio')
|
text_tag, audio_tag = SMIL('text'), SMIL('audio')
|
||||||
body_tag, seq_tag, par_tag = SMIL('body'), SMIL('seq'), SMIL('par')
|
body_tag, seq_tag, par_tag = SMIL('body'), SMIL('seq'), SMIL('par')
|
||||||
@ -419,7 +419,7 @@ def transform_inline_styles(container, name, transform_sheet, transform_style):
|
|||||||
return changed
|
return changed
|
||||||
|
|
||||||
|
|
||||||
def transform_html(container, name, virtualize_resources, link_uid, link_to_map, virtualized_names):
|
def transform_html(container, name, virtualize_resources, link_uid, link_to_map, virtualized_names, present_names):
|
||||||
link_xpath = XPath('//h:*[@href and (self::h:a or self::h:area)]')
|
link_xpath = XPath('//h:*[@href and (self::h:a or self::h:area)]')
|
||||||
svg_link_xpath = XPath('//svg:a')
|
svg_link_xpath = XPath('//svg:a')
|
||||||
img_xpath = XPath('//h:img[@src]')
|
img_xpath = XPath('//h:img[@src]')
|
||||||
@ -427,7 +427,7 @@ def transform_html(container, name, virtualize_resources, link_uid, link_to_map,
|
|||||||
res_link_xpath = XPath('//h:link[@href]')
|
res_link_xpath = XPath('//h:link[@href]')
|
||||||
root = container.parsed(name)
|
root = container.parsed(name)
|
||||||
changed_names = set()
|
changed_names = set()
|
||||||
link_replacer = create_link_replacer(container, link_uid, changed_names)
|
link_replacer = create_link_replacer(container, link_uid, changed_names, present_names)
|
||||||
|
|
||||||
# Used for viewing images
|
# Used for viewing images
|
||||||
for img in img_xpath(root):
|
for img in img_xpath(root):
|
||||||
@ -464,7 +464,7 @@ def transform_html(container, name, virtualize_resources, link_uid, link_to_map,
|
|||||||
transform_inline_styles(container, name, transform_sheet=transform_sheet, transform_style=transform_declaration)
|
transform_inline_styles(container, name, transform_sheet=transform_sheet, transform_style=transform_declaration)
|
||||||
|
|
||||||
if virtualize_resources:
|
if virtualize_resources:
|
||||||
virtualize_html(container, name, link_uid, link_to_map, virtualized_names)
|
virtualize_html(container, name, link_uid, link_to_map, virtualized_names, present_names)
|
||||||
else:
|
else:
|
||||||
|
|
||||||
def handle_link(a, attr='href'):
|
def handle_link(a, attr='href'):
|
||||||
@ -497,12 +497,12 @@ def transform_html(container, name, virtualize_resources, link_uid, link_to_map,
|
|||||||
f.write(shtml)
|
f.write(shtml)
|
||||||
|
|
||||||
|
|
||||||
def virtualize_html(container, name, link_uid, link_to_map, virtualized_names):
|
def virtualize_html(container, name, link_uid, link_to_map, virtualized_names, present_names):
|
||||||
|
|
||||||
changed = set()
|
changed = set()
|
||||||
link_xpath = XPath('//h:*[@href and (self::h:a or self::h:area)]')
|
link_xpath = XPath('//h:*[@href and (self::h:a or self::h:area)]')
|
||||||
svg_link_xpath = XPath('//svg:a')
|
svg_link_xpath = XPath('//svg:a')
|
||||||
link_replacer = create_link_replacer(container, link_uid, changed)
|
link_replacer = create_link_replacer(container, link_uid, changed, present_names)
|
||||||
|
|
||||||
virtualized_names.add(name)
|
virtualized_names.add(name)
|
||||||
root = container.parsed(name)
|
root = container.parsed(name)
|
||||||
@ -542,7 +542,7 @@ def virtualize_html(container, name, link_uid, link_to_map, virtualized_names):
|
|||||||
__smil_file_names__ = ''
|
__smil_file_names__ = ''
|
||||||
|
|
||||||
|
|
||||||
def process_book_file(virtualize_resources, link_uid, container, name):
|
def process_book_file(virtualize_resources, link_uid, container, present_names, name):
|
||||||
link_to_map = {}
|
link_to_map = {}
|
||||||
html_data = {}
|
html_data = {}
|
||||||
smil_map = {__smil_file_names__: []}
|
smil_map = {__smil_file_names__: []}
|
||||||
@ -556,14 +556,14 @@ def process_book_file(virtualize_resources, link_uid, container, name):
|
|||||||
'has_maths': check_for_maths(root),
|
'has_maths': check_for_maths(root),
|
||||||
'anchor_map': anchor_map(root)
|
'anchor_map': anchor_map(root)
|
||||||
}
|
}
|
||||||
transform_html(container, name, virtualize_resources, link_uid, link_to_map, virtualized_names)
|
transform_html(container, name, virtualize_resources, link_uid, link_to_map, virtualized_names, present_names)
|
||||||
elif mt in OEB_STYLES:
|
elif mt in OEB_STYLES:
|
||||||
transform_style_sheet(container, name, link_uid, virtualize_resources, virtualized_names)
|
transform_style_sheet(container, name, link_uid, virtualize_resources, virtualized_names, present_names)
|
||||||
elif mt == 'image/svg+xml':
|
elif mt == 'image/svg+xml':
|
||||||
transform_svg_image(container, name, link_uid, virtualize_resources, virtualized_names)
|
transform_svg_image(container, name, link_uid, virtualize_resources, virtualized_names, present_names)
|
||||||
elif mt in ('application/smil', 'application/smil+xml'):
|
elif mt in ('application/smil', 'application/smil+xml'):
|
||||||
smil_map[__smil_file_names__].append(name)
|
smil_map[__smil_file_names__].append(name)
|
||||||
transform_smil(container, name, link_uid, virtualize_resources, virtualized_names, smil_map)
|
transform_smil(container, name, link_uid, virtualize_resources, virtualized_names, smil_map, present_names)
|
||||||
return link_to_map, html_data, virtualized_names, smil_map
|
return link_to_map, html_data, virtualized_names, smil_map
|
||||||
|
|
||||||
|
|
||||||
@ -599,10 +599,16 @@ def process_exploded_book(
|
|||||||
# We do not add zero byte sized files as the IndexedDB API in the
|
# We do not add zero byte sized files as the IndexedDB API in the
|
||||||
# browser has no good way to distinguish between zero byte files and
|
# browser has no good way to distinguish between zero byte files and
|
||||||
# load failures.
|
# load failures.
|
||||||
excluded_names = {
|
excluded_names = set()
|
||||||
name for name, mt in container.mime_map.items() if
|
present_names = set()
|
||||||
name == container.opf_name or mt == guess_type('a.ncx') or name.startswith('META-INF/') or
|
for name, mt in container.mime_map.items():
|
||||||
name == 'mimetype' or not container.has_name_and_is_not_empty(name)}
|
if container.has_name_and_is_not_empty(name):
|
||||||
|
present_names.add(name)
|
||||||
|
if name == container.opf_name or mt == guess_type('a.ncx') or name.startswith(
|
||||||
|
'META-INF/') or name == 'mimetype':
|
||||||
|
excluded_names.add(name)
|
||||||
|
else:
|
||||||
|
excluded_names.add(name)
|
||||||
raster_cover_name, titlepage_name = create_cover_page(container, input_fmt.lower(), is_comic, book_metadata)
|
raster_cover_name, titlepage_name = create_cover_page(container, input_fmt.lower(), is_comic, book_metadata)
|
||||||
|
|
||||||
tocobj = get_toc(container, verify_destinations=False)
|
tocobj = get_toc(container, verify_destinations=False)
|
||||||
@ -647,7 +653,7 @@ def process_exploded_book(
|
|||||||
names_that_need_work = tuple(n for n, mt in container.mime_map.items() if needs_work(mt))
|
names_that_need_work = tuple(n for n, mt in container.mime_map.items() if needs_work(mt))
|
||||||
num_workers = calculate_number_of_workers(names_that_need_work, container, max_workers)
|
num_workers = calculate_number_of_workers(names_that_need_work, container, max_workers)
|
||||||
results = []
|
results = []
|
||||||
f = partial(process_book_file, virtualize_resources, book_render_data['link_uid'], container)
|
f = partial(process_book_file, virtualize_resources, book_render_data['link_uid'], container, present_names)
|
||||||
if num_workers < 2:
|
if num_workers < 2:
|
||||||
results.extend(map(f, names_that_need_work))
|
results.extend(map(f, names_that_need_work))
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user