Additional safe conversion of % format

This commit is contained in:
un-pogaz 2025-01-28 10:13:43 +01:00
parent e5a65f69ec
commit c15f118a5d
13 changed files with 37 additions and 37 deletions

View File

@ -1432,7 +1432,7 @@ class DB:
@user_version.setter @user_version.setter
def user_version(self, val): def user_version(self, val):
self.execute('PRAGMA user_version=%d'%int(val)) self.execute(f'PRAGMA user_version={int(val)}')
def initialize_database(self): def initialize_database(self):
metadata_sqlite = P('metadata_sqlite.sql', data=True, metadata_sqlite = P('metadata_sqlite.sql', data=True,

View File

@ -54,7 +54,7 @@ class Progress:
if msg is None: if msg is None:
self.total = float(step) self.total = float(step)
else: else:
prints(msg, '...', '%d%%' % int(100 * (step / self.total))) prints(msg, '...', f'{int(100*(step/self.total))}%')
def main(opts, args, dbctx): def main(opts, args, dbctx):

View File

@ -251,7 +251,7 @@ def fmt_sidx(i, fmt='%.2f', use_roman=False):
except Exception: except Exception:
return str(i) return str(i)
if int(i) == i: if int(i) == i:
return roman(int(i)) if use_roman else '%d'%int(i) return roman(int(i)) if use_roman else str(int(i))
ans = fmt%i ans = fmt%i
if '.' in ans: if '.' in ans:
ans = ans.rstrip('0') ans = ans.rstrip('0')

View File

@ -160,8 +160,8 @@ class MobiMLizer:
return ptsize return ptsize
embase = self.profile.fbase embase = self.profile.fbase
if round(ptsize) < embase: if round(ptsize) < embase:
return '%dpt' % int(round(ptsize)) return f'{int(round(ptsize))}pt'
return '%dem' % int(round(ptsize / embase)) return f'{int(round(ptsize/embase))}em'
def preize_text(self, text, pre_wrap=False): def preize_text(self, text, pre_wrap=False):
text = str(text) text = str(text)
@ -503,7 +503,7 @@ class MobiMLizer:
istate.attrib['width'] = raww istate.attrib['width'] = raww
else: else:
prop = style['width'] / self.profile.width prop = style['width'] / self.profile.width
istate.attrib['width'] = '%d%%' % int(round(prop * 100)) istate.attrib['width'] = f'{int(round(prop*100))}%'
elif display == 'table': elif display == 'table':
tag = 'table' tag = 'table'
elif display == 'table-row': elif display == 'table-row':

View File

@ -533,7 +533,7 @@ class MobiReader:
try: try:
nval = float(val[:-2]) nval = float(val[:-2])
nval *= 16 * (168.451/72) # Assume this was set using the Kindle profile nval *= 16 * (168.451/72) # Assume this was set using the Kindle profile
attrib[attr] = '%dpx'%int(nval) attrib[attr] = f'{int(nval)}px'
except: except:
del attrib[attr] del attrib[attr]
elif val.lower().endswith('%'): elif val.lower().endswith('%'):
@ -558,7 +558,7 @@ class MobiReader:
if 'filepos' in attrib: if 'filepos' in attrib:
filepos = attrib.pop('filepos') filepos = attrib.pop('filepos')
try: try:
attrib['href'] = '#filepos%d' % int(filepos) attrib['href'] = f'#filepos{int(filepos)}'
except ValueError: except ValueError:
pass pass
if (tag.tag == 'a' and attrib.get('id', '').startswith('filepos') and if (tag.tag == 'a' and attrib.get('id', '').startswith('filepos') and

View File

@ -151,8 +151,7 @@ class Image(Element):
self.align = 'L' self.align = 'L'
def to_html(self): def to_html(self):
return '<img src="%s" alt="" width="%dpx" height="%dpx"/>' % \ return f'<img src="{self.src}" alt="" width="{int(self.width)}px" height="{int(self.height)}px"/>'
(self.src, int(self.width), int(self.height))
def dump(self, f): def dump(self, f):
f.write(self.to_html()) f.write(self.to_html())

View File

@ -154,7 +154,7 @@ def py3_repr(x):
def options_to_recipe_source(title, oldest_article, max_articles_per_feed, feeds): def options_to_recipe_source(title, oldest_article, max_articles_per_feed, feeds):
classname = 'BasicUserRecipe%d' % int(time.time()) classname = f'BasicUserRecipe{int(time.time())}'
title = str(title).strip() or classname title = str(title).strip() or classname
indent = ' ' * 8 indent = ' ' * 8
if feeds: if feeds:

View File

@ -233,7 +233,7 @@ def zenity_cmd(window, title, *rest):
ans = ['zenity', '--modal', '--file-selection', '--title=' + title, '--separator=\n'] ans = ['zenity', '--modal', '--file-selection', '--title=' + title, '--separator=\n']
winid = get_winid(window) winid = get_winid(window)
if winid is not None: if winid is not None:
ans += ['--attach=%d' % int(winid)] ans += [f'--attach={int(winid)}']
return ans + list(rest) return ans + list(rest)

View File

@ -1555,7 +1555,7 @@ class CatalogBuilder:
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
if self.opts.generate_descriptions: if self.opts.generate_descriptions:
aTag['href'] = 'book_%d.html' % (int(float(book['id']))) aTag['href'] = 'book_{}.html'.format(int(float(book['id'])))
# Generate the title from the template # Generate the title from the template
args = self.generate_format_args(book) args = self.generate_format_args(book)
@ -1706,7 +1706,7 @@ class CatalogBuilder:
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
if self.opts.generate_descriptions: if self.opts.generate_descriptions:
aTag['href'] = 'book_%d.html' % (int(float(new_entry['id']))) aTag['href'] = 'book_{}.html'.format(int(float(new_entry['id'])))
# Generate the title from the template # Generate the title from the template
args = self.generate_format_args(new_entry) args = self.generate_format_args(new_entry)
@ -1758,7 +1758,7 @@ class CatalogBuilder:
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
if self.opts.generate_descriptions: if self.opts.generate_descriptions:
aTag['href'] = 'book_%d.html' % (int(float(new_entry['id']))) aTag['href'] = 'book_{}.html'.format(int(float(new_entry['id'])))
# Generate the title from the template # Generate the title from the template
args = self.generate_format_args(new_entry) args = self.generate_format_args(new_entry)
@ -1923,7 +1923,7 @@ class CatalogBuilder:
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
if self.opts.generate_descriptions: if self.opts.generate_descriptions:
aTag['href'] = 'book_%d.html' % (int(float(new_entry['id']))) aTag['href'] = 'book_{}.html'.format(int(float(new_entry['id'])))
aTag.insert(0, NavigableString(new_entry['title'])) aTag.insert(0, NavigableString(new_entry['title']))
pBookTag.insert(ptc, aTag) pBookTag.insert(ptc, aTag)
ptc += 1 ptc += 1
@ -1972,7 +1972,7 @@ class CatalogBuilder:
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
if self.opts.generate_descriptions: if self.opts.generate_descriptions:
aTag['href'] = 'book_%d.html' % (int(float(new_entry['id']))) aTag['href'] = 'book_{}.html'.format(int(float(new_entry['id'])))
aTag.insert(0, NavigableString(new_entry['title'])) aTag.insert(0, NavigableString(new_entry['title']))
pBookTag.insert(ptc, aTag) pBookTag.insert(ptc, aTag)
ptc += 1 ptc += 1
@ -2274,7 +2274,7 @@ class CatalogBuilder:
# Add the book title # Add the book title
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
if self.opts.generate_descriptions: if self.opts.generate_descriptions:
aTag['href'] = 'book_%d.html' % (int(float(book['id']))) aTag['href'] = 'book_{}.html'.format(int(float(book['id'])))
# Generate the title from the template # Generate the title from the template
args = self.generate_format_args(book) args = self.generate_format_args(book)
@ -2406,7 +2406,7 @@ class CatalogBuilder:
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
if self.opts.generate_descriptions: if self.opts.generate_descriptions:
aTag['href'] = 'book_%d.html' % (int(float(book['id']))) aTag['href'] = 'book_{}.html'.format(int(float(book['id'])))
# Use series, series index if avail else just title # Use series, series index if avail else just title
# aTag.insert(0,'%d. %s · %s' % (book['series_index'],escape(book['title']), ' & '.join(book['authors']))) # aTag.insert(0,'%d. %s · %s' % (book['series_index'],escape(book['title']), ' & '.join(book['authors'])))
@ -2566,7 +2566,7 @@ class CatalogBuilder:
# Link to book # Link to book
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
if self.opts.generate_descriptions: if self.opts.generate_descriptions:
aTag['href'] = 'book_%d.html' % (int(float(book['id']))) aTag['href'] = 'book_{}.html'.format(int(float(book['id'])))
# Generate the title from the template # Generate the title from the template
args = self.generate_format_args(book) args = self.generate_format_args(book)
@ -2728,7 +2728,7 @@ class CatalogBuilder:
_soup = BeautifulSoup('<html>', selfClosingTags=['img']) _soup = BeautifulSoup('<html>', selfClosingTags=['img'])
thumb = _soup.new_tag('img') thumb = _soup.new_tag('img')
if book.get('cover'): if book.get('cover'):
thumb['src'] = '../images/thumbnail_%d.jpg' % int(book['id']) thumb['src'] = '../images/thumbnail_{}.jpg'.format(int(book['id']))
else: else:
thumb['src'] = '../images/thumbnail_default.jpg' thumb['src'] = '../images/thumbnail_default.jpg'
thumb['alt'] = 'cover thumbnail' thumb['alt'] = 'cover thumbnail'
@ -2766,7 +2766,7 @@ class CatalogBuilder:
btc = 0 btc = 0
# Insert the title anchor for inbound links # Insert the title anchor for inbound links
aTag = soup.new_tag('a') aTag = soup.new_tag('a')
aTag['id'] = 'book%d' % int(book['id']) aTag['id'] = 'book{}'.format(int(book['id']))
divTag = soup.new_tag('div') divTag = soup.new_tag('div')
divTag.insert(0, aTag) divTag.insert(0, aTag)
body.insert(btc, divTag) body.insert(btc, divTag)
@ -2839,7 +2839,7 @@ class CatalogBuilder:
soup = self.generate_html_description_header(title) soup = self.generate_html_description_header(title)
# Write the book entry to content_dir # Write the book entry to content_dir
with open('%s/book_%d.html' % (self.content_dir, int(title['id'])), 'wb') as outfile: with open('{}/book_{}.html'.format(self.content_dir, int(title['id'])), 'wb') as outfile:
outfile.write(prettify(soup).encode('utf-8')) outfile.write(prettify(soup).encode('utf-8'))
def generate_html_empty_header(self, title): def generate_html_empty_header(self, title):
@ -2990,7 +2990,7 @@ class CatalogBuilder:
makeelement('content', navPointTag, src='content/ByDateAdded.html') makeelement('content', navPointTag, src='content/ByDateAdded.html')
elif self.opts.generate_descriptions: elif self.opts.generate_descriptions:
# Descriptions only # Descriptions only
makeelement('content', navPointTag, src='content/book_%d.html' % int(self.books_by_description[0]['id'])) makeelement('content', navPointTag, src='content/book_{}.html'.format(int(self.books_by_description[0]['id'])))
def generate_ncx_section_header(self, section_id, section_header, content_src): def generate_ncx_section_header(self, section_id, section_header, content_src):
root = self.ncx_root root = self.ncx_root
@ -3038,7 +3038,8 @@ class CatalogBuilder:
section_header = f'{tocTitle} [{len(self.books_by_description)}]' section_header = f'{tocTitle} [{len(self.books_by_description)}]'
if self.generate_for_kindle_mobi: if self.generate_for_kindle_mobi:
section_header = tocTitle section_header = tocTitle
navPointTag = self.generate_ncx_section_header('bydescription-ID', section_header, 'content/book_%d.html' % int(self.books_by_description[0]['id'])) navPointTag = self.generate_ncx_section_header(
'bydescription-ID', section_header, 'content/book_{}.html'.format(int(self.books_by_description[0]['id'])))
self.update_progress_full_step(_('NCX for descriptions')) self.update_progress_full_step(_('NCX for descriptions'))
@ -3047,7 +3048,7 @@ class CatalogBuilder:
# Loop over the titles # Loop over the titles
for book in self.books_by_description: for book in self.books_by_description:
sec_id = 'book%dID' % int(book['id']) sec_id = 'book{}ID'.format(int(book['id']))
if book['series']: if book['series']:
series_index = str(book['series_index']) series_index = str(book['series_index'])
if series_index.endswith('.0'): if series_index.endswith('.0'):
@ -3073,7 +3074,7 @@ class CatalogBuilder:
# Include Author for non-Kindle # Include Author for non-Kindle
sec_text = self.format_ncx_text('{} · {}'.format(book['title'], book['author']), dest='title') sec_text = self.format_ncx_text('{} · {}'.format(book['title'], book['author']), dest='title')
content_src='content/book_%d.html#book%d' % (int(book['id']), int(book['id'])) content_src='content/book_{}.html#book{}'.format(int(book['id']), int(book['id']))
cm_tags = {} cm_tags = {}
if book['date']: if book['date']:
@ -3677,7 +3678,7 @@ class CatalogBuilder:
for book in self.books_by_description: for book in self.books_by_description:
# manifest # manifest
manifest_item('book%d' % int(book['id']), 'content/book_%d.html' % int(book['id']), 'application/xhtml+xml', add_to_spine=True) manifest_item('book{}'.format(int(book['id'])), 'content/book_{}.html'.format(int(book['id'])), 'application/xhtml+xml', add_to_spine=True)
# Guide # Guide
if self.generate_for_kindle_mobi: if self.generate_for_kindle_mobi:
@ -3922,12 +3923,12 @@ class CatalogBuilder:
(_('Thumbnail'), i, len(self.books_by_title)), (_('Thumbnail'), i, len(self.books_by_title)),
i / float(len(self.books_by_title))) i / float(len(self.books_by_title)))
thumb_file = 'thumbnail_%d.jpg' % int(title['id']) thumb_file = 'thumbnail_{}.jpg'.format(int(title['id']))
thumb_generated = True thumb_generated = True
valid_cover = True valid_cover = True
try: try:
self.generate_thumbnail(title, image_dir, thumb_file) self.generate_thumbnail(title, image_dir, thumb_file)
thumbs.append('thumbnail_%d.jpg' % int(title['id'])) thumbs.append('thumbnail_{}.jpg'.format(int(title['id'])))
except: except:
if 'cover' in title and os.path.exists(title['cover']): if 'cover' in title and os.path.exists(title['cover']):
valid_cover = False valid_cover = False

View File

@ -90,7 +90,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
@user_version.setter @user_version.setter
def user_version(self, val): def user_version(self, val):
self.conn.execute('pragma user_version=%d'%int(val)) self.conn.execute(f'pragma user_version={int(val)}')
self.conn.commit() self.conn.commit()
@property @property

View File

@ -506,7 +506,7 @@ class HTTPConnection(HTTPRequest):
outheaders.set('Server', f'calibre {__version__}', replace_all=True) outheaders.set('Server', f'calibre {__version__}', replace_all=True)
keep_alive = not self.close_after_response and self.opts.timeout > 0 keep_alive = not self.close_after_response and self.opts.timeout > 0
if keep_alive: if keep_alive:
outheaders.set('Keep-Alive', 'timeout=%d' % int(self.opts.timeout)) outheaders.set('Keep-Alive', f'timeout={int(self.opts.timeout)}')
if 'Connection' not in outheaders: if 'Connection' not in outheaders:
if self.response_protocol is HTTP11: if self.response_protocol is HTTP11:
if self.close_after_response: if self.close_after_response:

View File

@ -574,7 +574,7 @@ class SchedulerConfig:
if urn.startswith('recipe_'): if urn.startswith('recipe_'):
urn = 'builtin:'+urn[7:] urn = 'builtin:'+urn[7:]
else: else:
urn = 'custom:%d'%int(urn) urn = f'custom:{int(urn)}'
try: try:
username, password = c[k] username, password = c[k]
except: except:
@ -596,14 +596,14 @@ class SchedulerConfig:
urn = 'builtin:'+r['id'][7:] urn = 'builtin:'+r['id'][7:]
elif not r['builtin']: elif not r['builtin']:
try: try:
urn = 'custom:%d'%int(r['id']) urn = 'custom:{}'.format(int(r['id']))
except: except:
return return
schedule = r['schedule'] schedule = r['schedule']
typ = 'interval' typ = 'interval'
if schedule > 1e5: if schedule > 1e5:
typ = 'day/time' typ = 'day/time'
raw = '%d'%int(schedule) raw = str(int(schedule))
day = int(raw[0]) - 1 day = int(raw[0]) - 1
hour = int(raw[2:4]) - 1 hour = int(raw[2:4]) - 1
minute = int(raw[-2:]) - 1 minute = int(raw[-2:]) - 1

View File

@ -313,7 +313,7 @@ class TouchscreenFeedTemplate(Template):
if f > 0: if f > 0:
link = A(attrs('feed_link'), link = A(attrs('feed_link'),
trim_title(feeds[f-1].title), trim_title(feeds[f-1].title),
href='../feed_%d/index.html' % int(f-1)) href=f'../feed_{int(f-1)}/index.html')
navbar_tr.append(TD(attrs('feed_prev'),link)) navbar_tr.append(TD(attrs('feed_prev'),link))
# Up to Sections # Up to Sections
@ -325,7 +325,7 @@ class TouchscreenFeedTemplate(Template):
if f < len(feeds)-1: if f < len(feeds)-1:
link = A(attrs('feed_link'), link = A(attrs('feed_link'),
trim_title(feeds[f+1].title), trim_title(feeds[f+1].title),
href='../feed_%d/index.html' % int(f+1)) href=f'../feed_{int(f+1)}/index.html')
navbar_tr.append(TD(attrs('feed_next'),link)) navbar_tr.append(TD(attrs('feed_next'),link))
navbar_t.append(navbar_tr) navbar_t.append(navbar_tr)
top_navbar = navbar_t top_navbar = navbar_t