Skip to content

Commit

Permalink
utilize f-strings
Browse files Browse the repository at this point in the history
Updating various implementation to take advantage of f-strings over the
use of printf (UP031) and format (UP032). This includes also tweaks to
original format calls to use variables for some complex values, as well
as even dropping format calls for simple string concatenation.

Signed-off-by: James Knight <james.d.knight@live.com>
  • Loading branch information
jdknight committed Mar 2, 2024
1 parent 17605b6 commit ac473fa
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 78 deletions.
9 changes: 4 additions & 5 deletions sphinxcontrib/confluencebuilder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ def init(self):
old_url = self.config.confluence_server_url
new_url = ConfluenceUtil.normalize_base_url(old_url)
if old_url != new_url:
self.warn('normalizing confluence url from '
'{} to {} '.format(old_url, new_url))
self.warn(f'normalizing confluence url from {old_url} to {new_url}')
self.config.confluence_server_url = new_url

# detect if Confluence Cloud if using the Atlassian domain
Expand Down Expand Up @@ -611,7 +610,7 @@ def publish_asset(self, key, docname, output, type_, hash_):
self.state.register_upload_id(docname, page_id)
else:
self.warn('cannot publish asset since publishing '
'point cannot be found ({}): {}'.format(key, docname))
f'point cannot be found ({key}): {docname}')
return

attachment_id = None
Expand Down Expand Up @@ -1166,9 +1165,9 @@ def _parse_doctree_title(self, docname, doctree):
doctitle = f'autogen-{docname}'
if self.publish:
self.warn('document will be published using an '
'generated title value: {}'.format(docname))
f'generated title value: {docname}')
elif self.publish:
self.warn('document will not be published since it '
'has no title: {}'.format(docname))
f'has no title: {docname}')

return doctitle
6 changes: 3 additions & 3 deletions sphinxcontrib/confluencebuilder/cmd/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ def report_main(args_parser):
for o in root.findall('buildNumber'):
info += ' build: ' + o.text + '\n'
else:
logger.error('bad response from server ({})'.format(
rsp.status_code))
info += f' fetched: error ({rsp.status_code})\n'
code = rsp.status_code
logger.error(f'bad response from server ({code})')
info += f' fetched: error ({code})\n'
rv = 1
except Exception: # noqa: BLE001
sys.stdout.flush()
Expand Down
18 changes: 9 additions & 9 deletions sphinxcontrib/confluencebuilder/intersphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ def escape(string):
inventory_db = builder.out_dir / INVENTORY_FILENAME
with inventory_db.open('wb') as f:
# header
f.write((
'# Sphinx inventory version 2\n'
'# Project: {}\n'
'# Version: {}\n'
'# The remainder of this file is compressed using zlib.\n'.format(
escape(builder.env.config.project),
escape(builder.env.config.version))).encode())
f.write(
(
'# Sphinx inventory version 2\n'
f'# Project: {escape(builder.env.config.project)}\n'
f'# Version: {escape(builder.env.config.version)}\n'
'# The remainder of this file is compressed using zlib.\n'
).encode()
)

# contents
compressor = zlib.compressobj(9)
Expand Down Expand Up @@ -74,8 +75,7 @@ def escape(string):
uri += '#' + anchor
if dispname == name:
dispname = '-'
entry = ('%s %s:%s %s %s %s\n' %
(name, domainname, typ, prio, uri, dispname))
entry = f'{name} {domainname}:{typ} {prio} {uri} {dispname}\n'
logger.verbose('(intersphinx) ' + entry.strip())
f.write(compressor.compress(entry.encode('utf-8')))

Expand Down
10 changes: 5 additions & 5 deletions sphinxcontrib/confluencebuilder/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ def _wrapper(self, *args, **kwargs):
if self.config.confluence_publish_delay:
delay = self.config.confluence_publish_delay
logger.verbose('user-set api delay set; '
'waiting {} seconds...'.format(math.ceil(delay)))
f'waiting {math.ceil(delay)} seconds...')
time.sleep(delay)

# if confluence asked us to wait so many seconds before a next
# api request, wait a moment
if self.next_delay:
delay = self.next_delay
logger.verbose('rate-limit header detected; '
'waiting {} seconds...'.format(math.ceil(delay)))
f'waiting {math.ceil(delay)} seconds...')
time.sleep(delay)
self.next_delay = None

Expand Down Expand Up @@ -128,7 +128,7 @@ def _wrapper(self, *args, **kwargs):

# wait the calculated delay before retrying again
logger.warn('rate-limit response detected; '
'waiting {} seconds...'.format(math.ceil(delay)))
f'waiting {math.ceil(delay)} seconds...')
time.sleep(delay)
self.last_retry = delay
attempt += 1
Expand Down Expand Up @@ -373,8 +373,8 @@ def _process_request(self, method, path, *args, **kwargs):
# user once
if delay >= 60 and not self._reported_large_delay:
logger.warn('(warning) site has reported a long '
'rate-limit delay ({} seconds)'.format(
math.ceil(delay)))
'rate-limit delay '
f'({math.ceil(delay)} seconds)')
self._reported_large_delay = True

# check if Confluence reports a `Deprecation` header in the response;
Expand Down
3 changes: 1 addition & 2 deletions sphinxcontrib/confluencebuilder/singlebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def get_relative_uri(self, from_, to, typ=None):

def get_target_uri(self, docname, typ=None):
if docname in self.env.all_docs:
return '{}{}#{}'.format(
self.config.root_doc, self.link_suffix, docname)
return f'{self.config.root_doc}{self.link_suffix}#{docname}'
else:
return self.link_transform(docname)

Expand Down
8 changes: 4 additions & 4 deletions sphinxcontrib/confluencebuilder/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def register_title(docname, title, config):
offset = 2
while title.lower() in ConfluenceState.title2doc:
if offset == 2:
docname2 = ConfluenceState.title2doc[title.lower()]
logger.warn('title conflict detected with '
"'{}' and '{}'".format(
ConfluenceState.title2doc[title.lower()], docname))
f"'{docname2}' and '{docname}'")

tail = f' ({offset}){base_tail}'
if len(base_title) + len(tail) > try_max:
Expand Down Expand Up @@ -227,8 +227,8 @@ def _format_postfix(postfix, docname, config):
)
except KeyError:
raise ConfluenceConfigError(
"Configured confluence_publish_prefix '{postfix}' has an "
"unknown template replacement.".format(postfix=postfix))
f"Configured confluence_publish_prefix '{postfix}' has an "
"unknown template replacement.")
return postfix

@staticmethod
Expand Down
59 changes: 30 additions & 29 deletions sphinxcontrib/confluencebuilder/storage/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def get_secnumber(self, node):
if self.builder.name == 'singleconfluence':
docname = self._docnames[-1]
raw_anchor = node.parent['ids'][0]
anchorname = '{}/#{}'.format(docname, node.parent['ids'][0])
anchorname = f'{docname}/#{raw_anchor}'
if anchorname not in self.builder.secnumbers:
anchorname = '%s/' % raw_anchor
anchorname = f'{raw_anchor}/'
else:
anchorname = '#' + node.parent['ids'][0]
if anchorname not in self.builder.secnumbers:
Expand Down Expand Up @@ -171,7 +171,7 @@ def visit_start_of_file(self, node):
# an anchor point with the name matching the title (which allows the
# fallback link to jump to the desired point in a document).
if self.builder.name == 'singleconfluence':
doc_anchorname = '%s/' % node['docname']
doc_anchorname = node['docname'] + '/'
doc_target = self.state.target(doc_anchorname)
if not doc_target:
doc_id = node['docname']
Expand Down Expand Up @@ -276,7 +276,8 @@ def visit_paragraph(self, node):
# on the paragraph since the v2 editor does not permit nesting
# block elements for indentation
if self.v2 and self._indent_level > 0:
style += 'margin-left: {}px;'.format(INDENT * self._indent_level)
offset = INDENT * self._indent_level
style += f'margin-left: {offset}px;'

# MyST-Parser will inject text-align hints in the node's classes
# attribute; if set, attempt to apply the style
Expand Down Expand Up @@ -430,14 +431,12 @@ def visit_enumerated_list(self, node):
elif node['enumtype'] == 'arabic':
list_style_type = 'decimal'
else:
self.warn(
'unknown enumerated list type: {}'.format(node['enumtype']))
self.warn('unknown enumerated list type: ' + node['enumtype'])

if list_style_type:
if 'style' not in attribs:
attribs['style'] = ''
attribs['style'] = '{}list-style-type: {};'.format(
attribs['style'], list_style_type)
attribs['style'] += f'list-style-type: {list_style_type};'

self.body.append(self._start_tag(node, 'ol', suffix=self.nl, **attribs))
self.context.append(self._end_tag(node))
Expand Down Expand Up @@ -513,9 +512,9 @@ def visit_term(self, node):
self.body.append(self.context.pop()) # dt/p

if self.v2:
offset = INDENT * self._indent_level
self.body.append(self._start_tag(node, 'p',
**{'style': 'margin-left: {}px;'.format(
INDENT * self._indent_level)}))
**{'style': f'margin-left: {offset}px;'}))
self.context.append(self._end_tag(node))

if 'ids' in node:
Expand Down Expand Up @@ -1133,8 +1132,9 @@ def visit_tgroup(self, node):
# configuring it explicitly may break the editor's page
# width configuration
attribs = {}
if colspec['colwidth'] != 100 or not self.v2:
attribs['style'] = 'width: {}%'.format(colspec['colwidth'])
colwidth = colspec['colwidth']
if colwidth != 100 or not self.v2:
attribs['style'] = f'width: {colwidth}%'

self.body.append(self._start_tag(
node, 'col', empty=True, **attribs))
Expand Down Expand Up @@ -1296,7 +1296,7 @@ def _visit_reference_intern_id(self, node):
docname = self._docnames[-1]
anchorname = f'{docname}/#{raw_anchor}'
if anchorname not in self.builder.secnumbers:
anchorname = '%s/' % raw_anchor
anchorname = f'{raw_anchor}/'
else:
anchorname = f'{self.docname}#{raw_anchor}'

Expand Down Expand Up @@ -1346,7 +1346,7 @@ def _visit_reference_intern_uri(self, node):
doctitle = self.state.title(docname)
if not doctitle:
self.warn('unable to build link to document due to '
'missing title (in {}): {}'.format(self.docname, docname))
f'missing title (in {self.docname}): {docname}')

# build a broken link
self.body.append(self._start_tag(node, 'a', **{'href': '#'}))
Expand Down Expand Up @@ -1695,8 +1695,7 @@ def visit_caption(self, node):

alignment = self._fetch_alignment(node)
if alignment and alignment != 'left':
attribs['style'] = '{}text-align: {};'.format(
attribs['style'], alignment)
attribs['style'] += f'text-align: {alignment};'

self.body.append(self._start_tag(node, 'p', **attribs))
self.add_fignumber(node.parent)
Expand Down Expand Up @@ -1730,8 +1729,9 @@ def _visit_image(self, node, opts):
# offset vertical image to align with paragraph
if not self.v2 and node.get('from_math') and node.get('math_depth'):
math_depth = node['math_depth']
offset = -1 * math_depth
self.body.append(self._start_tag(node, 'span',
**{'style': 'vertical-align: {}px'.format(-1 * math_depth)}))
**{'style': f'vertical-align: {offset}px'}))
self.context.append(self._end_tag(node))

node.math_number = None
Expand Down Expand Up @@ -1888,8 +1888,8 @@ def depart_legend(self, node):
# ------------------

def visit_download_reference(self, node):
uri = node['reftarget']
uri = self.encode(uri)
reftarget = node['reftarget']
uri = self.encode(reftarget)

if uri.find('://') != -1:
self.body.append(self._start_tag(node, 'strong'))
Expand All @@ -1915,8 +1915,7 @@ def visit_download_reference(self, node):
node, asset_docname, standalone=True)

if not file_key:
self.warn('unable to find download: ' '{}'.format(
node['reftarget']))
self.warn(f'unable to find download: {reftarget}')
raise nodes.SkipNode

hosting_doctitle = self.state.title(hosting_docname)
Expand Down Expand Up @@ -2065,7 +2064,8 @@ def visit_productionlist(self, node):
self.body.append(f'{formatted_token} ::=')
lastname = production['tokenname']
else:
self.body.append('{} '.format(' ' * len(lastname)))
prefix = ' ' * len(lastname)
self.body.append(f'{prefix} ')
text = production.astext()
text = self.encode(text)
self.body.append(text + self.nl)
Expand Down Expand Up @@ -2263,8 +2263,7 @@ def visit_versionmodified(self, node):
elif node['type'] == 'versionadded':
self._visit_info(node)
else:
self.warn('unsupported version modification type: '
'{}'.format(node['type']))
self.warn('unsupported version modification type: ' + node['type'])
self._visit_info(node)

depart_versionmodified = _depart_admonition
Expand Down Expand Up @@ -2297,7 +2296,7 @@ def visit_confluence_excerpt_include(self, node):
if not doctitle:
self.warn('unable to build excerpt include to document since '
'document is missing or is missing title '
'(in {}): {}'.format(self.docname, docname))
f'(in {self.docname}): {docname}')
raise nodes.SkipNode
elif ':' in doclink:
space_key, doctitle = doclink.split(':', 1)
Expand Down Expand Up @@ -2542,7 +2541,7 @@ def visit_confluence_doc_card(self, node):
self.body.append(self._end_tag(node, suffix=''))
else:
self.warn('unable to build link to document card due to '
'missing title (in {}): {}'.format(self.docname, docname))
f'missing title (in {self.docname}): {docname}')
self.body.append(node.astext())

if not self.v2:
Expand All @@ -2567,7 +2566,7 @@ def visit_confluence_doc_card_inline(self, node):
self.body.append(self._end_ac_link(node))
else:
self.warn('unable to build link to document card due to '
'missing title (in {}): {}'.format(self.docname, docname))
f'missing title (in {self.docname}): {docname}')
self.body.append(node.astext())

raise nodes.SkipNode
Expand Down Expand Up @@ -3038,7 +3037,8 @@ def visit_line_block(self, node):
style += f'margin-left: {indent}px;'
elif self.v2:
# (see "visit_paragraph")
style += 'margin-left: {}px;'.format(INDENT * self._indent_level)
offset = INDENT * self._indent_level
style += f'margin-left: {offset}px;'

if style:
attribs['style'] = style
Expand Down Expand Up @@ -3163,7 +3163,8 @@ def _start_tag(self, node, tag, suffix=None, empty=False, **kwargs):
except AttributeError:
node.__confluence_tag = [tag]

return '<{}{}'.format(' '.join(data), suffix)
prefix = ' '.join(data)
return f'<{prefix}{suffix}'

def _end_tag(self, node, suffix=None):
"""
Expand Down
12 changes: 6 additions & 6 deletions sphinxcontrib/confluencebuilder/transmute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ def __init__(self, builder):
if 'ids' in new_node:
math_image_ids.extend(new_node['ids'])

except imgmath.MathExtError as exc:
ConfluenceLogger.warn('inline latex {}: {}'.format(
node.astext(), exc))
except imgmath.MathExtError as ex:
ConfluenceLogger.warn(f'inline latex {node.astext()}: {ex}')

# v2 editor will manually inject anchors in an image-managed
# section to avoid a newline spacing between the anchor and
Expand Down Expand Up @@ -215,8 +214,9 @@ def __init__(self, builder):
mock_translator = MockTranslator(builder)

for node in findall(doctree, graphviz):
node_code = node['code']
try:
_, out_filename = render_dot(mock_translator, node['code'],
_, out_filename = render_dot(mock_translator, node_code,
node['options'], builder.graphviz_output_format, 'graphviz')
if not out_filename:
node.parent.remove(node)
Expand All @@ -226,8 +226,8 @@ def __init__(self, builder):
if 'align' in node:
new_node['align'] = node['align']
node.replace_self(new_node)
except GraphvizError as exc:
ConfluenceLogger.warn('dot code {}: {}'.format(node['code'], exc))
except GraphvizError as ex:
ConfluenceLogger.warn(f'dot code {node_code}: {ex}')
node.parent.remove(node)


Expand Down
6 changes: 3 additions & 3 deletions tests/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def main():
target_unit_test_name_pattern)
if not target_unit_tests:
print('\nERROR: unable to find test with pattern: '
'{}\n'.format(target_unit_test_name_pattern))
f'{target_unit_test_name_pattern}\n')
if issued:
issued[0].debug()
sys.exit(1)

total_tests = len(target_unit_tests)
print('')
print('running specific test(s) (total: {})'.format(
len(target_unit_tests)))
print(f'running specific test(s) (total: {total_tests})')
for target_unit_test in target_unit_tests:
print(f' {target_unit_test.id()}')
print('')
Expand Down
Loading

0 comments on commit ac473fa

Please sign in to comment.