| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2017, Joseph Benden <[email protected]> |
| 3 |
# |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: xfconf |
| 11 |
author: |
| 12 |
- "Joseph Benden (@jbenden)" |
| 13 |
- "Alexei Znamensky (@russoz)" |
| 14 |
short_description: Edit XFCE4 Configurations |
| 15 |
description: |
| 16 |
- This module allows for the manipulation of Xfce 4 Configuration with the help of C(xfconf-query). |
| 17 |
seealso: |
| 18 |
- name: xfconf-query(1) man page |
| 19 |
description: Manual page of the C(xfconf-query) tool at the XFCE documentation site. |
| 20 |
link: 'https://docs.xfce.org/xfce/xfconf/xfconf-query' |
| 21 |
|
| 22 |
- name: xfconf - Configuration Storage System |
| 23 |
description: XFCE documentation for the Xfconf configuration system. |
| 24 |
link: 'https://docs.xfce.org/xfce/xfconf/start' |
| 25 |
|
| 26 |
- name: xfce4-settings-editor - Settings Editor |
| 27 |
description: XFCE documentation for the graphical editor for configuration settings. |
| 28 |
link: https://docs.xfce.org/xfce/xfce4-settings/editor#change_properties |
| 29 |
|
| 30 |
extends_documentation_fragment: |
| 31 |
- community.general._attributes |
| 32 |
|
| 33 |
attributes: |
| 34 |
check_mode: |
| 35 |
support: full |
| 36 |
diff_mode: |
| 37 |
support: full |
| 38 |
|
| 39 |
options: |
| 40 |
channel: |
| 41 |
description: |
| 42 |
- A Xfconf preference channel is a top-level tree key, inside of the Xfconf repository that corresponds to the location |
| 43 |
for which all application properties/keys are stored. See man xfconf-query(1). |
| 44 |
required: true |
| 45 |
type: str |
| 46 |
property: |
| 47 |
description: |
| 48 |
- A Xfce preference key is an element in the Xfconf repository that corresponds to an application preference. See man |
| 49 |
xfconf-query(1). |
| 50 |
required: true |
| 51 |
type: str |
| 52 |
value: |
| 53 |
description: |
| 54 |
- Preference properties typically have simple values such as strings, integers, or lists of strings and integers. See |
| 55 |
man xfconf-query(1). |
| 56 |
type: list |
| 57 |
elements: raw |
| 58 |
value_type: |
| 59 |
description: |
| 60 |
- The type of value being set. |
| 61 |
- When providing more than one O(value_type), the length of the list must be equal to the length of O(value). |
| 62 |
- If only one O(value_type) is provided, but O(value) contains more than on element, that O(value_type) is applied to |
| 63 |
all elements of O(value). |
| 64 |
- If the O(property) being set is an array and it can possibly have only one element in the array, then O(force_array=true) |
| 65 |
must be used to ensure that C(xfconf-query) interprets the value as an array rather than a scalar. |
| 66 |
- Support for V(uchar), V(char), V(uint64), and V(int64) has been added in community.general 4.8.0. |
| 67 |
type: list |
| 68 |
elements: str |
| 69 |
choices: [string, int, double, bool, uint, uchar, char, uint64, int64, float] |
| 70 |
state: |
| 71 |
type: str |
| 72 |
description: |
| 73 |
- The action to take upon the property/value. |
| 74 |
- The state V(get) has been removed in community.general 5.0.0. Please use the module M(community.general.xfconf_info) |
| 75 |
instead. |
| 76 |
- Xfce4 may, and usually does, have default values that come with the system packages. |
| 77 |
You can set new values for these default properties and override the their values. |
| 78 |
However, whey you use O(state=absent), the module executes the command C(xfconf-query reset) for the specified property and |
| 79 |
that only removes user-configured properties, so those properties are B(not removed), but rather they go back to the default values. |
| 80 |
choices: [present, absent] |
| 81 |
default: "present" |
| 82 |
force_array: |
| 83 |
description: |
| 84 |
- Force array even if only one element. |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
aliases: ['array'] |
| 88 |
version_added: 1.0.0 |
| 89 |
""" |
| 90 |
|
| 91 |
EXAMPLES = r""" |
| 92 |
- name: Change the DPI to "192" |
| 93 |
xfconf: |
| 94 |
channel: "xsettings" |
| 95 |
property: "/Xft/DPI" |
| 96 |
value_type: "int" |
| 97 |
value: "192" |
| 98 |
|
| 99 |
- name: Set workspace names (4) |
| 100 |
xfconf: |
| 101 |
channel: xfwm4 |
| 102 |
property: /general/workspace_names |
| 103 |
value_type: string |
| 104 |
value: ['Main', 'Work1', 'Work2', 'Tmp'] |
| 105 |
|
| 106 |
- name: Set workspace names (1) |
| 107 |
xfconf: |
| 108 |
channel: xfwm4 |
| 109 |
property: /general/workspace_names |
| 110 |
value_type: string |
| 111 |
value: ['Main'] |
| 112 |
force_array: true |
| 113 |
""" |
| 114 |
|
| 115 |
RETURN = r""" |
| 116 |
channel: |
| 117 |
description: The channel specified in the module parameters. |
| 118 |
returned: success |
| 119 |
type: str |
| 120 |
sample: "xsettings" |
| 121 |
property: |
| 122 |
description: The property specified in the module parameters. |
| 123 |
returned: success |
| 124 |
type: str |
| 125 |
sample: "/Xft/DPI" |
| 126 |
value_type: |
| 127 |
description: |
| 128 |
- The type of the value that was changed (V(none) for O(state=reset)). Either a single string value or a list of strings |
| 129 |
for array types. |
| 130 |
- This is a string or a list of strings. |
| 131 |
returned: success |
| 132 |
type: any |
| 133 |
sample: '"int" or ["str", "str", "str"]' |
| 134 |
value: |
| 135 |
description: |
| 136 |
- The value of the preference key after executing the module. Either a single string value or a list of strings for array |
| 137 |
types. |
| 138 |
- This is a string or a list of strings. |
| 139 |
returned: success |
| 140 |
type: any |
| 141 |
sample: "'192' or ['orange', 'yellow', 'violet']" |
| 142 |
previous_value: |
| 143 |
description: |
| 144 |
- The value of the preference key before executing the module. Either a single string value or a list of strings for array |
| 145 |
types. |
| 146 |
- This is a string or a list of strings. |
| 147 |
returned: success |
| 148 |
type: any |
| 149 |
sample: '"96" or ["red", "blue", "green"]' |
| 150 |
cmd: |
| 151 |
description: |
| 152 |
- A list with the resulting C(xfconf-query) command executed by the module. |
| 153 |
returned: success |
| 154 |
type: list |
| 155 |
elements: str |
| 156 |
version_added: 5.4.0 |
| 157 |
sample: |
| 158 |
- /usr/bin/xfconf-query |
| 159 |
- --channel |
| 160 |
- xfce4-panel |
| 161 |
- --property |
| 162 |
- /plugins/plugin-19/timezone |
| 163 |
- --create |
| 164 |
- --type |
| 165 |
- string |
| 166 |
- --set |
| 167 |
- Pacific/Auckland |
| 168 |
version: |
| 169 |
description: |
| 170 |
- The version of the C(xfconf-query) command. |
| 171 |
returned: success |
| 172 |
type: str |
| 173 |
sample: 4.18.1 |
| 174 |
version_added: 10.2.0 |
| 175 |
""" |
| 176 |
|
| 177 |
from ansible.module_utils.parsing.convert_bool import boolean |
| 178 |
|
| 179 |
from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper |
| 180 |
from ansible_collections.community.general.plugins.module_utils._xfconf import get_xfconf_version, xfconf_runner |
| 181 |
|
| 182 |
|
| 183 |
class XFConfProperty(StateModuleHelper): |
| 184 |
change_params = ("value",) |
| 185 |
diff_params = ("value",) |
| 186 |
output_params = ("property", "channel", "value") |
| 187 |
module = dict( |
| 188 |
argument_spec=dict( |
| 189 |
state=dict(type="str", choices=("present", "absent"), default="present"), |
| 190 |
channel=dict(type="str", required=True), |
| 191 |
property=dict(type="str", required=True), |
| 192 |
value_type=dict( |
| 193 |
type="list", |
| 194 |
elements="str", |
| 195 |
choices=("string", "int", "double", "bool", "uint", "uchar", "char", "uint64", "int64", "float"), |
| 196 |
), |
| 197 |
value=dict(type="list", elements="raw"), |
| 198 |
force_array=dict(type="bool", default=False, aliases=["array"]), |
| 199 |
), |
| 200 |
required_if=[("state", "present", ["value", "value_type"])], |
| 201 |
required_together=[("value", "value_type")], |
| 202 |
supports_check_mode=True, |
| 203 |
) |
| 204 |
|
| 205 |
def __init_module__(self): |
| 206 |
self.runner = xfconf_runner(self.module) |
| 207 |
self.vars.version = get_xfconf_version(self.runner) |
| 208 |
self.does_not = f'Property "{self.vars.property}" does not exist on channel "{self.vars.channel}".' |
| 209 |
self.vars.set("previous_value", self._get()) |
| 210 |
self.vars.set("type", self.vars.value_type) |
| 211 |
self.vars.set_meta("value", initial_value=self.vars.previous_value) |
| 212 |
|
| 213 |
def process_command_output(self, rc, out, err): |
| 214 |
if err.rstrip() == self.does_not: |
| 215 |
return None |
| 216 |
if rc or len(err): |
| 217 |
self.do_raise(f"xfconf-query failed with error (rc={rc}): {err}") |
| 218 |
|
| 219 |
result = out.rstrip() |
| 220 |
if "Value is an array with" in result: |
| 221 |
result = result.split("\n") |
| 222 |
if len(result) > 1: |
| 223 |
result.pop(0) |
| 224 |
result.pop(0) |
| 225 |
else: |
| 226 |
return [] |
| 227 |
|
| 228 |
return result |
| 229 |
|
| 230 |
def _get(self): |
| 231 |
with self.runner("channel property", output_process=self.process_command_output) as ctx: |
| 232 |
return ctx.run() |
| 233 |
|
| 234 |
def state_absent(self): |
| 235 |
with self.runner("channel property reset", check_mode_skip=True) as ctx: |
| 236 |
ctx.run(reset=True) |
| 237 |
self.vars.stdout = ctx.results_out |
| 238 |
self.vars.stderr = ctx.results_err |
| 239 |
self.vars.cmd = ctx.cmd |
| 240 |
self.vars.set("run_info", ctx.run_info, verbosity=4) |
| 241 |
self.vars.value = None |
| 242 |
|
| 243 |
def state_present(self): |
| 244 |
value_type = self.vars.value_type |
| 245 |
|
| 246 |
values_len = len(self.vars.value) |
| 247 |
types_len = len(value_type) |
| 248 |
|
| 249 |
if types_len == 1: |
| 250 |
# use one single type for the entire list |
| 251 |
value_type = value_type * values_len |
| 252 |
elif types_len != values_len: |
| 253 |
# or complain if lists' lengths are different |
| 254 |
self.do_raise('Number of elements in "value" and "value_type" must be the same') |
| 255 |
|
| 256 |
# stringify all values - in the CLI they will all be happy strings anyway |
| 257 |
# and by doing this here the rest of the code can be agnostic to it |
| 258 |
# bool values are normalized to 'true'/'false' to match xfconf-query output format |
| 259 |
self.vars.value = [ |
| 260 |
("true" if boolean(v) else "false") if vt == "bool" else str(v) |
| 261 |
for v, vt in zip(self.vars.value, value_type) |
| 262 |
] |
| 263 |
|
| 264 |
# calculates if it is an array |
| 265 |
self.vars.is_array = bool(self.vars.force_array) or isinstance(self.vars.previous_value, list) or values_len > 1 |
| 266 |
|
| 267 |
with self.runner("channel property create force_array values_and_types", check_mode_skip=True) as ctx: |
| 268 |
ctx.run(create=True, force_array=self.vars.is_array, values_and_types=(self.vars.value, value_type)) |
| 269 |
self.vars.stdout = ctx.results_out |
| 270 |
self.vars.stderr = ctx.results_err |
| 271 |
self.vars.cmd = ctx.cmd |
| 272 |
self.vars.set("run_info", ctx.run_info, verbosity=4) |
| 273 |
|
| 274 |
if not self.vars.is_array: |
| 275 |
self.vars.value = self.vars.value[0] |
| 276 |
self.vars.type = value_type[0] |
| 277 |
else: |
| 278 |
self.vars.type = value_type |
| 279 |
|
| 280 |
|
| 281 |
def main(): |
| 282 |
XFConfProperty.execute() |
| 283 |
|
| 284 |
|
| 285 |
if __name__ == "__main__": |
| 286 |
main() |