Debian Codegraph @

ansible_collections/community/general/docs/docsite/rst/guide_deps.rst

raw
1 ..
2 Copyright (c) Ansible Project
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 .. _ansible_collections.community.general.docsite.guide_deps:
7
8 ``deps`` Guide
9 ==============
10
11
12 Using ``deps``
13 ^^^^^^^^^^^^^^
14
15 The ``ansible_collections.community.general.plugins.module_utils._deps`` module util simplifies
16 the importing of code as described in :ref:`Importing and using shared code <shared_code>`.
17 Please notice that ``deps`` is meant to be used specifically with Ansible modules, and not other types of plugins.
18
19 The same example from the Developer Guide would become:
20
21 .. code-block:: python
22
23 from ansible_collections.community.general.plugins.module_utils import _deps as deps
24
25
26 with deps.declare("foo"):
27 import foo
28
29 Then in ``main()``, just after the argspec (or anywhere in the code, for that matter), do
30
31 .. code-block:: python
32
33 deps.validate(module) # assuming module is a valid AnsibleModule instance
34
35 By default, ``deps`` will rely on ``ansible.module_utils.basic.missing_required_lib`` to generate
36 a message about a failing import. That function accepts parameters ``reason`` and ``url``, and
37 and so does ``deps```:
38
39 .. code-block:: python
40
41 with deps.declare("foo", reason="foo is needed to properly bar", url="https://foo.bar.io"):
42 import foo
43
44 If you would rather write a custom message instead of using ``missing_required_lib`` then do:
45
46 .. code-block:: python
47
48 with deps.declare("foo", msg="Custom msg explaining why foo is needed"):
49 import foo
50
51 ``deps`` allows for multiple dependencies to be declared:
52
53 .. code-block:: python
54
55 with deps.declare("foo"):
56 import foo
57
58 with deps.declare("bar"):
59 import bar
60
61 with deps.declare("doe"):
62 import doe
63
64 By default, ``deps.validate()`` will check on all the declared dependencies, but if so desired,
65 they can be validated selectively by doing:
66
67 .. code-block:: python
68
69 deps.validate(module, "foo") # only validates the "foo" dependency
70
71 deps.validate(module, "doe:bar") # only validates the "doe" and "bar" dependencies
72
73 deps.validate(module, "-doe:bar") # validates all dependencies except "doe" and "bar"
74
75 .. versionadded:: 6.1.0