Skip to content

Commit 2754702

Browse files
authored
Merge pull request #760 from sphinx-contrib/code-cleanup
code cleanup to use python 3 capabilities
2 parents 0d1383b + bb63277 commit 2754702

File tree

91 files changed

+254
-268
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+254
-268
lines changed

sphinxcontrib/confluencebuilder/assets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def add(self, path, docname):
8484
"""
8585
logger.verbose('adding manual attachment: %s' % path)
8686
abspath = find_env_abspath(self.env, self.outdir, path)
87-
return self._handle_entry(abspath, docname, True)
87+
return self._handle_entry(abspath, docname, standalone=True)
8888

8989
def build(self):
9090
"""
@@ -295,7 +295,7 @@ def _handle_entry(self, path, docname, standalone=False):
295295
idx = 1
296296
while key in self.keys:
297297
idx += 1
298-
key = '{}_{}{}'.format(filename, idx, file_ext)
298+
key = f'{filename}_{idx}{file_ext}'
299299
self.keys.add(key)
300300

301301
asset = ConfluenceAsset(key, path, type_, hash_)

sphinxcontrib/confluencebuilder/builder.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
from sphinxcontrib.confluencebuilder.util import first
3939
from sphinxcontrib.confluencebuilder.util import handle_cli_file_subset
4040
from sphinxcontrib.confluencebuilder.writer import ConfluenceWriter
41-
import io
4241
import os
4342
import tempfile
4443

@@ -57,10 +56,10 @@ def __init__(self, app, env=None):
5756
# assigned.
5857
if sphinx_version_info >= (5, 1):
5958
# pylint: disable=too-many-function-args
60-
super(ConfluenceBuilder, self).__init__(app, env)
59+
super().__init__(app, env)
6160
# pylint: enable=too-many-function-args
6261
else:
63-
super(ConfluenceBuilder, self).__init__(app)
62+
super().__init__(app)
6463

6564
self.cache_doctrees = {}
6665
self.cloud = False
@@ -252,7 +251,7 @@ def prepare_writing(self, docnames):
252251
for domain_name in sorted(self.env.domains):
253252
domain = self.env.domains[domain_name]
254253
for indexcls in domain.indices:
255-
indexname = '%s-%s' % (domain.name, indexcls.name)
254+
indexname = f'{domain.name}-{indexcls.name}'
256255

257256
if isinstance(indices_config, list):
258257
if indexname not in indices_config:
@@ -477,11 +476,11 @@ def write_doc(self, docname, doctree):
477476
if self.writer.output is not None:
478477
ensuredir(path.dirname(outfilename))
479478
try:
480-
with io.open(outfilename, 'w', encoding='utf-8') as file:
479+
with open(outfilename, 'w', encoding='utf-8') as file:
481480
if self.writer.output:
482481
file.write(self.writer.output)
483482
except (IOError, OSError) as err:
484-
self.warn('error writing file %s: %s' % (outfilename, err))
483+
self.warn(f'error writing file {outfilename}: {err}')
485484

486485
def publish_doc(self, docname, output):
487486
conf = self.config
@@ -698,7 +697,7 @@ def finish(self):
698697
# build domain indexes
699698
if self.domain_indices:
700699
for indexname, indexdata in self.domain_indices.items():
701-
self.info('generating index ({})...'.format(indexname),
700+
self.info(f'generating index ({indexname})...',
702701
nonl=(not self._verbose))
703702

704703
self._generate_special_document(indexname,
@@ -733,12 +732,12 @@ def finish(self):
733732
docfile = path.join(self.outdir, self.file_transform(docname))
734733

735734
try:
736-
with io.open(docfile, 'r', encoding='utf-8') as file:
735+
with open(docfile, 'r', encoding='utf-8') as file:
737736
output = file.read()
738737
self.publish_doc(docname, output)
739738

740739
except (IOError, OSError) as err:
741-
self.warn('error reading file %s: %s' % (docfile, err))
740+
self.warn(f'error reading file {docfile}: {err}')
742741

743742
self.info('building intersphinx... ', nonl=(not self._verbose))
744743
build_intersphinx(self)
@@ -769,7 +768,7 @@ def to_asset_name(asset):
769768
output = file.read()
770769
self.publish_asset(key, docname, output, type_, hash_)
771770
except (IOError, OSError) as err:
772-
self.warn('error reading asset %s: %s' % (key, err))
771+
self.warn(f'error reading asset {key}: {err}')
773772

774773
self.publish_cleanup()
775774
self.publish_finalize()
@@ -864,10 +863,10 @@ def _generate_special_document(self, docname, generator):
864863
fname = path.join(self.env.srcdir,
865864
self.config.confluence_header_file)
866865
try:
867-
with io.open(fname, encoding='utf-8') as file:
866+
with open(fname, encoding='utf-8') as file:
868867
header_template_data = file.read() + '\n'
869868
except (IOError, OSError) as err:
870-
self.warn('error reading file {}: {}'.format(fname, err))
869+
self.warn(f'error reading file {fname}: {err}')
871870

872871
# if no data is supplied, the file is plain text
873872
if self.config.confluence_header_data is None:
@@ -886,10 +885,10 @@ def _generate_special_document(self, docname, generator):
886885
fname = path.join(self.env.srcdir,
887886
self.config.confluence_footer_file)
888887
try:
889-
with io.open(fname, encoding='utf-8') as file:
888+
with open(fname, encoding='utf-8') as file:
890889
footer_template_data = file.read() + '\n'
891890
except (IOError, OSError) as err:
892-
self.warn('error reading file {}: {}'.format(fname, err))
891+
self.warn(f'error reading file {fname}: {err}')
893892

894893
# if no data is supplied, the file is plain text
895894
if self.config.confluence_footer_data is None:
@@ -902,7 +901,7 @@ def _generate_special_document(self, docname, generator):
902901
# generate/replace the document in the output directory
903902
fname = path.join(self.outdir, docname + self.file_suffix)
904903
try:
905-
with io.open(fname, 'w', encoding='utf-8') as f:
904+
with open(fname, 'w', encoding='utf-8') as f:
906905
f.write(self._cached_header_data)
907906
generator(self, docname, f)
908907
f.write(self._cached_footer_data)
@@ -973,7 +972,7 @@ def _header_footer_init(self, docname, doctree):
973972
else:
974973
# unsupported source type should not pass here after this
975974
# extension's configuration check
976-
assert False
975+
raise AssertionError('unsupported source type')
977976

978977
sourcelink['url'] = url_base + url
979978

@@ -1085,10 +1084,10 @@ def _register_doctree_title_targets(self, docname, doctree):
10851084
section_id = doc_used_names.get(target, 0)
10861085
doc_used_names[target] = section_id + 1
10871086
if section_id > 0:
1088-
target = '{}.{}'.format(target, section_id)
1087+
target = f'{target}.{section_id}'
10891088

10901089
for id_ in section_node['ids']:
1091-
id_ = '{}#{}'.format(docname, id_)
1090+
id_ = f'{docname}#{id_}'
10921091
self.state.register_target(id_, target)
10931092

10941093
def _top_ref_check(self, node):
@@ -1121,7 +1120,7 @@ def _parse_doctree_title(self, docname, doctree):
11211120

11221121
if not doctitle:
11231122
if not self.config.confluence_disable_autogen_title:
1124-
doctitle = "autogen-{}".format(docname)
1123+
doctitle = f'autogen-{docname}'
11251124
if self.publish:
11261125
self.warn('document will be published using an '
11271126
'generated title value: {}'.format(docname))

sphinxcontrib/confluencebuilder/cmd/build.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: BSD-2-Clause
22
# Copyright 2020-2023 Sphinx Confluence Builder Contributors (AUTHORS)
33

4+
from contextlib import suppress
45
from sphinx.application import Sphinx
56
from sphinx.util.docutils import docutils_namespace
67
from sphinxcontrib.confluencebuilder.logger import ConfluenceLogger as logger
@@ -51,10 +52,8 @@ def build_main(args_parser):
5152

5253
verbosity = 0
5354
if args.verbose:
54-
try:
55+
with suppress(ValueError):
5556
verbosity = int(args.verbose)
56-
except ValueError:
57-
pass
5857

5958
# run sphinx engine
6059
with docutils_namespace():

sphinxcontrib/confluencebuilder/cmd/report.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def report_main(args_parser):
187187
else:
188188
logger.error('bad response from server ({})'.format(
189189
rsp.status_code))
190-
info += ' fetched: error ({})\n'.format(rsp.status_code)
190+
info += f' fetched: error ({rsp.status_code})\n'
191191
rv = 1
192192
except Exception:
193193
sys.stdout.flush()
@@ -279,7 +279,7 @@ def sensitive_config(key):
279279
print('(configuration)')
280280
if config:
281281
for k, v in OrderedDict(sorted(config.items())).items():
282-
print('{}: {}'.format(k, v))
282+
print(f'{k}: {v}')
283283
else:
284284
print('~default configuration~')
285285

sphinxcontrib/confluencebuilder/config/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def process_ask_configs(config):
5050
default_user = config.confluence_server_user
5151
u_str = ''
5252
if default_user:
53-
u_str = ' [{}]'.format(default_user)
53+
u_str = f' [{default_user}]'
5454

55-
target_user = input(' User{}: '.format(u_str)) or default_user
55+
target_user = input(f' User{u_str}: ') or default_user
5656
if not target_user:
5757
raise ConfluenceConfigurationError('no user provided')
5858

sphinxcontrib/confluencebuilder/config/notifications.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def deprecated(validator):
4545
# inform users of a deprecated configuration being used
4646
for key, msg in DEPRECATED_CONFIGS.items():
4747
if config[key] is not None:
48-
logger.warn('%s deprecated; %s' % (key, msg))
48+
logger.warn(f'{key} deprecated; {msg}')
4949

5050

5151
def warnings(validator):

sphinxcontrib/confluencebuilder/config/validation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def docnames(self):
163163
os.path.join(self.env.srcdir, docname + suffix))
164164
for suffix in self.config.source_suffix):
165165
raise ConfluenceConfigurationError(
166-
'%s is missing document %s' % (self.key, docname))
166+
f'{self.key} is missing document {docname}')
167167

168168
return self
169169

@@ -198,7 +198,7 @@ def docnames_from_file(self):
198198
os.path.join(self.env.srcdir, docname + suffix))
199199
for suffix in self.config.source_suffix):
200200
raise ConfluenceConfigurationError(
201-
'%s is missing document %s' % (self.key, docname))
201+
f'{self.key} is missing document {docname}')
202202

203203
return self
204204

sphinxcontrib/confluencebuilder/exceptions.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ConfluenceError(SphinxError):
1111

1212
class ConfluenceAuthenticationFailedUrlError(ConfluenceError):
1313
def __init__(self):
14-
super(ConfluenceAuthenticationFailedUrlError, self).__init__('''
14+
super().__init__('''
1515
---
1616
Unable to authenticate with Confluence
1717
@@ -25,7 +25,7 @@ def __init__(self):
2525

2626
class ConfluenceBadApiError(ConfluenceError):
2727
def __init__(self, code, details):
28-
super(ConfluenceBadApiError, self).__init__('''
28+
super().__init__('''
2929
---
3030
Unsupported Confluence API call
3131
@@ -43,7 +43,7 @@ def __init__(self, space_key, uname, pw_set, token_set, extras):
4343
uname_value = uname if uname else '(empty)'
4444
pw_value = '<set>' if pw_set else '(empty)'
4545
token_value = '<set>' if token_set else '(empty)'
46-
super(ConfluenceBadSpaceError, self).__init__('''
46+
super().__init__('''
4747
---
4848
Invalid Confluence URL detected
4949
@@ -66,7 +66,7 @@ def __init__(self, space_key, uname, pw_set, token_set, extras):
6666

6767
class ConfluenceBadServerUrlError(ConfluenceError):
6868
def __init__(self, server_url, ex):
69-
super(ConfluenceBadServerUrlError, self).__init__('''
69+
super().__init__('''
7070
---
7171
Invalid Confluence URL detected
7272
@@ -83,7 +83,7 @@ def __init__(self, server_url, ex):
8383

8484
class ConfluenceCertificateError(ConfluenceError):
8585
def __init__(self, ex):
86-
super(ConfluenceCertificateError, self).__init__('''
86+
super().__init__('''
8787
---
8888
SSL certificate issue
8989
@@ -101,7 +101,7 @@ class ConfluenceConfigurationError(ConfluenceError, ConfigError):
101101

102102
class ConfluenceMissingPageIdError(ConfluenceError):
103103
def __init__(self, space_key, page_id):
104-
super(ConfluenceMissingPageIdError, self).__init__('''
104+
super().__init__('''
105105
---
106106
Unable to find a requested page
107107
@@ -117,7 +117,7 @@ def __init__(self, space_key, page_id):
117117

118118
class ConfluencePermissionError(ConfluenceError):
119119
def __init__(self, details):
120-
super(ConfluencePermissionError, self).__init__('''
120+
super().__init__('''
121121
---
122122
Permission denied on Confluence ({desc})
123123
@@ -130,7 +130,7 @@ def __init__(self, details):
130130

131131
class ConfluenceProxyPermissionError(ConfluenceError):
132132
def __init__(self):
133-
super(ConfluenceProxyPermissionError, self).__init__('''
133+
super().__init__('''
134134
---
135135
Unable to authenticate with the proxy server
136136
@@ -148,7 +148,7 @@ class ConfluencePublishCheckError(ConfluenceError):
148148

149149
class ConfluencePublishAncestorError(ConfluencePublishCheckError):
150150
def __init__(self, page_name):
151-
super(ConfluencePublishAncestorError, self).__init__('''
151+
super().__init__('''
152152
---
153153
Ancestor publish check failed for: {name}
154154
@@ -177,7 +177,7 @@ def __init__(self, page_name):
177177

178178
class ConfluencePublishSelfAncestorError(ConfluencePublishCheckError):
179179
def __init__(self, page_name):
180-
super(ConfluencePublishSelfAncestorError, self).__init__('''
180+
super().__init__('''
181181
---
182182
Ancestor (self) publish check failed for: {name}
183183
@@ -198,7 +198,7 @@ def __init__(self, page_name):
198198

199199
class ConfluenceRateLimited(ConfluenceError):
200200
def __init__(self):
201-
super(ConfluenceRateLimited, self).__init__('''
201+
super().__init__('''
202202
---
203203
Request has been rate limited
204204
@@ -211,7 +211,7 @@ def __init__(self):
211211

212212
class ConfluenceSeraphAuthenticationFailedUrlError(ConfluenceError):
213213
def __init__(self):
214-
super(ConfluenceSeraphAuthenticationFailedUrlError, self).__init__('''
214+
super().__init__('''
215215
---
216216
Unable to authenticate with the Confluence instance (Seraph)
217217
@@ -226,7 +226,7 @@ def __init__(self):
226226

227227
class ConfluenceSslError(ConfluenceError):
228228
def __init__(self, server_url, ex):
229-
super(ConfluenceSslError, self).__init__('''
229+
super().__init__('''
230230
---
231231
SSL connection issue has been detected
232232
@@ -249,7 +249,7 @@ def __init__(self, server_url, ex):
249249

250250
class ConfluenceTimeoutError(ConfluenceError):
251251
def __init__(self, server_url):
252-
super(ConfluenceTimeoutError, self).__init__('''
252+
super().__init__('''
253253
---
254254
Connection has timed out
255255
@@ -265,7 +265,7 @@ def __init__(self, server_url):
265265

266266
class ConfluenceUnreconciledPageError(ConfluenceError):
267267
def __init__(self, page_name, page_id, url, ex):
268-
super(ConfluenceUnreconciledPageError, self).__init__('''
268+
super().__init__('''
269269
---
270270
Unable to update unreconciled page: {name} (id: {id})
271271

0 commit comments

Comments
 (0)