Debian Codegraph @

ansible_collections/community/general/plugins/modules/gio_mime.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2022, 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: gio_mime
10 author:
11 - "Alexei Znamensky (@russoz)"
12 short_description: Set default handler for MIME type, for applications using Gnome GIO
13 version_added: 7.5.0
14 description:
15 - This module allows configuring the default handler for a specific MIME type, to be used by applications built with the
16 Gnome GIO API.
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: full
22 diff_mode:
23 support: full
24 options:
25 mime_type:
26 description:
27 - MIME type for which a default handler is set.
28 type: str
29 required: true
30 handler:
31 description:
32 - Default handler set for the MIME type.
33 type: str
34 required: true
35 notes:
36 - This module is a thin wrapper around the C(gio mime) command (and subcommand).
37 - See man gio(1) for more details.
38 seealso:
39 - name: C(gio) command manual page
40 description: Manual page for the command.
41 link: https://man.archlinux.org/man/gio.1
42 - name: GIO Documentation
43 description: Reference documentation for the GIO API..
44 link: https://docs.gtk.org/gio/
45 """
46
47 EXAMPLES = r"""
48 - name: Set chrome as the default handler for https
49 community.general.gio_mime:
50 mime_type: x-scheme-handler/https
51 handler: google-chrome.desktop
52 register: result
53 """
54
55 RETURN = r"""
56 handler:
57 description:
58 - The handler set as default.
59 returned: success
60 type: str
61 sample: google-chrome.desktop
62 version:
63 description: Version of gio.
64 type: str
65 returned: always
66 sample: "2.80.0"
67 version_added: 10.0.0
68 """
69
70 from ansible_collections.community.general.plugins.module_utils._gio_mime import gio_mime_get, gio_mime_runner
71 from ansible_collections.community.general.plugins.module_utils._module_helper import ModuleHelper
72
73
74 class GioMime(ModuleHelper):
75 output_params = ["handler"]
76 module = dict(
77 argument_spec=dict(
78 mime_type=dict(type="str", required=True),
79 handler=dict(type="str", required=True),
80 ),
81 supports_check_mode=True,
82 )
83
84 def __init_module__(self):
85 self.runner = gio_mime_runner(self.module, check_rc=True)
86 with self.runner("version") as ctx:
87 rc, out, err = ctx.run()
88 self.vars.version = out.strip()
89 self.vars.set_meta(
90 "handler", initial_value=gio_mime_get(self.runner, self.vars.mime_type), diff=True, change=True
91 )
92
93 def __run__(self):
94 check_mode_return = (0, "Module executed in check mode", "")
95 if self.vars.has_changed:
96 with self.runner.context(
97 args_order="mime mime_type handler", check_mode_skip=True, check_mode_return=check_mode_return
98 ) as ctx:
99 rc, out, err = ctx.run()
100 self.vars.stdout = out
101 self.vars.stderr = err
102 self.vars.set("run_info", ctx.run_info, verbosity=4)
103
104
105 def main():
106 GioMime.execute()
107
108
109 if __name__ == "__main__":
110 main()