Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_xfconf.py

raw
1 # Copyright (c) 2022, Alexei Znamensky <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import typing as t
11
12 from ansible.module_utils.parsing.convert_bool import boolean
13
14 from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt
15
16 if t.TYPE_CHECKING:
17 from ansible.module_utils.basic import AnsibleModule
18
19
20 @cmd_runner_fmt.unpack_args
21 def _values_fmt(values, value_types):
22 result = []
23 for value, value_type in zip(values, value_types):
24 if value_type == "bool":
25 value = "true" if boolean(value) else "false"
26 result.extend(["--type", f"{value_type}", "--set", f"{value}"])
27 return result
28
29
30 def xfconf_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
31 runner = CmdRunner(
32 module,
33 command="xfconf-query",
34 arg_formats=dict(
35 channel=cmd_runner_fmt.as_opt_val("--channel"),
36 property=cmd_runner_fmt.as_opt_val("--property"),
37 force_array=cmd_runner_fmt.as_bool("--force-array"),
38 reset=cmd_runner_fmt.as_bool("--reset"),
39 create=cmd_runner_fmt.as_bool("--create"),
40 list_arg=cmd_runner_fmt.as_bool("--list"),
41 values_and_types=_values_fmt,
42 version=cmd_runner_fmt.as_fixed("--version"),
43 ),
44 **kwargs,
45 )
46 return runner
47
48
49 def get_xfconf_version(runner: CmdRunner) -> str:
50 with runner("version") as ctx:
51 rc, out, err = ctx.run()
52 return out.splitlines()[0].split()[1]