Debian Codegraph @

lib/ansible/utils/collection_loader/_collection_config.py

raw
1 # (c) 2019 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 # CAUTION: This implementation of the collection loader is used by ansible-test.
5 # Because of this, it must be compatible with all Python versions supported on the controller or remote.
6
7 from __future__ import annotations
8
9 from . import _to_text
10
11
12 class _EventSource:
13 def __init__(self):
14 self._handlers = set()
15
16 def __iadd__(self, handler):
17 if not callable(handler):
18 raise ValueError('handler must be callable')
19 self._handlers.add(handler)
20 return self
21
22 def __isub__(self, handler):
23 try:
24 self._handlers.remove(handler)
25 except KeyError:
26 pass
27
28 return self
29
30 def _on_exception(self, handler, exc, *args, **kwargs):
31 # if we return True, we want the caller to re-raise
32 return True
33
34 def fire(self, *args, **kwargs):
35 for h in self._handlers:
36 try:
37 h(*args, **kwargs)
38 except Exception as ex:
39 if self._on_exception(h, ex, *args, **kwargs):
40 raise
41
42
43 class _AnsibleCollectionConfig(type):
44 def __init__(cls, meta, name, bases):
45 cls._collection_finder = None
46 cls._default_collection = None
47 cls._on_collection_load = _EventSource()
48
49 @property
50 def collection_finder(cls):
51 return cls._collection_finder
52
53 @collection_finder.setter
54 def collection_finder(cls, value):
55 if cls._collection_finder:
56 raise ValueError('an AnsibleCollectionFinder has already been configured')
57
58 cls._collection_finder = value
59
60 @property
61 def collection_paths(cls):
62 cls._require_finder()
63 return [_to_text(p) for p in cls._collection_finder._n_collection_paths]
64
65 @property
66 def _internal_collections(cls):
67 cls._require_finder()
68 return cls._collection_finder._internal_collections
69
70 @property
71 def default_collection(cls):
72 return cls._default_collection
73
74 @default_collection.setter
75 def default_collection(cls, value):
76
77 cls._default_collection = value
78
79 @property
80 def on_collection_load(cls):
81 return cls._on_collection_load
82
83 @on_collection_load.setter
84 def on_collection_load(cls, value):
85 if value is not cls._on_collection_load:
86 raise ValueError('on_collection_load is not directly settable (use +=)')
87
88 @property
89 def playbook_paths(cls):
90 cls._require_finder()
91 return [_to_text(p) for p in cls._collection_finder._n_playbook_paths]
92
93 @playbook_paths.setter
94 def playbook_paths(cls, value):
95 cls._require_finder()
96 cls._collection_finder.set_playbook_paths(value)
97
98 def _require_finder(cls):
99 if not cls._collection_finder:
100 raise NotImplementedError('an AnsibleCollectionFinder has not been installed in this process')
101
102
103 # concrete class of our metaclass type that defines the class properties we want
104
105 class AnsibleCollectionConfig(object, metaclass=_AnsibleCollectionConfig):
106 pass