Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_xdg_mime.py

raw
1 # Copyright (c) 2025, Marcos Alano <[email protected]>
2 # Based on gio_mime module. 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 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
7 # Do not use this from other collections or standalone plugins/modules!
8
9 from __future__ import annotations
10
11 import typing as t
12
13 from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt
14
15 if t.TYPE_CHECKING:
16 from ansible.module_utils.basic import AnsibleModule
17
18
19 def xdg_mime_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
20 return CmdRunner(
21 module,
22 command=["xdg-mime"],
23 arg_formats=dict(
24 default=cmd_runner_fmt.as_fixed("default"),
25 query=cmd_runner_fmt.as_fixed("query"),
26 mime_types=cmd_runner_fmt.as_list(),
27 handler=cmd_runner_fmt.as_list(),
28 version=cmd_runner_fmt.as_fixed("--version"),
29 ),
30 **kwargs,
31 )
32
33
34 def xdg_mime_get(runner: CmdRunner, mime_type) -> str | None:
35 def process(rc, out, err) -> str | None:
36 if not out.strip():
37 return None
38 out = out.splitlines()[0]
39 return out.split()[-1]
40
41 with runner("query default mime_types", output_process=process) as ctx:
42 return ctx.run(mime_types=mime_type)