| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2025, Marcos Alano <[email protected]> |
| 3 |
# Based on gio_mime module. Copyright (c) 2022, Alexei Znamensky <[email protected]> |
| 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 |
# In memory: This code is dedicated to my late grandmother, Maria Marlene. 1936-2025. Rest in peace, grandma. |
| 8 |
# -Marcos Alano- |
| 9 |
|
| 10 |
# TODO: Add support for diff mode |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
module: xdg_mime |
| 16 |
author: |
| 17 |
- "Marcos Alano (@mhalano)" |
| 18 |
short_description: Set default handler for MIME types, for applications using XDG tools |
| 19 |
version_added: 10.7.0 |
| 20 |
description: |
| 21 |
- This module allows configuring the default handler for specific MIME types when you use applications that rely on XDG. |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._attributes |
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: full |
| 27 |
diff_mode: |
| 28 |
support: none |
| 29 |
options: |
| 30 |
mime_types: |
| 31 |
description: |
| 32 |
- One or more MIME types for which a default handler is set. |
| 33 |
type: list |
| 34 |
elements: str |
| 35 |
required: true |
| 36 |
handler: |
| 37 |
description: |
| 38 |
- Sets the default handler for the specified MIME types. |
| 39 |
- The desktop file must be installed in the system. If the desktop file is not installed, the module does not fail, |
| 40 |
but the handler is not set either. |
| 41 |
- You must pass a handler in the form V(*.desktop), otherwise the module fails. |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
notes: |
| 45 |
- This module is a thin wrapper around C(xdg-mime) tool. |
| 46 |
- See man xdg-mime(1) for more details. |
| 47 |
seealso: |
| 48 |
- name: C(xdg-mime) command manual page |
| 49 |
description: Manual page for the command. |
| 50 |
link: https://portland.freedesktop.org/doc/xdg-mime.html |
| 51 |
- name: xdg-utils Documentation |
| 52 |
description: Reference documentation for xdg-utils. |
| 53 |
link: https://www.freedesktop.org/wiki/Software/xdg-utils/ |
| 54 |
""" |
| 55 |
|
| 56 |
EXAMPLES = r""" |
| 57 |
- name: Set Chrome as the default handler for HTTPS |
| 58 |
community.general.xdg_mime: |
| 59 |
mime_types: x-scheme-handler/https |
| 60 |
handler: google-chrome.desktop |
| 61 |
register: result |
| 62 |
|
| 63 |
- name: Set Chrome as the default handler for both HTTP and HTTPS |
| 64 |
community.general.xdg_mime: |
| 65 |
mime_types: |
| 66 |
- x-scheme-handler/http |
| 67 |
- x-scheme-handler/https |
| 68 |
handler: google-chrome.desktop |
| 69 |
register: result |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r""" |
| 73 |
current_handlers: |
| 74 |
description: |
| 75 |
- Currently set handlers for the passed MIME types. |
| 76 |
returned: success |
| 77 |
type: list |
| 78 |
elements: str |
| 79 |
sample: |
| 80 |
- google-chrome.desktop |
| 81 |
- firefox.desktop |
| 82 |
version: |
| 83 |
description: Version of the C(xdg-mime) tool. |
| 84 |
type: str |
| 85 |
returned: always |
| 86 |
sample: "1.2.1" |
| 87 |
""" |
| 88 |
|
| 89 |
from ansible_collections.community.general.plugins.module_utils._module_helper import ModuleHelper |
| 90 |
from ansible_collections.community.general.plugins.module_utils._xdg_mime import xdg_mime_get, xdg_mime_runner |
| 91 |
|
| 92 |
|
| 93 |
class XdgMime(ModuleHelper): |
| 94 |
output_params = ["handler"] |
| 95 |
|
| 96 |
module = dict( |
| 97 |
argument_spec=dict( |
| 98 |
mime_types=dict(type="list", elements="str", required=True), |
| 99 |
handler=dict(type="str", required=True), |
| 100 |
), |
| 101 |
supports_check_mode=True, |
| 102 |
) |
| 103 |
|
| 104 |
def __init_module__(self): |
| 105 |
self.runner = xdg_mime_runner(self.module, check_rc=True) |
| 106 |
|
| 107 |
with self.runner("version") as ctx: |
| 108 |
rc, out, err = ctx.run() |
| 109 |
self.vars.version = out.replace("xdg-mime ", "").strip() |
| 110 |
|
| 111 |
if not self.vars.handler.endswith(".desktop"): |
| 112 |
self.do_raise(msg="Handler must be a .desktop file") |
| 113 |
|
| 114 |
self.vars.current_handlers = [] |
| 115 |
for mime in self.vars.mime_types: |
| 116 |
handler_value = xdg_mime_get(self.runner, mime) |
| 117 |
if not handler_value: |
| 118 |
handler_value = "" |
| 119 |
self.vars.current_handlers.append(handler_value) |
| 120 |
|
| 121 |
def __run__(self): |
| 122 |
check_mode_return = (0, "Module executed in check mode", "") |
| 123 |
|
| 124 |
if any(h != self.vars.handler for h in self.vars.current_handlers): |
| 125 |
self.changed = True |
| 126 |
|
| 127 |
if self.has_changed(): |
| 128 |
with self.runner.context( |
| 129 |
args_order="default handler mime_types", check_mode_skip=True, check_mode_return=check_mode_return |
| 130 |
) as ctx: |
| 131 |
rc, out, err = ctx.run() |
| 132 |
self.vars.stdout = out |
| 133 |
self.vars.stderr = err |
| 134 |
self.vars.set("run_info", ctx.run_info, verbosity=1) |
| 135 |
|
| 136 |
|
| 137 |
def main(): |
| 138 |
XdgMime.execute() |
| 139 |
|
| 140 |
|
| 141 |
if __name__ == "__main__": |
| 142 |
main() |