From 3ab7a898c66d48c0b0984ea8bb924b35a77d1f0d Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Tue, 24 Jun 2025 07:10:19 +1200 Subject: [PATCH] replace concatenations with f-string in plugins (#10285) * replace concatenations with f-string in plugins * add changelog frag --- changelogs/fragments/10285-fstr-plugins.yml | 7 +++++++ plugins/callback/dense.py | 8 ++------ plugins/callback/mail.py | 3 ++- plugins/connection/wsl.py | 6 ++++-- plugins/filter/jc.py | 4 ++-- plugins/inventory/iocage.py | 2 +- plugins/inventory/xen_orchestra.py | 4 ++-- 7 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 changelogs/fragments/10285-fstr-plugins.yml diff --git a/changelogs/fragments/10285-fstr-plugins.yml b/changelogs/fragments/10285-fstr-plugins.yml new file mode 100644 index 0000000000..6fff590fee --- /dev/null +++ b/changelogs/fragments/10285-fstr-plugins.yml @@ -0,0 +1,7 @@ +minor_changes: + - dense callback plugin - use f-strings instead of concatenation (https://github.com/ansible-collections/community.general/pull/10285). + - mail callback plugin - use f-strings instead of concatenation (https://github.com/ansible-collections/community.general/pull/10285). + - wsl connection plugin - use f-strings instead of concatenation (https://github.com/ansible-collections/community.general/pull/10285). + - jc filter plugin - use f-strings instead of concatenation (https://github.com/ansible-collections/community.general/pull/10285). + - iocage inventory plugin - use f-strings instead of concatenation (https://github.com/ansible-collections/community.general/pull/10285). + - xen_orchestra inventory plugin - use f-strings instead of concatenation (https://github.com/ansible-collections/community.general/pull/10285). diff --git a/plugins/callback/dense.py b/plugins/callback/dense.py index 67cad4fd8f..052bd4ea6f 100644 --- a/plugins/callback/dense.py +++ b/plugins/callback/dense.py @@ -263,12 +263,8 @@ class CallbackModule(CallbackModule_default): sys.stdout.write(colors[self.hosts[name]['state']] + name + vt100.reset) sys.stdout.flush() -# if result._result.get('diff', False): -# sys.stdout.write('\n' + vt100.linewrap) sys.stdout.write(vt100.linewrap) -# self.keep = True - def _display_task_banner(self): if not self.shown_title: self.shown_title = True @@ -312,12 +308,12 @@ class CallbackModule(CallbackModule_default): delegated_vars = result._result.get('_ansible_delegated_vars', None) if delegated_vars: - sys.stdout.write(f"{vt100.reset + result._host.get_name()}>{colors[status]}{delegated_vars['ansible_host']}") + sys.stdout.write(f"{vt100.reset}{result._host.get_name()}>{colors[status]}{delegated_vars['ansible_host']}") else: sys.stdout.write(result._host.get_name()) sys.stdout.write(f": {dump}\n") - sys.stdout.write(vt100.reset + vt100.save + vt100.clearline) + sys.stdout.write(f"{vt100.reset}{vt100.save}{vt100.clearline}") sys.stdout.flush() if status == 'changed': diff --git a/plugins/callback/mail.py b/plugins/callback/mail.py index 80bef26044..eb88e4dcb6 100644 --- a/plugins/callback/mail.py +++ b/plugins/callback/mail.py @@ -212,7 +212,8 @@ class CallbackModule(CallbackBase): if self.itembody: body += self.itembody elif result._result.get('failed_when_result') is True: - fail_cond = self.indent('failed_when:\n- ' + '\n- '.join(result._task.failed_when)) + fail_cond_list = '\n- '.join(result._task.failed_when) + fail_cond = self.indent(f"failed_when:\n- {fail_cond_list}") body += f"due to the following condition:\n\n{fail_cond}\n\n" elif result._result.get('msg'): body += self.body_blob(result._result['msg'], 'message') diff --git a/plugins/connection/wsl.py b/plugins/connection/wsl.py index 06c0652312..6f2fec5102 100644 --- a/plugins/connection/wsl.py +++ b/plugins/connection/wsl.py @@ -522,8 +522,10 @@ class Connection(ConnectionBase): if u'PID check failed' in msg: raise AnsibleError('paramiko version issue, please upgrade paramiko on the machine running ansible') elif u'Private key file is encrypted' in msg: - msg = f'ssh {self.get_option("remote_user")}@{self.get_options("remote_addr")}:{port} : ' + \ + msg = ( + f'ssh {self.get_option("remote_user")}@{self.get_options("remote_addr")}:{port} : ' f'{msg}\nTo connect as a different user, use -u .' + ) raise AnsibleConnectionFailure(msg) else: raise AnsibleConnectionFailure(msg) @@ -656,7 +658,7 @@ class Connection(ConnectionBase): chan.shutdown_write() except socket.timeout: - raise AnsibleError('ssh timed out waiting for privilege escalation.\n' + to_text(become_output)) + raise AnsibleError(f'ssh timed out waiting for privilege escalation.\n{to_text(become_output)}') stdout = b''.join(chan.makefile('rb', bufsize)) stderr = b''.join(chan.makefile_stderr('rb', bufsize)) diff --git a/plugins/filter/jc.py b/plugins/filter/jc.py index 48d53bcbd3..6a2feb93f0 100644 --- a/plugins/filter/jc.py +++ b/plugins/filter/jc.py @@ -143,11 +143,11 @@ def jc_filter(data, parser, quiet=True, raw=False): # old API (jc v1.17.7 and lower) else: - jc_parser = importlib.import_module('jc.parsers.' + parser) + jc_parser = importlib.import_module(f'jc.parsers.{parser}') return jc_parser.parse(data, quiet=quiet, raw=raw) except Exception as e: - raise AnsibleFilterError('Error in jc filter plugin: %s' % e) + raise AnsibleFilterError(f'Error in jc filter plugin: {e}') class FilterModule(object): diff --git a/plugins/inventory/iocage.py b/plugins/inventory/iocage.py index 8e296b571a..15621aa2dd 100644 --- a/plugins/inventory/iocage.py +++ b/plugins/inventory/iocage.py @@ -350,7 +350,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): for hostname, host_vars in results['_meta']['hostvars'].items(): iocage_hooks = [] for hook in hooks_results: - path = "/" + iocage_pool + "/iocage/jails/" + hostname + "/root" + hook + path = f"/{iocage_pool}/iocage/jails/{hostname}/root{hook}" cmd_cat_hook = cmd.copy() cmd_cat_hook.append('cat') cmd_cat_hook.append(path) diff --git a/plugins/inventory/xen_orchestra.py b/plugins/inventory/xen_orchestra.py index ce45b161c9..3ad7767542 100644 --- a/plugins/inventory/xen_orchestra.py +++ b/plugins/inventory/xen_orchestra.py @@ -224,7 +224,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): vm_name_list.append(vm['name_label']) else: vm_duplicate_count = vm_name_list.count(vm['name_label']) - entry_name = vm['name_label'] + "_" + str(vm_duplicate_count) + entry_name = f"{vm['name_label']}_{vm_duplicate_count}" vm_name_list.append(vm['name_label']) else: entry_name = uuid @@ -284,7 +284,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): host_name_list.append(host['name_label']) else: host_duplicate_count = host_name_list.count(host['name_label']) - entry_name = host['name_label'] + "_" + str(host_duplicate_count) + entry_name = f"{host['name_label']}_{host_duplicate_count}" host_name_list.append(host['name_label']) else: entry_name = host['uuid']