mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Attribute defaults and optional accessors.
This commit is contained in:
parent
e66a0096a7
commit
d97b38ba83
4 changed files with 49 additions and 12 deletions
|
@ -8,6 +8,10 @@ basic_shell_task = dict(
|
||||||
shell = 'echo hi'
|
shell = 'echo hi'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
kv_shell_task = dict(
|
||||||
|
action = 'shell echo hi'
|
||||||
|
)
|
||||||
|
|
||||||
class TestTask(unittest.TestCase):
|
class TestTask(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -36,6 +40,17 @@ class TestTask(unittest.TestCase):
|
||||||
assert t.args == 'echo hi'
|
assert t.args == 'echo hi'
|
||||||
|
|
||||||
def test_can_load_action_kv_form(self):
|
def test_can_load_action_kv_form(self):
|
||||||
|
t = Task.load(kv_shell_task)
|
||||||
|
assert t.action == 'shell'
|
||||||
|
assert t.args == 'echo hi'
|
||||||
|
|
||||||
|
def test_can_auto_name(self):
|
||||||
|
assert 'name' not in kv_shell_task
|
||||||
|
t = Task.load(kv_shell_task)
|
||||||
|
print "GOT NAME=(%s)" % t.name
|
||||||
|
assert t.name == 'shell echo hi'
|
||||||
|
|
||||||
|
def test_can_auto_name_with_role(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_can_load_action_complex_form(self):
|
def test_can_load_action_complex_form(self):
|
||||||
|
|
|
@ -18,10 +18,13 @@
|
||||||
#from ansible.common.errors import AnsibleError
|
#from ansible.common.errors import AnsibleError
|
||||||
|
|
||||||
class Attribute(object):
|
class Attribute(object):
|
||||||
def __init__(self, isa=None, private=False):
|
|
||||||
|
def __init__(self, isa=None, private=False, default=None):
|
||||||
|
|
||||||
self.isa = isa
|
self.isa = isa
|
||||||
self.private = private
|
self.private = private
|
||||||
self.value = None
|
self.value = None
|
||||||
|
self.default = default
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
|
@ -23,10 +23,11 @@ class Base(object):
|
||||||
|
|
||||||
# each class knows attributes set upon it, see Task.py for example
|
# each class knows attributes set upon it, see Task.py for example
|
||||||
self._attributes = dict()
|
self._attributes = dict()
|
||||||
for name in self.__class__.__dict__:
|
|
||||||
|
for (name, value) in self.__class__.__dict__.iteritems():
|
||||||
aname = name[1:]
|
aname = name[1:]
|
||||||
if isinstance(aname, Attribute) and not isinstance(aname, FieldAttribute):
|
if isinstance(value, Attribute):
|
||||||
self._attributes[aname] = None
|
self._attributes[aname] = value.default
|
||||||
|
|
||||||
def munge(self, ds):
|
def munge(self, ds):
|
||||||
''' infrequently used method to do some pre-processing of legacy terms '''
|
''' infrequently used method to do some pre-processing of legacy terms '''
|
||||||
|
@ -94,9 +95,16 @@ class Base(object):
|
||||||
|
|
||||||
def __getattr__(self, needle):
|
def __getattr__(self, needle):
|
||||||
|
|
||||||
# return any attribute names as if they were real.
|
# return any attribute names as if they were real
|
||||||
# access them like obj.attrname()
|
# 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:
|
if needle in self._attributes:
|
||||||
return self._attributes[needle]
|
return self._attributes[needle]
|
||||||
|
|
||||||
raise AttributeError("attribute not found: %s" % needle)
|
raise AttributeError("attribute not found: %s" % needle)
|
||||||
|
|
||||||
|
|
|
@ -87,9 +87,6 @@ class Task(Base):
|
||||||
_transport = FieldAttribute(isa='string')
|
_transport = FieldAttribute(isa='string')
|
||||||
_until = FieldAttribute(isa='list') # ?
|
_until = FieldAttribute(isa='list') # ?
|
||||||
|
|
||||||
_role = Attribute()
|
|
||||||
_block = Attribute()
|
|
||||||
|
|
||||||
def __init__(self, block=None, role=None):
|
def __init__(self, block=None, role=None):
|
||||||
''' constructors a task, without the Task.load classmethod, it will be pretty blank '''
|
''' constructors a task, without the Task.load classmethod, it will be pretty blank '''
|
||||||
self._block = block
|
self._block = block
|
||||||
|
@ -99,10 +96,24 @@ class Task(Base):
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
''' return the name of the task '''
|
''' return the name of the task '''
|
||||||
|
|
||||||
if self.role:
|
if self._role and self.name:
|
||||||
return "%s : %s" % (self.role.get_name(), self.name)
|
return "%s : %s" % (self._role.name, self.name)
|
||||||
else:
|
elif self.name:
|
||||||
return self.name
|
return self.name
|
||||||
|
else:
|
||||||
|
return "%s %s" % (self.action, self._merge_kv(self.args))
|
||||||
|
|
||||||
|
def _merge_kv(self, ds):
|
||||||
|
if ds is None:
|
||||||
|
return ""
|
||||||
|
elif isinstance(ds, basestring):
|
||||||
|
return ds
|
||||||
|
elif instance(ds, dict):
|
||||||
|
buf = ""
|
||||||
|
for (k,v) in ds.iteritems():
|
||||||
|
buf = buf + "%s=%s " % (k,v)
|
||||||
|
buf = buf.strip()
|
||||||
|
return buf
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load(data, block=None, role=None):
|
def load(data, block=None, role=None):
|
||||||
|
|
Loading…
Reference in a new issue