Reformat everything with black.

I had to undo the u string prefix removals to not drop Python 2 compatibility.
That's why black isn't enabled in antsibull-nox.toml yet.
This commit is contained in:
Felix Fontein
2025-04-28 09:51:33 +02:00
parent 04a0d38e3b
commit aec1826c34
118 changed files with 11780 additions and 7565 deletions

View File

@@ -16,28 +16,28 @@ import tempfile
def load_file(path, module=None):
'''
"""
Load the file as a bytes string.
'''
"""
try:
with open(path, 'rb') as f:
with open(path, "rb") as f:
return f.read()
except Exception as exc:
if module is None:
raise
module.fail_json('Error while loading {0} - {1}'.format(path, str(exc)))
module.fail_json("Error while loading {0} - {1}".format(path, str(exc)))
def load_file_if_exists(path, module=None, ignore_errors=False):
'''
"""
Load the file as a bytes string. If the file does not exist, ``None`` is returned.
If ``ignore_errors`` is ``True``, will ignore errors. Otherwise, errors are
raised as exceptions if ``module`` is not specified, and result in ``module.fail_json``
being called when ``module`` is specified.
'''
"""
try:
with open(path, 'rb') as f:
with open(path, "rb") as f:
return f.read()
except EnvironmentError as exc:
if exc.errno == errno.ENOENT:
@@ -46,20 +46,20 @@ def load_file_if_exists(path, module=None, ignore_errors=False):
return None
if module is None:
raise
module.fail_json('Error while loading {0} - {1}'.format(path, str(exc)))
module.fail_json("Error while loading {0} - {1}".format(path, str(exc)))
except Exception as exc:
if ignore_errors:
return None
if module is None:
raise
module.fail_json('Error while loading {0} - {1}'.format(path, str(exc)))
module.fail_json("Error while loading {0} - {1}".format(path, str(exc)))
def write_file(module, content, default_mode=None, path=None):
'''
"""
Writes content into destination file as securely as possible.
Uses file arguments from module.
'''
"""
# Find out parameters for file
try:
file_args = module.load_file_common_arguments(module.params, path=path)
@@ -68,11 +68,11 @@ def write_file(module, content, default_mode=None, path=None):
# pre-2.10 behavior of module_utils/crypto.py for older Ansible versions.
file_args = module.load_file_common_arguments(module.params)
if path is not None:
file_args['path'] = path
if file_args['mode'] is None:
file_args['mode'] = default_mode
file_args["path"] = path
if file_args["mode"] is None:
file_args["mode"] = default_mode
# Create tempfile name
tmp_fd, tmp_name = tempfile.mkstemp(prefix=b'.ansible_tmp')
tmp_fd, tmp_name = tempfile.mkstemp(prefix=b".ansible_tmp")
try:
os.close(tmp_fd)
except Exception:
@@ -89,18 +89,22 @@ def write_file(module, content, default_mode=None, path=None):
os.remove(tmp_name)
except Exception:
pass
module.fail_json(msg='Error while writing result into temporary file: {0}'.format(e))
module.fail_json(
msg="Error while writing result into temporary file: {0}".format(e)
)
# Update destination to wanted permissions
if os.path.exists(file_args['path']):
if os.path.exists(file_args["path"]):
module.set_fs_attributes_if_different(file_args, False)
# Move tempfile to final destination
module.atomic_move(os.path.abspath(tmp_name), os.path.abspath(file_args['path']))
module.atomic_move(
os.path.abspath(tmp_name), os.path.abspath(file_args["path"])
)
# Try to update permissions again
if not module.check_file_absent_if_check_mode(file_args['path']):
if not module.check_file_absent_if_check_mode(file_args["path"]):
module.set_fs_attributes_if_different(file_args, False)
except Exception as e:
try:
os.remove(tmp_name)
except Exception:
pass
module.fail_json(msg='Error while writing result: {0}'.format(e))
module.fail_json(msg="Error while writing result: {0}".format(e))