-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add xdg_mime module #10007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
felixfontein
merged 53 commits into
ansible-collections:main
from
mhalano:add-xdg-mime-module
Apr 26, 2025
Merged
Add xdg_mime module #10007
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
f656618
Add version of xdg_mime module
mhalano 68f99c0
Fix xdg_mime_get since the command is different
mhalano 2bac2c3
Add query parameter
mhalano 8ad12d8
Fix order of parameters
mhalano 652c959
Add myself to BOTMETA
mhalano dc73fb5
Add unit tests
mhalano 4404de3
Fix the way we deal when there is no handler set
mhalano b112e41
Improve documentation
mhalano c04754b
Remove unused import
mhalano a67b389
Fix documentation
mhalano 28324cb
Strip xdg-mime from version string
mhalano 73ec66b
Fix information about version
mhalano f3167ca
Add error message sample
mhalano 3ca5472
Add test to invalid handler
mhalano 2de85db
Add support to multiple mime-types
mhalano 9fce2db
Change the output parameter from handlers to handler
mhalano cec924c
Change tests related to multiple mime-type support
mhalano 3abf43b
Small fixes
mhalano 3892076
Stop using constant to enable changed state
mhalano 3efd313
Add before_handlers and after_handlers
mhalano 324dbee
Change tests to use before and after structures
mhalano 80f94a9
Add a stronger message about using a non-installed handler
mhalano e3f5063
Manage some edge cases
mhalano 2b1b834
Change error message to match the new value
mhalano 9cd43ce
Add some fixes
mhalano 610a4a3
Change some tests
mhalano b7d8ecc
Update plugins/modules/xdg_mime.py
mhalano bfe6e97
Remove a blank line
mhalano 22e7e8d
Remove single quote
mhalano 53357c4
Add xdg-mime to the version in the mocks
mhalano fc76766
Remove after_handlers and make code simpler
mhalano 4465d07
Update tests to work without after_handlers
mhalano cf72cdd
Remove diff_params and clean output_params
mhalano 47519ed
Make mime_type plural since it supports multiple items
mhalano 10773de
Move the handler check to module init
mhalano 3192ca8
Use anchors in the test to make yaml simpler
mhalano 3c54db7
Update plugins/modules/xdg_mime.py
mhalano 38795d5
Update plugins/modules/xdg_mime.py
mhalano 2485af1
Update plugins/modules/xdg_mime.py
mhalano 7cc3b85
Update plugins/modules/xdg_mime.py
mhalano 1fb3b30
Update plugins/modules/xdg_mime.py
mhalano 4ba456a
Update plugins/modules/xdg_mime.py
mhalano 5301bee
Update plugins/modules/xdg_mime.py
mhalano 0315a4d
Update plugins/modules/xdg_mime.py
mhalano c4266f0
Update plugins/modules/xdg_mime.py
mhalano b5664bc
Update plugins/modules/xdg_mime.py
mhalano 8cf32c0
Add blank line to separe examples
mhalano 20ad429
Update plugins/modules/xdg_mime.py
mhalano 0c78dc7
Add a small homage to my late grandma
mhalano 6c745c1
Update plugins/modules/xdg_mime.py
mhalano 3421953
Fix pep8 problem with the homage
mhalano d9cfe4b
Remove trailing whitespace
mhalano 1371c45
Update plugins/modules/xdg_mime.py
mhalano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (c) 2025, Marcos Alano <marcoshalano@gmail.com> | ||
| # Based on gio_mime module. Copyright (c) 2022, 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 | ||
|
|
||
| from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt | ||
|
|
||
|
|
||
| def xdg_mime_runner(module, **kwargs): | ||
| return CmdRunner( | ||
| module, | ||
| command=['xdg-mime'], | ||
| arg_formats=dict( | ||
| default=cmd_runner_fmt.as_fixed('default'), | ||
| query=cmd_runner_fmt.as_fixed('query'), | ||
| mime_types=cmd_runner_fmt.as_list(), | ||
| handler=cmd_runner_fmt.as_list(), | ||
| version=cmd_runner_fmt.as_fixed('--version'), | ||
| ), | ||
| **kwargs | ||
| ) | ||
|
|
||
|
|
||
| def xdg_mime_get(runner, mime_type): | ||
| def process(rc, out, err): | ||
| if not out.strip(): | ||
| return None | ||
| out = out.splitlines()[0] | ||
| return out.split()[-1] | ||
|
|
||
| with runner("query default mime_types", output_process=process) as ctx: | ||
| return ctx.run(mime_types=mime_type) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (c) 2025, Marcos Alano <marcoshalano@gmail.com> | ||
| # Based on gio_mime module. Copyright (c) 2022, 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 | ||
|
|
||
| # In memory: This code is dedicated to my late grandmother, Maria Marlene. 1936-2025. Rest in peace, grandma. | ||
| # -Marcos Alano- | ||
|
|
||
| # TODO: Add support for diff mode | ||
|
|
||
| from __future__ import absolute_import, division, print_function | ||
| __metaclass__ = type | ||
|
|
||
| DOCUMENTATION = r""" | ||
| module: xdg_mime | ||
| author: | ||
| - "Marcos Alano (@mhalano)" | ||
| short_description: Set default handler for MIME types, for applications using XDG tools | ||
| version_added: 10.7.0 | ||
| description: | ||
| - This module allows configuring the default handler for specific MIME types when you use applications that rely on XDG. | ||
| extends_documentation_fragment: | ||
| - community.general.attributes | ||
| attributes: | ||
| check_mode: | ||
| support: full | ||
| diff_mode: | ||
| support: none | ||
| options: | ||
| mime_types: | ||
| description: | ||
| - One or more MIME types for which a default handler will be set. | ||
| type: list | ||
| elements: str | ||
| required: true | ||
| handler: | ||
| description: | ||
| - Sets the default handler for the specified MIME types. | ||
| - The desktop file must be installed in the system. | ||
| If the desktop file is not installed, the module | ||
| does not fail, but the handler is not set either. | ||
| - You must pass a handler in the form V(*.desktop), otherwise the module fails. | ||
| type: str | ||
mhalano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| required: true | ||
| notes: | ||
| - This module is a thin wrapper around C(xdg-mime) tool. | ||
| - See man xdg-mime(1) for more details. | ||
| seealso: | ||
| - name: C(xdg-mime) command manual page | ||
| description: Manual page for the command. | ||
| link: https://portland.freedesktop.org/doc/xdg-mime.html | ||
| - name: xdg-utils Documentation | ||
| description: Reference documentation for xdg-utils. | ||
| link: https://www.freedesktop.org/wiki/Software/xdg-utils/ | ||
| """ | ||
|
|
||
| EXAMPLES = r""" | ||
| - name: Set Chrome as the default handler for HTTPS | ||
| community.general.xdg_mime: | ||
| mime_types: x-scheme-handler/https | ||
| handler: google-chrome.desktop | ||
| register: result | ||
|
|
||
| - name: Set Chrome as the default handler for both HTTP and HTTPS | ||
mhalano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| community.general.xdg_mime: | ||
| mime_types: | ||
| - x-scheme-handler/http | ||
| - x-scheme-handler/https | ||
| handler: google-chrome.desktop | ||
| register: result | ||
| """ | ||
|
|
||
| RETURN = r""" | ||
| current_handlers: | ||
| description: | ||
| - Currently set handlers for the passed MIME types. | ||
| returned: success | ||
| type: list | ||
| elements: str | ||
| sample: | ||
| - google-chrome.desktop | ||
| - firefox.desktop | ||
| version: | ||
| description: Version of the C(xdg-mime) tool. | ||
| type: str | ||
| returned: always | ||
| sample: "1.2.1" | ||
| """ | ||
|
|
||
| from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper | ||
| from ansible_collections.community.general.plugins.module_utils.xdg_mime import xdg_mime_runner, xdg_mime_get | ||
|
|
||
|
|
||
| class XdgMime(ModuleHelper): | ||
| output_params = ['handler'] | ||
|
|
||
| module = dict( | ||
| argument_spec=dict( | ||
| mime_types=dict(type='list', elements='str', required=True), | ||
| handler=dict(type='str', required=True), | ||
| ), | ||
| supports_check_mode=True, | ||
| ) | ||
| use_old_vardict = False | ||
|
|
||
| def __init_module__(self): | ||
| self.runner = xdg_mime_runner(self.module, check_rc=True) | ||
|
|
||
| with self.runner("version") as ctx: | ||
| rc, out, err = ctx.run() | ||
| self.vars.version = out.replace("xdg-mime ", "").strip() | ||
|
|
||
| if not self.vars.handler.endswith(".desktop"): | ||
| self.do_raise(msg="Handler must be a .desktop file") | ||
|
|
||
| self.vars.current_handlers = [] | ||
| for mime in self.vars.mime_types: | ||
| handler_value = xdg_mime_get(self.runner, mime) | ||
| if not handler_value: | ||
| handler_value = '' | ||
| self.vars.current_handlers.append(handler_value) | ||
|
|
||
| def __run__(self): | ||
| check_mode_return = (0, 'Module executed in check mode', '') | ||
|
|
||
| if any(h != self.vars.handler for h in self.vars.current_handlers): | ||
| self.changed = True | ||
|
|
||
| if self.has_changed(): | ||
| with self.runner.context(args_order="default handler mime_types", check_mode_skip=True, check_mode_return=check_mode_return) as ctx: | ||
| rc, out, err = ctx.run() | ||
| self.vars.stdout = out | ||
| self.vars.stderr = err | ||
| self.vars.set("run_info", ctx.run_info, verbosity=1) | ||
|
|
||
|
|
||
mhalano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def main(): | ||
| XdgMime.execute() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (c) 2025, Marcos Alano <marcoshalano@gmail.com> | ||
| # Based on gio_mime module. Copyright (c) 2022, 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 | ||
|
|
||
|
|
||
| from ansible_collections.community.general.plugins.modules import xdg_mime | ||
| from .uthelper import UTHelper, RunCommandMock | ||
|
|
||
|
|
||
| UTHelper.from_module(xdg_mime, __name__, mocks=[RunCommandMock]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (c) 2025, Marcos Alano <marcoshalano@gmail.com> | ||
| # Based on gio_mime module. Copyright (c) 2022, 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 | ||
|
|
||
| # TODO: add tests for setting multiple mime types at once | ||
| --- | ||
| anchors: | ||
| environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} | ||
| input: &input | ||
| mime_types: x-scheme-handler/http | ||
| handler: google-chrome.desktop | ||
| get_version: &get_version | ||
| command: [/testbin/xdg-mime, --version] | ||
| environ: *env-def | ||
| rc: 0 | ||
| out: "xdg-mime 1.2.1\n" | ||
| err: '' | ||
| query_mime_type: &query_mime_type | ||
| command: [/testbin/xdg-mime, query, default, x-scheme-handler/http] | ||
| environ: *env-def | ||
| rc: 0 | ||
| out: '' | ||
| err: '' | ||
| set_handler: &set_handler | ||
| command: [/testbin/xdg-mime, default, google-chrome.desktop, x-scheme-handler/http] | ||
| environ: *env-def | ||
| rc: 0 | ||
| out: '' | ||
| err: '' | ||
| test_cases: | ||
| - id: test_set_handler | ||
| input: *input | ||
| output: | ||
| current_handlers: [''] | ||
| changed: true | ||
| mocks: | ||
| run_command: | ||
| - *get_version | ||
| - *query_mime_type | ||
| - *set_handler | ||
| - id: test_set_handler_check | ||
| input: *input | ||
| output: | ||
| current_handlers: ['google-chrome.desktop'] | ||
| changed: false | ||
| flags: | ||
| check: true | ||
| mocks: | ||
| run_command: | ||
| - *get_version | ||
| - <<: *query_mime_type | ||
| out: | | ||
| google-chrome.desktop | ||
mhalano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - id: test_set_handler_idempot | ||
| input: *input | ||
| output: | ||
| current_handlers: ['google-chrome.desktop'] | ||
| changed: false | ||
| mocks: | ||
| run_command: | ||
| - *get_version | ||
| - <<: *query_mime_type | ||
| out: | | ||
| google-chrome.desktop | ||
| - id: test_set_handler_idempot_check | ||
| input: *input | ||
| output: | ||
| current_handlers: ['google-chrome.desktop'] | ||
mhalano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| changed: false | ||
| flags: | ||
| check: true | ||
| mocks: | ||
| run_command: | ||
| - *get_version | ||
| - <<: *query_mime_type | ||
| out: | | ||
| google-chrome.desktop | ||
| - id: test_set_invalid_handler | ||
| input: | ||
| <<: *input | ||
| handler: google-chrome.desktopX | ||
| output: | ||
| failed: true | ||
| msg: Handler must be a .desktop file | ||
| mocks: | ||
| run_command: | ||
| - *get_version | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.