mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 06:10:22 -07:00
Attribute defaults and optional accessors.
This commit is contained in:
parent
e66a0096a7
commit
d97b38ba83
4 changed files with 49 additions and 12 deletions
|
@ -23,10 +23,11 @@ class Base(object):
|
|||
|
||||
# each class knows attributes set upon it, see Task.py for example
|
||||
self._attributes = dict()
|
||||
for name in self.__class__.__dict__:
|
||||
|
||||
for (name, value) in self.__class__.__dict__.iteritems():
|
||||
aname = name[1:]
|
||||
if isinstance(aname, Attribute) and not isinstance(aname, FieldAttribute):
|
||||
self._attributes[aname] = None
|
||||
if isinstance(value, Attribute):
|
||||
self._attributes[aname] = value.default
|
||||
|
||||
def munge(self, ds):
|
||||
''' infrequently used method to do some pre-processing of legacy terms '''
|
||||
|
@ -94,9 +95,16 @@ class Base(object):
|
|||
|
||||
def __getattr__(self, needle):
|
||||
|
||||
# return any attribute names as if they were real.
|
||||
# access them like obj.attrname()
|
||||
# return any attribute names as if they were real
|
||||
# optionally allowing masking by accessors
|
||||
|
||||
if not needle.startswith("_"):
|
||||
method = "get_%s" % needle
|
||||
if method in self.__dict__:
|
||||
return method(self)
|
||||
|
||||
if needle in self._attributes:
|
||||
return self._attributes[needle]
|
||||
|
||||
raise AttributeError("attribute not found: %s" % needle)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue