mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-13 23:45:04 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -135,7 +135,7 @@ from ansible.module_utils.common.respawn import (
|
|||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible_collections.community.general.plugins.module_utils import deps
|
||||
|
||||
glib_module_name = 'gi.repository.GLib'
|
||||
glib_module_name = "gi.repository.GLib"
|
||||
|
||||
try:
|
||||
from gi.repository.GLib import Variant, GError
|
||||
|
|
@ -178,7 +178,7 @@ class DBusWrapper:
|
|||
# If no existing D-Bus session was detected, check if dbus-run-session
|
||||
# is available.
|
||||
if self.dbus_session_bus_address is None:
|
||||
self.dbus_run_session_cmd = self.module.get_bin_path('dbus-run-session', required=True)
|
||||
self.dbus_run_session_cmd = self.module.get_bin_path("dbus-run-session", required=True)
|
||||
|
||||
def _get_existing_dbus_session(self):
|
||||
"""
|
||||
|
|
@ -199,15 +199,25 @@ class DBusWrapper:
|
|||
try:
|
||||
process = psutil.Process(pid)
|
||||
process_real_uid, dummy, dummy = process.uids()
|
||||
if process_real_uid == uid and 'DBUS_SESSION_BUS_ADDRESS' in process.environ():
|
||||
dbus_session_bus_address_candidate = process.environ()['DBUS_SESSION_BUS_ADDRESS']
|
||||
self.module.debug(f"Found D-Bus user session candidate at address: {dbus_session_bus_address_candidate}")
|
||||
dbus_send_cmd = self.module.get_bin_path('dbus-send', required=True)
|
||||
command = [dbus_send_cmd, f'--address={dbus_session_bus_address_candidate}', '--type=signal', '/', 'com.example.test']
|
||||
if process_real_uid == uid and "DBUS_SESSION_BUS_ADDRESS" in process.environ():
|
||||
dbus_session_bus_address_candidate = process.environ()["DBUS_SESSION_BUS_ADDRESS"]
|
||||
self.module.debug(
|
||||
f"Found D-Bus user session candidate at address: {dbus_session_bus_address_candidate}"
|
||||
)
|
||||
dbus_send_cmd = self.module.get_bin_path("dbus-send", required=True)
|
||||
command = [
|
||||
dbus_send_cmd,
|
||||
f"--address={dbus_session_bus_address_candidate}",
|
||||
"--type=signal",
|
||||
"/",
|
||||
"com.example.test",
|
||||
]
|
||||
rc, dummy, dummy = self.module.run_command(command)
|
||||
|
||||
if rc == 0:
|
||||
self.module.debug(f"Verified D-Bus user session candidate as usable at address: {dbus_session_bus_address_candidate}")
|
||||
self.module.debug(
|
||||
f"Verified D-Bus user session candidate as usable at address: {dbus_session_bus_address_candidate}"
|
||||
)
|
||||
|
||||
return dbus_session_bus_address_candidate
|
||||
|
||||
|
|
@ -240,16 +250,17 @@ class DBusWrapper:
|
|||
rc, out, err = self.module.run_command(command)
|
||||
|
||||
if self.dbus_session_bus_address is None and rc == 127:
|
||||
self.module.fail_json(msg=f"Failed to run passed-in command, dbus-run-session faced an internal error: {err}")
|
||||
self.module.fail_json(
|
||||
msg=f"Failed to run passed-in command, dbus-run-session faced an internal error: {err}"
|
||||
)
|
||||
else:
|
||||
extra_environment = {'DBUS_SESSION_BUS_ADDRESS': self.dbus_session_bus_address}
|
||||
extra_environment = {"DBUS_SESSION_BUS_ADDRESS": self.dbus_session_bus_address}
|
||||
rc, out, err = self.module.run_command(command, environ_update=extra_environment)
|
||||
|
||||
return rc, out, err
|
||||
|
||||
|
||||
class DconfPreference:
|
||||
|
||||
def __init__(self, module, check_mode=False):
|
||||
"""
|
||||
Initialises instance of the class.
|
||||
|
|
@ -264,7 +275,7 @@ class DconfPreference:
|
|||
self.module = module
|
||||
self.check_mode = check_mode
|
||||
# Check if dconf binary exists
|
||||
self.dconf_bin = self.module.get_bin_path('dconf', required=True)
|
||||
self.dconf_bin = self.module.get_bin_path("dconf", required=True)
|
||||
|
||||
@staticmethod
|
||||
def variants_are_equal(canonical_value, user_value):
|
||||
|
|
@ -302,14 +313,12 @@ class DconfPreference:
|
|||
rc, out, err = self.module.run_command(command)
|
||||
|
||||
if rc != 0:
|
||||
self.module.fail_json(msg=f'dconf failed while reading the value with error: {err}',
|
||||
out=out,
|
||||
err=err)
|
||||
self.module.fail_json(msg=f"dconf failed while reading the value with error: {err}", out=out, err=err)
|
||||
|
||||
if out == '':
|
||||
if out == "":
|
||||
value = None
|
||||
else:
|
||||
value = out.rstrip('\n')
|
||||
value = out.rstrip("\n")
|
||||
|
||||
return value
|
||||
|
||||
|
|
@ -343,9 +352,9 @@ class DconfPreference:
|
|||
rc, out, err = dbus_wrapper.run_command(command)
|
||||
|
||||
if rc != 0:
|
||||
self.module.fail_json(msg=f'dconf failed while writing key {key}, value {value} with error: {err}',
|
||||
out=out,
|
||||
err=err)
|
||||
self.module.fail_json(
|
||||
msg=f"dconf failed while writing key {key}, value {value} with error: {err}", out=out, err=err
|
||||
)
|
||||
|
||||
# Value was changed.
|
||||
return True
|
||||
|
|
@ -381,9 +390,7 @@ class DconfPreference:
|
|||
rc, out, err = dbus_wrapper.run_command(command)
|
||||
|
||||
if rc != 0:
|
||||
self.module.fail_json(msg=f'dconf failed while resetting the value with error: {err}',
|
||||
out=out,
|
||||
err=err)
|
||||
self.module.fail_json(msg=f"dconf failed while resetting the value with error: {err}", out=out, err=err)
|
||||
|
||||
# Value was changed.
|
||||
return True
|
||||
|
|
@ -393,14 +400,14 @@ def main():
|
|||
# Setup the Ansible module
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
state=dict(default='present', choices=['present', 'absent', 'read']),
|
||||
key=dict(required=True, type='str', no_log=False),
|
||||
state=dict(default="present", choices=["present", "absent", "read"]),
|
||||
key=dict(required=True, type="str", no_log=False),
|
||||
# Converted to str below after special handling of bool.
|
||||
value=dict(type='raw'),
|
||||
value=dict(type="raw"),
|
||||
),
|
||||
supports_check_mode=True,
|
||||
required_if=[
|
||||
('state', 'present', ['value']),
|
||||
("state", "present", ["value"]),
|
||||
],
|
||||
)
|
||||
|
||||
|
|
@ -415,13 +422,11 @@ def main():
|
|||
|
||||
if has_respawned():
|
||||
# This shouldn't be possible; short-circuit early if it happens.
|
||||
module.fail_json(
|
||||
msg=f"{glib_module_name} must be installed and visible from {sys.executable}.")
|
||||
module.fail_json(msg=f"{glib_module_name} must be installed and visible from {sys.executable}.")
|
||||
|
||||
interpreters = ['/usr/bin/python3', '/usr/bin/python']
|
||||
interpreters = ["/usr/bin/python3", "/usr/bin/python"]
|
||||
|
||||
interpreter = probe_interpreters_for_module(
|
||||
interpreters, glib_module_name)
|
||||
interpreter = probe_interpreters_for_module(interpreters, glib_module_name)
|
||||
|
||||
if interpreter:
|
||||
# Found the Python bindings; respawn this module under the
|
||||
|
|
@ -435,18 +440,18 @@ def main():
|
|||
# about converting strings that look like booleans into booleans. Convert
|
||||
# the boolean into a string of the type dconf will understand. Any type for
|
||||
# the value other than boolean is just converted into a string directly.
|
||||
if module.params['value'] is not None:
|
||||
if isinstance(module.params['value'], bool):
|
||||
module.params['value'] = 'true' if module.params['value'] else 'false'
|
||||
if module.params["value"] is not None:
|
||||
if isinstance(module.params["value"], bool):
|
||||
module.params["value"] = "true" if module.params["value"] else "false"
|
||||
else:
|
||||
module.params['value'] = to_native(
|
||||
module.params['value'], errors='surrogate_or_strict')
|
||||
module.params["value"] = to_native(module.params["value"], errors="surrogate_or_strict")
|
||||
|
||||
if Variant is None:
|
||||
module.warn(
|
||||
'WARNING: The gi.repository Python library is not available; '
|
||||
'using string comparison to check value equality. This fallback '
|
||||
'will be deprecated in a future version of community.general.')
|
||||
"WARNING: The gi.repository Python library is not available; "
|
||||
"using string comparison to check value equality. This fallback "
|
||||
"will be deprecated in a future version of community.general."
|
||||
)
|
||||
|
||||
deps.validate(module)
|
||||
|
||||
|
|
@ -454,16 +459,16 @@ def main():
|
|||
dconf = DconfPreference(module, module.check_mode)
|
||||
|
||||
# Process based on different states.
|
||||
if module.params['state'] == 'read':
|
||||
value = dconf.read(module.params['key'])
|
||||
if module.params["state"] == "read":
|
||||
value = dconf.read(module.params["key"])
|
||||
module.exit_json(changed=False, value=value)
|
||||
elif module.params['state'] == 'present':
|
||||
changed = dconf.write(module.params['key'], module.params['value'])
|
||||
elif module.params["state"] == "present":
|
||||
changed = dconf.write(module.params["key"], module.params["value"])
|
||||
module.exit_json(changed=changed)
|
||||
elif module.params['state'] == 'absent':
|
||||
changed = dconf.reset(module.params['key'])
|
||||
elif module.params["state"] == "absent":
|
||||
changed = dconf.reset(module.params["key"])
|
||||
module.exit_json(changed=changed)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue