| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2021, Alexei Znamensky <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: xfconf_info |
| 10 |
author: |
| 11 |
- "Alexei Znamensky (@russoz)" |
| 12 |
short_description: Retrieve XFCE4 configurations |
| 13 |
version_added: 3.5.0 |
| 14 |
description: |
| 15 |
- This module allows retrieving Xfce 4 configurations with the help of C(xfconf-query). |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
- community.general._attributes.info_module |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
version_added: 3.3.0 |
| 22 |
# This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix |
| 23 |
options: |
| 24 |
channel: |
| 25 |
description: |
| 26 |
- A Xfconf preference channel is a top-level tree key, inside of the Xfconf repository that corresponds to the location |
| 27 |
for which all application properties/keys are stored. |
| 28 |
- If not provided, the module lists all available channels. |
| 29 |
type: str |
| 30 |
property: |
| 31 |
description: |
| 32 |
- A Xfce preference key is an element in the Xfconf repository that corresponds to an application preference. |
| 33 |
- If provided, then O(channel) is required. |
| 34 |
- If not provided and a O(channel) is provided, then the module lists all available properties in that O(channel). |
| 35 |
type: str |
| 36 |
notes: |
| 37 |
- See man xfconf-query(1) for more details. |
| 38 |
""" |
| 39 |
|
| 40 |
EXAMPLES = r""" |
| 41 |
- name: Get list of all available channels |
| 42 |
community.general.xfconf_info: {} |
| 43 |
register: result |
| 44 |
|
| 45 |
- name: Get list of all properties in a specific channel |
| 46 |
community.general.xfconf_info: |
| 47 |
channel: xsettings |
| 48 |
register: result |
| 49 |
|
| 50 |
- name: Retrieve the DPI value |
| 51 |
community.general.xfconf_info: |
| 52 |
channel: xsettings |
| 53 |
property: /Xft/DPI |
| 54 |
register: result |
| 55 |
|
| 56 |
- name: Get workspace names (4) |
| 57 |
community.general.xfconf_info: |
| 58 |
channel: xfwm4 |
| 59 |
property: /general/workspace_names |
| 60 |
register: result |
| 61 |
""" |
| 62 |
|
| 63 |
RETURN = r""" |
| 64 |
channels: |
| 65 |
description: |
| 66 |
- List of available channels. |
| 67 |
- Returned when the module receives no parameter at all. |
| 68 |
returned: success |
| 69 |
type: list |
| 70 |
elements: str |
| 71 |
sample: |
| 72 |
- xfce4-desktop |
| 73 |
- displays |
| 74 |
- xsettings |
| 75 |
- xfwm4 |
| 76 |
properties: |
| 77 |
description: |
| 78 |
- List of available properties for a specific channel. |
| 79 |
- Returned by passing only the O(channel) parameter to the module. |
| 80 |
returned: success |
| 81 |
type: list |
| 82 |
elements: str |
| 83 |
sample: |
| 84 |
- /Gdk/WindowScalingFactor |
| 85 |
- /Gtk/ButtonImages |
| 86 |
- /Gtk/CursorThemeSize |
| 87 |
- /Gtk/DecorationLayout |
| 88 |
- /Gtk/FontName |
| 89 |
- /Gtk/MenuImages |
| 90 |
- /Gtk/MonospaceFontName |
| 91 |
- /Net/DoubleClickTime |
| 92 |
- /Net/IconThemeName |
| 93 |
- /Net/ThemeName |
| 94 |
- /Xft/Antialias |
| 95 |
- /Xft/Hinting |
| 96 |
- /Xft/HintStyle |
| 97 |
- /Xft/RGBA |
| 98 |
is_array: |
| 99 |
description: |
| 100 |
- Flag indicating whether the property is an array or not. |
| 101 |
returned: success |
| 102 |
type: bool |
| 103 |
value: |
| 104 |
description: |
| 105 |
- The value of the property. Empty if the property is of array type. |
| 106 |
returned: success |
| 107 |
type: str |
| 108 |
sample: Monospace 10 |
| 109 |
value_array: |
| 110 |
description: |
| 111 |
- The array value of the property. Empty if the property is not of array type. |
| 112 |
returned: success |
| 113 |
type: list |
| 114 |
elements: str |
| 115 |
sample: |
| 116 |
- Main |
| 117 |
- Work |
| 118 |
- Tmp |
| 119 |
version: |
| 120 |
description: |
| 121 |
- The version of the C(xfconf-query) command. |
| 122 |
returned: success |
| 123 |
type: str |
| 124 |
sample: 4.18.1 |
| 125 |
version_added: 10.2.0 |
| 126 |
""" |
| 127 |
|
| 128 |
from ansible_collections.community.general.plugins.module_utils._module_helper import ModuleHelper |
| 129 |
from ansible_collections.community.general.plugins.module_utils._xfconf import get_xfconf_version, xfconf_runner |
| 130 |
|
| 131 |
|
| 132 |
class XFConfInfo(ModuleHelper): |
| 133 |
module = dict( |
| 134 |
argument_spec=dict( |
| 135 |
channel=dict(type="str"), |
| 136 |
property=dict(type="str"), |
| 137 |
), |
| 138 |
required_by=dict(property=["channel"]), |
| 139 |
supports_check_mode=True, |
| 140 |
) |
| 141 |
|
| 142 |
def __init_module__(self): |
| 143 |
self.runner = xfconf_runner(self.module, check_rc=True) |
| 144 |
self.vars.version = get_xfconf_version(self.runner) |
| 145 |
self.vars.set("list_arg", False, output=False) |
| 146 |
self.vars.set("is_array", False) |
| 147 |
|
| 148 |
def process_command_output(self, rc, out, err): |
| 149 |
result = out.rstrip() |
| 150 |
if "Value is an array with" in result: |
| 151 |
result = result.split("\n") |
| 152 |
self.vars.is_array = True |
| 153 |
if len(result) > 1: |
| 154 |
result.pop(0) |
| 155 |
result.pop(0) |
| 156 |
else: |
| 157 |
return [] |
| 158 |
|
| 159 |
return result |
| 160 |
|
| 161 |
def _process_list_properties(self, rc, out, err): |
| 162 |
return out.splitlines() |
| 163 |
|
| 164 |
def _process_list_channels(self, rc, out, err): |
| 165 |
lines = out.splitlines() |
| 166 |
lines.pop(0) |
| 167 |
lines = [s.lstrip() for s in lines] |
| 168 |
return lines |
| 169 |
|
| 170 |
def __run__(self): |
| 171 |
self.vars.list_arg = not (bool(self.vars.channel) and bool(self.vars.property)) |
| 172 |
output = "value" |
| 173 |
proc = self.process_command_output |
| 174 |
if self.vars.channel is None: |
| 175 |
output = "channels" |
| 176 |
proc = self._process_list_channels |
| 177 |
elif self.vars.property is None: |
| 178 |
output = "properties" |
| 179 |
proc = self._process_list_properties |
| 180 |
|
| 181 |
with self.runner.context("list_arg channel property", output_process=proc) as ctx: |
| 182 |
result = ctx.run(**self.vars.as_dict()) |
| 183 |
|
| 184 |
if not self.vars.list_arg and self.vars.is_array: |
| 185 |
output = "value_array" |
| 186 |
self.vars.set(output, result) |
| 187 |
|
| 188 |
|
| 189 |
def main(): |
| 190 |
XFConfInfo.execute() |
| 191 |
|
| 192 |
|
| 193 |
if __name__ == "__main__": |
| 194 |
main() |