| 1 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import typing as t |
| 21 |
|
| 22 |
from ansible.utils.sentinel import Sentinel |
| 23 |
|
| 24 |
if t.TYPE_CHECKING: |
| 25 |
from ansible.playbook.base import FieldAttributeBase |
| 26 |
|
| 27 |
_CONTAINERS = frozenset(('list', 'dict', 'set')) |
| 28 |
|
| 29 |
|
| 30 |
class Attribute: |
| 31 |
|
| 32 |
def __init__( |
| 33 |
self, |
| 34 |
isa=None, |
| 35 |
private=False, |
| 36 |
default=None, |
| 37 |
required=False, |
| 38 |
listof=None, |
| 39 |
priority=0, |
| 40 |
class_type=None, |
| 41 |
always_post_validate=False, |
| 42 |
alias=None, |
| 43 |
static=False, |
| 44 |
): |
| 45 |
|
| 46 |
""" |
| 47 |
:class:`Attribute` specifies constraints for attributes of objects which |
| 48 |
derive from playbook data. The attributes of the object are basically |
| 49 |
a schema for the yaml playbook. |
| 50 |
|
| 51 |
:kwarg isa: The type of the attribute. Allowable values are a string |
| 52 |
representation of any yaml basic datatype, python class, or percent. |
| 53 |
(Enforced at post-validation time). |
| 54 |
:kwarg private: Not used at runtime. The docs playbook keyword dumper uses it to determine |
| 55 |
that a keyword should not be documented. mpdehaan had plans to remove attributes marked |
| 56 |
private from the ds so they would not have been available at all. |
| 57 |
:kwarg default: Default value if unspecified in the YAML document. |
| 58 |
:kwarg required: Whether or not the YAML document must contain this field. |
| 59 |
If the attribute is None when post-validated, an error will be raised. |
| 60 |
:kwarg listof: If isa is set to "list", this can optionally be set to |
| 61 |
ensure that all elements in the list are of the given type. Valid |
| 62 |
values here are the same as those for isa. |
| 63 |
:kwarg priority: The order in which the fields should be parsed. Generally |
| 64 |
this does not need to be set, it is for rare situations where another |
| 65 |
field depends on the fact that another field was parsed first. |
| 66 |
:kwarg class_type: If isa is set to "class", this can be optionally set to |
| 67 |
a class (not a string name). The YAML data for this field will be |
| 68 |
passed to the __init__ method of that class during post validation and |
| 69 |
the field will be an instance of that class. |
| 70 |
:kwarg always_post_validate: Controls whether a field should be post |
| 71 |
validated or not (default: False). |
| 72 |
:kwarg alias: An alias to use for the attribute name, for situations where |
| 73 |
the attribute name may conflict with a Python reserved word. |
| 74 |
""" |
| 75 |
|
| 76 |
self.isa = isa |
| 77 |
self.private = private |
| 78 |
self.default = default |
| 79 |
self.required = required |
| 80 |
self.listof = listof |
| 81 |
self.priority = priority |
| 82 |
self.class_type = class_type |
| 83 |
self.always_post_validate = always_post_validate |
| 84 |
self.alias = alias |
| 85 |
self.static = static |
| 86 |
|
| 87 |
if default is not None and self.isa in _CONTAINERS and not callable(default): |
| 88 |
raise TypeError('defaults for FieldAttribute may not be mutable, please provide a callable instead') |
| 89 |
|
| 90 |
def __set_name__(self, owner, name): |
| 91 |
self.name = name |
| 92 |
|
| 93 |
def __eq__(self, other): |
| 94 |
return other.priority == self.priority |
| 95 |
|
| 96 |
def __ne__(self, other): |
| 97 |
return other.priority != self.priority |
| 98 |
|
| 99 |
# NB: higher priority numbers sort first |
| 100 |
|
| 101 |
def __lt__(self, other): |
| 102 |
return other.priority < self.priority |
| 103 |
|
| 104 |
def __gt__(self, other): |
| 105 |
return other.priority > self.priority |
| 106 |
|
| 107 |
def __le__(self, other): |
| 108 |
return other.priority <= self.priority |
| 109 |
|
| 110 |
def __ge__(self, other): |
| 111 |
return other.priority >= self.priority |
| 112 |
|
| 113 |
def __get__(self, obj: FieldAttributeBase, obj_type=None): |
| 114 |
method = f'_get_attr_{self.name}' |
| 115 |
if hasattr(obj, method): |
| 116 |
# NOTE this appears to be not used in the codebase, |
| 117 |
# _get_attr_connection has been replaced by ConnectionFieldAttribute. |
| 118 |
# Leaving it here for test_attr_method from |
| 119 |
# test/units/playbook/test_base.py to pass and for backwards compat. |
| 120 |
if getattr(obj, '_squashed', False): |
| 121 |
value = getattr(obj, f'_{self.name}', Sentinel) |
| 122 |
else: |
| 123 |
value = getattr(obj, method)() |
| 124 |
else: |
| 125 |
value = getattr(obj, f'_{self.name}', Sentinel) |
| 126 |
|
| 127 |
if value is Sentinel: |
| 128 |
value = self.default |
| 129 |
if callable(value): |
| 130 |
value = value() |
| 131 |
setattr(obj, f'_{self.name}', value) |
| 132 |
|
| 133 |
return value |
| 134 |
|
| 135 |
def __set__(self, obj: FieldAttributeBase, value): |
| 136 |
setattr(obj, f'_{self.name}', value) |
| 137 |
if self.alias is not None: |
| 138 |
setattr(obj, f'_{self.alias}', value) |
| 139 |
|
| 140 |
# NOTE this appears to be not needed in the codebase, |
| 141 |
# leaving it here for test_attr_int_del from |
| 142 |
# test/units/playbook/test_base.py to pass. |
| 143 |
def __delete__(self, obj): |
| 144 |
delattr(obj, f'_{self.name}') |
| 145 |
|
| 146 |
|
| 147 |
class NonInheritableFieldAttribute(Attribute): |
| 148 |
... |
| 149 |
|
| 150 |
|
| 151 |
class FieldAttribute(Attribute): |
| 152 |
def __init__(self, extend=False, prepend=False, **kwargs): |
| 153 |
super().__init__(**kwargs) |
| 154 |
|
| 155 |
self.extend = extend |
| 156 |
self.prepend = prepend |
| 157 |
|
| 158 |
def __get__(self, obj, obj_type=None): |
| 159 |
if getattr(obj, '_squashed', False) or getattr(obj, '_finalized', False): |
| 160 |
value = getattr(obj, f'_{self.name}', Sentinel) |
| 161 |
else: |
| 162 |
try: |
| 163 |
value = obj._get_parent_attribute(self.name) |
| 164 |
except AttributeError: |
| 165 |
method = f'_get_attr_{self.name}' |
| 166 |
if hasattr(obj, method): |
| 167 |
# NOTE this appears to be not needed in the codebase, |
| 168 |
# _get_attr_connection has been replaced by ConnectionFieldAttribute. |
| 169 |
# Leaving it here for test_attr_method from |
| 170 |
# test/units/playbook/test_base.py to pass and for backwards compat. |
| 171 |
if getattr(obj, '_squashed', False): |
| 172 |
value = getattr(obj, f'_{self.name}', Sentinel) |
| 173 |
else: |
| 174 |
value = getattr(obj, method)() |
| 175 |
else: |
| 176 |
value = getattr(obj, f'_{self.name}', Sentinel) |
| 177 |
|
| 178 |
if value is Sentinel: |
| 179 |
value = self.default |
| 180 |
if callable(value): |
| 181 |
value = value() |
| 182 |
|
| 183 |
return value |