mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-09-30 21:43:22 -07:00
plugins: replace to_native(), to_text(), str() with str() where possible or leave it away in f-string formatting (#9379)
* Replace to_native(), to_text(), str() with str() where possible or leave it away in f-string formatting. * Improve formulation. Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Use more f-strings. * Remove unicode prefix for strings. --------- Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
4b4e4b7e0a
commit
2203560867
34 changed files with 111 additions and 99 deletions
|
@ -118,7 +118,6 @@ password: secure
|
|||
import socket
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, to_safe_group_name
|
||||
from ansible.module_utils.six import text_type
|
||||
|
||||
|
@ -377,7 +376,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
|
|||
try:
|
||||
self.inventory.set_variable(hostname, 'cobbler', make_unsafe(host))
|
||||
except ValueError as e:
|
||||
self.display.warning(f"Could not set host info for {hostname}: {to_text(e)}")
|
||||
self.display.warning(f"Could not set host info for {hostname}: {e}")
|
||||
|
||||
if self.get_option('want_ip_addresses'):
|
||||
self.inventory.set_variable(self.group, 'cobbler_ipv4_addresses', make_unsafe(ip_addresses))
|
||||
|
|
|
@ -81,7 +81,6 @@ keyed_groups:
|
|||
'''
|
||||
|
||||
from ansible.errors import AnsibleError, AnsibleParserError
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
|
||||
|
||||
from ansible_collections.community.general.plugins.plugin_utils.unsafe import make_unsafe
|
||||
|
@ -124,7 +123,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
|||
# Create groups based on variable values and add the corresponding hosts to it
|
||||
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), host_attrs, host, strict=strict)
|
||||
except Exception as e:
|
||||
raise AnsibleParserError(f'Unable to fetch hosts from GitLab API, this was the original exception: {to_native(e)}')
|
||||
raise AnsibleParserError(f'Unable to fetch hosts from GitLab API, this was the original exception: {e}')
|
||||
|
||||
def verify_file(self, path):
|
||||
"""Return the possibly of a file being consumable by this plugin."""
|
||||
|
|
|
@ -195,17 +195,15 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
p = Popen(cmd_list, stdout=PIPE, stderr=PIPE, env=my_env)
|
||||
stdout, stderr = p.communicate()
|
||||
if p.returncode != 0:
|
||||
raise AnsibleError('Failed to run cmd=%s, rc=%s, stderr=%s' %
|
||||
(cmd_list, p.returncode, to_native(stderr)))
|
||||
raise AnsibleError(f'Failed to run cmd={cmd_list}, rc={p.returncode}, stderr={to_native(stderr)}')
|
||||
|
||||
try:
|
||||
t_stdout = to_text(stdout, errors='surrogate_or_strict')
|
||||
except UnicodeError as e:
|
||||
raise AnsibleError('Invalid (non unicode) input returned: %s' % to_native(e)) from e
|
||||
raise AnsibleError(f'Invalid (non unicode) input returned: {e}') from e
|
||||
|
||||
except Exception as e:
|
||||
raise AnsibleParserError('Failed to parse %s: %s' %
|
||||
(to_native(path), to_native(e))) from e
|
||||
raise AnsibleParserError(f'Failed to parse {to_native(path)}: {e}') from e
|
||||
|
||||
results = {'_meta': {'hostvars': {}}}
|
||||
self.get_jails(t_stdout, results)
|
||||
|
@ -220,16 +218,16 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
p = Popen(cmd_get_properties, stdout=PIPE, stderr=PIPE, env=my_env)
|
||||
stdout, stderr = p.communicate()
|
||||
if p.returncode != 0:
|
||||
raise AnsibleError('Failed to run cmd=%s, rc=%s, stderr=%s' %
|
||||
(cmd_get_properties, p.returncode, to_native(stderr)))
|
||||
raise AnsibleError(
|
||||
f'Failed to run cmd={cmd_get_properties}, rc={p.returncode}, stderr={to_native(stderr)}')
|
||||
|
||||
try:
|
||||
t_stdout = to_text(stdout, errors='surrogate_or_strict')
|
||||
except UnicodeError as e:
|
||||
raise AnsibleError('Invalid (non unicode) input returned: %s' % to_native(e)) from e
|
||||
raise AnsibleError(f'Invalid (non unicode) input returned: {e}') from e
|
||||
|
||||
except Exception as e:
|
||||
raise AnsibleError('Failed to get properties: %s' % to_native(e)) from e
|
||||
raise AnsibleError(f'Failed to get properties: {e}') from e
|
||||
|
||||
self.get_properties(t_stdout, results, hostname)
|
||||
|
||||
|
|
|
@ -211,7 +211,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
with open(path, 'r') as json_file:
|
||||
return json.load(json_file)
|
||||
except (IOError, json.decoder.JSONDecodeError) as err:
|
||||
raise AnsibleParserError(f'Could not load the test data from {to_native(path)}: {to_native(err)}')
|
||||
raise AnsibleParserError(f'Could not load the test data from {to_native(path)}: {err}')
|
||||
|
||||
def save_json_data(self, path, file_name=None):
|
||||
"""save data as json
|
||||
|
@ -241,7 +241,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
with open(os.path.abspath(os.path.join(cwd, *path)), 'w') as json_file:
|
||||
json.dump(self.data, json_file)
|
||||
except IOError as err:
|
||||
raise AnsibleParserError(f'Could not save data: {to_native(err)}')
|
||||
raise AnsibleParserError(f'Could not save data: {err}')
|
||||
|
||||
def verify_file(self, path):
|
||||
"""Check the config
|
||||
|
@ -281,7 +281,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
if not isinstance(url, str):
|
||||
return False
|
||||
if not url.startswith(('unix:', 'https:')):
|
||||
raise AnsibleError(f'URL is malformed: {to_native(url)}')
|
||||
raise AnsibleError(f'URL is malformed: {url}')
|
||||
return True
|
||||
|
||||
def _connect_to_socket(self):
|
||||
|
@ -306,7 +306,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
return socket_connection
|
||||
except LXDClientException as err:
|
||||
error_storage[url] = err
|
||||
raise AnsibleError(f'No connection to the socket: {to_native(error_storage)}')
|
||||
raise AnsibleError(f'No connection to the socket: {error_storage}')
|
||||
|
||||
def _get_networks(self):
|
||||
"""Get Networknames
|
||||
|
@ -579,7 +579,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
else:
|
||||
path[instance_name][key] = value
|
||||
except KeyError as err:
|
||||
raise AnsibleParserError(f"Unable to store Information: {to_native(err)}")
|
||||
raise AnsibleParserError(f"Unable to store Information: {err}")
|
||||
|
||||
def extract_information_from_instance_configs(self):
|
||||
"""Process configuration information
|
||||
|
@ -792,7 +792,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
network = ipaddress.ip_network(to_text(self.groupby[group_name].get('attribute')))
|
||||
except ValueError as err:
|
||||
raise AnsibleParserError(
|
||||
f"Error while parsing network range {self.groupby[group_name].get('attribute')}: {to_native(err)}")
|
||||
f"Error while parsing network range {self.groupby[group_name].get('attribute')}: {err}")
|
||||
|
||||
for instance_name in self.inventory.hosts:
|
||||
if self.data['inventory'][instance_name].get('network_interfaces') is not None:
|
||||
|
@ -1120,6 +1120,6 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
self.url = self.get_option('url')
|
||||
except Exception as err:
|
||||
raise AnsibleParserError(
|
||||
f'All correct options required: {to_native(err)}')
|
||||
f'All correct options required: {err}')
|
||||
# Call our internal helper to populate the dynamic inventory
|
||||
self._populate()
|
||||
|
|
|
@ -178,7 +178,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
try:
|
||||
self._nmap = get_bin_path('nmap')
|
||||
except ValueError as e:
|
||||
raise AnsibleParserError(f'nmap inventory plugin requires the nmap cli tool to work: {to_native(e)}')
|
||||
raise AnsibleParserError(f'nmap inventory plugin requires the nmap cli tool to work: {e}')
|
||||
|
||||
super(InventoryModule, self).parse(inventory, loader, path, cache=cache)
|
||||
|
||||
|
@ -259,7 +259,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
try:
|
||||
t_stdout = to_text(stdout, errors='surrogate_or_strict')
|
||||
except UnicodeError as e:
|
||||
raise AnsibleParserError(f'Invalid (non unicode) input returned: {to_native(e)}')
|
||||
raise AnsibleParserError(f'Invalid (non unicode) input returned: {e}')
|
||||
|
||||
for line in t_stdout.splitlines():
|
||||
hits = self.find_host.match(line)
|
||||
|
@ -300,7 +300,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
results[-1]['ports'] = ports
|
||||
|
||||
except Exception as e:
|
||||
raise AnsibleParserError(f"failed to parse {to_native(path)}: {to_native(e)} ")
|
||||
raise AnsibleParserError(f"failed to parse {to_native(path)}: {e} ")
|
||||
|
||||
if cache_needs_update:
|
||||
self._cache[cache_key] = results
|
||||
|
|
|
@ -96,7 +96,6 @@ except ImportError:
|
|||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
|
||||
from ansible_collections.community.general.plugins.plugin_utils.unsafe import make_unsafe
|
||||
|
||||
|
@ -172,7 +171,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
|||
try:
|
||||
vm_pool = one_client.vmpool.infoextended(-2, -1, -1, 3)
|
||||
except Exception as e:
|
||||
raise AnsibleError(f"Something happened during XML-RPC call: {to_native(e)}")
|
||||
raise AnsibleError(f"Something happened during XML-RPC call: {e}")
|
||||
|
||||
return vm_pool
|
||||
|
||||
|
|
|
@ -222,7 +222,6 @@ from ansible.module_utils.common._collections_compat import MutableMapping
|
|||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
||||
from ansible.utils.display import Display
|
||||
|
@ -523,7 +522,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
if not self._compose(host_filter, properties):
|
||||
return False
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
message = f"Could not evaluate host filter {host_filter} for host {name} - {to_native(e)}"
|
||||
message = f"Could not evaluate host filter {host_filter} for host {name} - {e}"
|
||||
if self.strict:
|
||||
raise AnsibleError(message)
|
||||
display.warning(message)
|
||||
|
|
|
@ -125,7 +125,7 @@ from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
|
|||
from ansible_collections.community.general.plugins.module_utils.scaleway import SCALEWAY_LOCATION, parse_pagination_link
|
||||
from ansible_collections.community.general.plugins.plugin_utils.unsafe import make_unsafe
|
||||
from ansible.module_utils.urls import open_url
|
||||
from ansible.module_utils.common.text.converters import to_native, to_text
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
from ansible.module_utils.six import raise_from
|
||||
|
||||
import ansible.module_utils.six.moves.urllib.parse as urllib_parse
|
||||
|
@ -140,7 +140,7 @@ def _fetch_information(token, url):
|
|||
headers={'X-Auth-Token': token,
|
||||
'Content-type': 'application/json'})
|
||||
except Exception as e:
|
||||
raise AnsibleError(f"Error while fetching {url}: {to_native(e)}")
|
||||
raise AnsibleError(f"Error while fetching {url}: {e}")
|
||||
try:
|
||||
raw_json = json.loads(to_text(response.read()))
|
||||
except ValueError:
|
||||
|
|
|
@ -76,7 +76,7 @@ import os
|
|||
from subprocess import Popen, PIPE
|
||||
|
||||
from ansible.errors import AnsibleParserError
|
||||
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
|
||||
from ansible.module_utils.common.text.converters import to_bytes, to_text
|
||||
from ansible.module_utils.common._collections_compat import MutableMapping
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
|
||||
from ansible.module_utils.common.process import get_bin_path
|
||||
|
@ -352,7 +352,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
try:
|
||||
p = Popen(cmd, stdout=PIPE)
|
||||
except Exception as e:
|
||||
raise AnsibleParserError(to_native(e))
|
||||
raise AnsibleParserError(str(e))
|
||||
|
||||
source_data = p.stdout.read().splitlines()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue