1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00

[PR #11112/f5c2c8b9 backport][stable-12] replace redundant to_native()/to_text() occurrences, batch 7 (#11142)

replace redundant to_native()/to_text() occurrences, batch 7 (#11112)

* replace redundant to_native()/to_text() occurrences, batch 7

* add changelog frag

* made changes per review

(cherry picked from commit f5c2c8b9a2)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2025-11-13 06:09:17 +01:00 committed by GitHub
parent f1d9a2b134
commit 04fb53e8a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 41 additions and 38 deletions

View file

@ -0,0 +1,15 @@
minor_changes:
- csv module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- deps module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- gandi_livedns_api module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- ibm_sa_utils module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- keycloak module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- ipa module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- ldap module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- lxd module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- exceptions module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- ocapi_utils module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- oneview module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- redfish_utils module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- bitbucket module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).
- utm_utils module utils - remove redundant conversions to unicode (https://github.com/ansible-collections/community.general/pull/11112).

View file

@ -52,7 +52,7 @@ def initialize_dialect(dialect, **kwargs):
def read_csv(data, dialect, fieldnames=None):
BOM = to_native("\ufeff")
BOM = "\ufeff"
data = to_native(data, errors="surrogate_or_strict")
if data.startswith(BOM):
data = data[len(BOM) :]

View file

@ -9,7 +9,6 @@ from __future__ import annotations
import traceback
from contextlib import contextmanager
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.basic import missing_required_lib
@ -40,7 +39,7 @@ class _Dependency:
@property
def message(self):
if self.msg:
return to_native(self.msg)
return str(self.msg)
else:
return missing_required_lib(self.name, reason=self.reason, url=self.url)

View file

@ -6,7 +6,6 @@ from __future__ import annotations
import json
from ansible.module_utils.common.text.converters import to_text
from ansible.module_utils.urls import fetch_url
@ -70,7 +69,7 @@ class GandiLiveDNSAPI:
if content:
try:
result = json.loads(to_text(content, errors="surrogate_or_strict"))
result = json.loads(content)
except getattr(json, "JSONDecodeError", ValueError) as e:
error_msg += f"; Failed to parse API response with error {e}: {content}"

View file

@ -9,7 +9,6 @@ from __future__ import annotations
import traceback
from functools import wraps
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.basic import missing_required_lib
PYXCLI_INSTALLED = True
@ -54,7 +53,7 @@ def xcli_wrapper(func):
try:
return func(module, *args, **kwargs)
except errors.CommandExecutionError as e:
module.fail_json(msg=to_native(e))
module.fail_json(msg=f"{e}")
return wrapper

View file

@ -200,16 +200,14 @@ def _token_request(module_params, payload):
try:
r = json.loads(
to_native(
open_url(
auth_url,
method="POST",
validate_certs=validate_certs,
http_agent=http_agent,
timeout=connection_timeout,
data=urlencode(payload),
).read()
)
open_url(
auth_url,
method="POST",
validate_certs=validate_certs,
http_agent=http_agent,
timeout=connection_timeout,
data=urlencode(payload),
).read()
)
return r["access_token"]
@ -467,7 +465,7 @@ class KeycloakAPI:
:param data: (optional) data for request
:return: raw API response
"""
return json.loads(to_native(self._request(url, method, data).read()))
return json.loads(self._request(url, method, data).read())
def get_realm_info_by_id(self, realm="master"):
"""Obtain realm public info by id

View file

@ -17,7 +17,7 @@ import socket
import uuid
import re
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.urls import fetch_url, HAS_GSSAPI
from ansible.module_utils.basic import env_fallback, AnsibleFallbackNotFound
from urllib.parse import quote
@ -91,7 +91,7 @@ class IPAClient:
self.headers = {"Cookie": info.get("set-cookie")}
except Exception as e:
self._fail("login", to_native(e))
self._fail("login", f"{e}")
if not self.headers:
self.headers = dict()
self.headers.update(
@ -144,7 +144,7 @@ class IPAClient:
if status_code not in [200, 201, 204]:
self._fail(method, info["msg"])
except Exception as e:
self._fail(f"post {method}", to_native(e))
self._fail(f"post {method}", f"{e}")
charset = resp.headers.get_content_charset("latin-1")
resp = json.loads(to_text(resp.read(), encoding=charset))

View file

@ -9,7 +9,6 @@ from __future__ import annotations
import re
import traceback
from ansible.module_utils.common.text.converters import to_native
try:
import ldap
@ -78,7 +77,7 @@ class LdapGeneric:
self.dn = self.module.params["dn"]
def fail(self, msg, exn):
self.module.fail_json(msg=msg, details=to_native(exn), exception=traceback.format_exc())
self.module.fail_json(msg=msg, details=f"{exn}", exception=traceback.format_exc())
def _find_dn(self):
dn = self.module.params["dn"]

View file

@ -13,7 +13,6 @@ import json
from urllib.parse import urlparse
from ansible.module_utils.urls import generic_urlparse
from ansible.module_utils.common.text.converters import to_text
# httplib/http.client connection using unix domain socket
HTTPConnection = http_client.HTTPConnection
@ -98,7 +97,6 @@ class LXDClient:
self.connection.request(method, url, body=body)
resp = self.connection.getresponse()
resp_data = resp.read()
resp_data = to_text(resp_data, errors="surrogate_or_strict")
resp_json = json.loads(resp_data)
self.logs.append(
{

View file

@ -7,12 +7,10 @@ from __future__ import annotations
import typing as t
from ansible.module_utils.common.text.converters import to_native
class ModuleHelperException(Exception):
def __init__(self, msg: str, update_output: dict[str, t.Any] | None = None, *args, **kwargs):
self.msg: str = to_native(msg or f"Module failed with exception: {self}")
self.msg: str = msg or f"Module failed with exception: {self}"
if update_output is None:
update_output = {}
self.update_output: dict[str, t.Any] = update_output

View file

@ -58,7 +58,7 @@ class OcapiUtils:
use_proxy=True,
timeout=self.timeout,
)
data = json.loads(to_native(resp.read()))
data = json.loads(resp.read())
headers = {k.lower(): v for (k, v) in resp.info().items()}
except HTTPError as e:
return {"ret": False, "msg": f"HTTP Error {e.code} on GET request to '{uri}'", "status": e.code}
@ -88,7 +88,7 @@ class OcapiUtils:
timeout=self.timeout,
)
if resp.status != 204:
data = json.loads(to_native(resp.read()))
data = json.loads(resp.read())
else:
data = ""
headers = {k.lower(): v for (k, v) in resp.info().items()}

View file

@ -290,7 +290,7 @@ class OneViewModuleBase(metaclass=abc.ABCMeta):
self.module.exit_json(**result)
except OneViewModuleException as exception:
error_msg = "; ".join(to_native(e) for e in exception.args)
error_msg = "; ".join(str(e) for e in exception.args)
self.module.fail_json(msg=error_msg, exception=traceback.format_exc())
def resource_absent(self, resource, method="delete"):

View file

@ -185,7 +185,7 @@ class RedfishUtils:
timeout=timeout,
)
try:
data = json.loads(to_native(resp.read()))
data = json.loads(resp.read())
except Exception:
# No response data; this is okay in certain cases
data = None
@ -232,7 +232,7 @@ class RedfishUtils:
force_basic_auth=basic_auth,
)
try:
data = json.loads(to_native(resp.read()))
data = json.loads(resp.read())
except Exception:
# No response data; this is okay in many cases
data = None
@ -407,7 +407,7 @@ class RedfishUtils:
if "filename" in fields[form]:
name = os.path.basename(fields[form]["filename"]).replace('"', '\\"')
write_buffer(
body, f'Content-Disposition: form-data; name="{to_text(form)}"; filename="{to_text(name)}"'
body, f'Content-Disposition: form-data; name="{to_text(form)}"; filename="{to_native(name)}"'
)
else:
write_buffer(body, f'Content-Disposition: form-data; name="{form}"')

View file

@ -6,7 +6,6 @@ from __future__ import annotations
import json
from ansible.module_utils.common.text.converters import to_text
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.urls import fetch_url, basic_auth_header
@ -94,7 +93,7 @@ class BitbucketHelper:
content = {}
if response is not None:
body = to_text(response.read())
body = response.read()
if body:
content = json.loads(body)

View file

@ -13,7 +13,6 @@ from __future__ import annotations
import json
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url
@ -109,7 +108,7 @@ class UTM:
else:
self._info()
except Exception as e:
self.module.fail_json(msg=to_native(e))
self.module.fail_json(msg=f"{e}")
def _info(self):
"""