pep8 and misc cleanups

This commit is contained in:
Kovid Goyal 2025-02-08 09:33:35 +05:30
parent 78be0f69a4
commit cc8b7ad47b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 40 additions and 22 deletions

View File

@ -753,8 +753,8 @@ class Metadata:
res = human_readable(res) res = human_readable(res)
return (name, str(res), orig_res, fmeta) return (name, str(res), orig_res, fmeta)
if self.get(key, None): if kv := self.get(key, None):
return (key, str(self.get(key)), self.get(key), None) return (key, str(kv), kv, None)
return (None, None, None, None) return (None, None, None, None)

View File

@ -69,17 +69,21 @@ def search_action_with_data(search_term, value, book_id, field=None, **k):
def cc_search_action_with_data(search_term, value, book_id, fm, mi, field=None, **k): def cc_search_action_with_data(search_term, value, book_id, fm, mi, field=None, **k):
if mi is not None and fm is not None: if mi is not None and fm is not None:
template = fm.get('display', {}).get('web_search_template') if template := fm.get('display', {}).get('web_search_template'):
if template: try:
formatter = SafeFormat() formatter = SafeFormat()
mi.set('item_value', value) mi.set('item_value', value)
u = formatter.safe_format(template, mi, "BOOK DETAILS WEB LINK", mi) u = formatter.safe_format(template, mi, 'BOOK DETAILS WEB LINK', mi)
if u: if u:
v = prepare_string_for_xml(_('Click to browse to {0}').format(u), attribute=True) v = prepare_string_for_xml(_('Click to browse to {}').format(u), attribute=True)
return action('cc_url', url=u),v return action('cc_url', url=u), v
except Exception:
import traceback
traceback.print_exc()
t = _('Click to see books with {0}: {1}').format(mi.get('name', search_term), prepare_string_for_xml(value)) t = _('Click to see books with {0}: {1}').format(mi.get('name', search_term), prepare_string_for_xml(value))
return search_action_with_data(search_term, value, book_id, **k), t return search_action_with_data(search_term, value, book_id, **k), t
def notes_action(**keys): def notes_action(**keys):
return 'notes:' + as_hex_unicode(json_dumps(keys)) return 'notes:' + as_hex_unicode(json_dumps(keys))
@ -225,16 +229,16 @@ def mi_to_html(
ans.append((field, row % (name, comments_to_html(val)))) ans.append((field, row % (name, comments_to_html(val))))
else: else:
if not metadata['is_multiple']: if not metadata['is_multiple']:
u,v = cc_search_action_with_data(field, val, book_id, metadata, mi, field) u, v = cc_search_action_with_data(field, val, book_id, metadata, mi, field)
val = '<a href="{}" title="{}">{}</a>'.format(u, v, p(val)) val = f'<a href="{u}" title="{v}">{p(val)}</a>'
else: else:
all_vals = [v.strip() all_vals = [v.strip()
for v in val.split(metadata['is_multiple']['cache_to_list']) if v.strip()] for v in val.split(metadata['is_multiple']['cache_to_list']) if v.strip()]
if show_links: if show_links:
links = [] links = []
for x in all_vals: for x in all_vals:
u,v = cc_search_action_with_data(field, x, book_id, metadata, mi, field) u, v = cc_search_action_with_data(field, x, book_id, metadata, mi, field)
links.append('<a href="{}" title="{}">{}</a>'.format(u, v, p(x))) links.append(f'<a href="{u}" title="{v}">{p(x)}</a>')
else: else:
links = all_vals links = all_vals
val = value_list(metadata['is_multiple']['list_to_ui'], links) val = value_list(metadata['is_multiple']['list_to_ui'], links)
@ -364,7 +368,7 @@ def mi_to_html(
st = field st = field
series = getattr(mi, field) series = getattr(mi, field)
if metadata.get('display', {}).get('web_search_template'): if metadata.get('display', {}).get('web_search_template'):
u,v = cc_search_action_with_data(st, series, book_id, metadata, mi, field) u, v = cc_search_action_with_data(st, series, book_id, metadata, mi, field)
val = _('%(sidx)s of <a href="%(href)s" title="%(tt)s">' val = _('%(sidx)s of <a href="%(href)s" title="%(tt)s">'
'<span class="%(cls)s">%(series)s</span></a>') % dict( '<span class="%(cls)s">%(series)s</span></a>') % dict(
sidx=fmt_sidx(sidx, use_roman=use_roman_numbers), cls='series_name', sidx=fmt_sidx(sidx, use_roman=use_roman_numbers), cls='series_name',
@ -405,8 +409,8 @@ def mi_to_html(
if show_links: if show_links:
for x in all_vals: for x in all_vals:
if metadata['is_custom']: if metadata['is_custom']:
u,v = cc_search_action_with_data(field, x, book_id, metadata, mi, field) u, v = cc_search_action_with_data(field, x, book_id, metadata, mi, field)
v = '<a href="{}" title="{}">{}</a>'.format(u, v, p(x)) v = f'<a href="{u}" title="{v}">{p(x)}</a>'
else: else:
v = '<a href="{}" title="{}">{}</a>'.format( v = '<a href="{}" title="{}">{}</a>'.format(
search_action_with_data(st, x, book_id, field), search_action_with_data(st, x, book_id, field),
@ -424,8 +428,8 @@ def mi_to_html(
st = field st = field
if show_links: if show_links:
if metadata['is_custom']: if metadata['is_custom']:
u,v = cc_search_action_with_data(st, x, book_id, metadata, mi, field) u, v = cc_search_action_with_data(st, x, book_id, metadata, mi, field)
v = '<a href="{}" title="{}">{}</a>'.format(u, v, p(x)) v = f'<a href="{u}" title="{v}">{p(x)}</a>'
else: else:
v = '<a href="{}" title="{}">{}</a>'.format( v = '<a href="{}" title="{}">{}</a>'.format(
search_action_with_data(st, x, book_id, field), search_action_with_data(st, x, book_id, field),
@ -435,8 +439,8 @@ def mi_to_html(
v = val v = val
val = v + add_other_links(field, val) val = v + add_other_links(field, val)
elif metadata['datatype'] == 'enumeration': elif metadata['datatype'] == 'enumeration':
u,v = cc_search_action_with_data(field, x, book_id, metadata, mi, field) u, v = cc_search_action_with_data(field, x, book_id, metadata, mi, field)
val = '<a href="{}" title="{}">{}</a>'.format(u, v, p(x)) + add_other_links(field, val) val = f'<a href="{u}" title="{v}">{p(x)}</a>' + add_other_links(field, val)
elif metadata['datatype'] == 'bool': elif metadata['datatype'] == 'bool':
val = '<a href="{}" title="{}">{}</a>'.format( val = '<a href="{}" title="{}">{}</a>'.format(
search_action_with_data(field, val, book_id, None), a( search_action_with_data(field, val, book_id, None), a(

View File

@ -7,7 +7,21 @@ __docformat__ = 'restructuredtext en'
import json import json
from qt.core import QAbstractListModel, QComboBox, QDialog, QFormLayout, QHBoxLayout, QIcon, QItemSelectionModel, QLineEdit, Qt, QToolButton, QVBoxLayout, QWidget, pyqtSignal from qt.core import (
QAbstractListModel,
QComboBox,
QDialog,
QFormLayout,
QHBoxLayout,
QIcon,
QItemSelectionModel,
QLineEdit,
Qt,
QToolButton,
QVBoxLayout,
QWidget,
pyqtSignal,
)
from calibre.ebooks.metadata.book.render import DEFAULT_AUTHOR_LINK from calibre.ebooks.metadata.book.render import DEFAULT_AUTHOR_LINK
from calibre.ebooks.metadata.search_internet import qquote from calibre.ebooks.metadata.search_internet import qquote