summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/modules/xfconf_info.py
blob: 3d56a70cb96d17ff6adc16ece0bd28c7aaa475f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2021, Alexei Znamensky <russoz@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = '''
module: xfconf_info
author:
  - "Alexei Znamensky (@russoz)"
short_description: Retrieve XFCE4 configurations
version_added: 3.5.0
description:
  - This module allows retrieving Xfce 4 configurations with the help of C(xfconf-query).
extends_documentation_fragment:
  - community.general.attributes
  - community.general.attributes.info_module
attributes:
  check_mode:
    version_added: 3.3.0
    # This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix
options:
  channel:
    description:
    - >
      A Xfconf preference channel is a top-level tree key, inside of the
      Xfconf repository that corresponds to the location for which all
      application properties/keys are stored.
    - If not provided, the module will list all available channels.
    type: str
  property:
    description:
    - >
      A Xfce preference key is an element in the Xfconf repository
      that corresponds to an application preference.
    - If provided, then O(channel) is required.
    - If not provided and a O(channel) is provided, then the module will list all available properties in that O(channel).
    type: str
notes:
  - See man xfconf-query(1) for more details.
'''

EXAMPLES = """
- name: Get list of all available channels
  community.general.xfconf_info: {}
  register: result

- name: Get list of all properties in a specific channel
  community.general.xfconf_info:
    channel: xsettings
  register: result

- name: Retrieve the DPI value
  community.general.xfconf_info:
    channel: xsettings
    property: /Xft/DPI
  register: result

- name: Get workspace names (4)
  community.general.xfconf_info:
    channel: xfwm4
    property: /general/workspace_names
  register: result
"""

RETURN = '''
  channels:
    description:
      - List of available channels.
      - Returned when the module receives no parameter at all.
    returned: success
    type: list
    elements: str
    sample:
    - xfce4-desktop
    - displays
    - xsettings
    - xfwm4
  properties:
    description:
      - List of available properties for a specific channel.
      - Returned by passing only the O(channel) parameter to the module.
    returned: success
    type: list
    elements: str
    sample:
      - /Gdk/WindowScalingFactor
      - /Gtk/ButtonImages
      - /Gtk/CursorThemeSize
      - /Gtk/DecorationLayout
      - /Gtk/FontName
      - /Gtk/MenuImages
      - /Gtk/MonospaceFontName
      - /Net/DoubleClickTime
      - /Net/IconThemeName
      - /Net/ThemeName
      - /Xft/Antialias
      - /Xft/Hinting
      - /Xft/HintStyle
      - /Xft/RGBA
  is_array:
    description:
      - Flag indicating whether the property is an array or not.
    returned: success
    type: bool
  value:
    description:
      - The value of the property. Empty if the property is of array type.
    returned: success
    type: str
    sample: Monospace 10
  value_array:
    description:
      - The array value of the property. Empty if the property is not of array type.
    returned: success
    type: list
    elements: str
    sample:
      - Main
      - Work
      - Tmp
'''

from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
from ansible_collections.community.general.plugins.module_utils.xfconf import xfconf_runner


class XFConfInfo(ModuleHelper):
    module = dict(
        argument_spec=dict(
            channel=dict(type='str'),
            property=dict(type='str'),
        ),
        required_by=dict(
            property=['channel']
        ),
        supports_check_mode=True,
    )
    use_old_vardict = False

    def __init_module__(self):
        self.runner = xfconf_runner(self.module, check_rc=True)
        self.vars.set("list_arg", False, output=False)
        self.vars.set("is_array", False)

    def process_command_output(self, rc, out, err):
        result = out.rstrip()
        if "Value is an array with" in result:
            result = result.split("\n")
            result.pop(0)
            result.pop(0)
            self.vars.is_array = True

        return result

    def _process_list_properties(self, rc, out, err):
        return out.splitlines()

    def _process_list_channels(self, rc, out, err):
        lines = out.splitlines()
        lines.pop(0)
        lines = [s.lstrip() for s in lines]
        return lines

    def __run__(self):
        self.vars.list_arg = not (bool(self.vars.channel) and bool(self.vars.property))
        output = 'value'
        proc = self.process_command_output
        if self.vars.channel is None:
            output = 'channels'
            proc = self._process_list_channels
        elif self.vars.property is None:
            output = 'properties'
            proc = self._process_list_properties

        with self.runner.context('list_arg channel property', output_process=proc) as ctx:
            result = ctx.run(**self.vars.as_dict())

        if not self.vars.list_arg and self.vars.is_array:
            output = "value_array"
        self.vars.set(output, result)


def main():
    XFConfInfo.execute()


if __name__ == '__main__':
    main()