Debian Codegraph @

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

raw
1 # Copyright (c) 2022, Alexei Znamensky <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import typing as t
11
12 from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt
13
14 if t.TYPE_CHECKING:
15 from ansible.module_utils.basic import AnsibleModule
16
17
18 def gio_mime_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
19 return CmdRunner(
20 module,
21 command=["gio"],
22 arg_formats=dict(
23 mime=cmd_runner_fmt.as_fixed("mime"),
24 mime_type=cmd_runner_fmt.as_list(),
25 handler=cmd_runner_fmt.as_list(),
26 version=cmd_runner_fmt.as_fixed("--version"),
27 ),
28 **kwargs,
29 )
30
31
32 def gio_mime_get(runner: CmdRunner, mime_type) -> str | None:
33 def process(rc, out, err) -> str | None:
34 if err.startswith("No default applications for"):
35 return None
36 out = out.splitlines()[0]
37 return out.split()[-1]
38
39 with runner("mime mime_type", output_process=process) as ctx:
40 return ctx.run(mime_type=mime_type)