diff --git a/test/v2/playbook/test_task.py b/test/v2/playbook/test_task.py index eef422aee8..124fc1bc98 100644 --- a/test/v2/playbook/test_task.py +++ b/test/v2/playbook/test_task.py @@ -8,6 +8,10 @@ basic_shell_task = dict( shell = 'echo hi' ) +kv_shell_task = dict( + action = 'shell echo hi' +) + class TestTask(unittest.TestCase): def setUp(self): @@ -36,6 +40,17 @@ class TestTask(unittest.TestCase): assert t.args == 'echo hi' 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 def test_can_load_action_complex_form(self): diff --git a/v2/ansible/playbook/attribute.py b/v2/ansible/playbook/attribute.py index 1a22f3dbdb..a10da490c7 100644 --- a/v2/ansible/playbook/attribute.py +++ b/v2/ansible/playbook/attribute.py @@ -18,10 +18,13 @@ #from ansible.common.errors import AnsibleError class Attribute(object): - def __init__(self, isa=None, private=False): + + def __init__(self, isa=None, private=False, default=None): + self.isa = isa self.private = private self.value = None + self.default = default def __call__(self): return self.value diff --git a/v2/ansible/playbook/base.py b/v2/ansible/playbook/base.py index 825e9171ff..6390f3432f 100644 --- a/v2/ansible/playbook/base.py +++ b/v2/ansible/playbook/base.py @@ -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) + diff --git a/v2/ansible/playbook/task.py b/v2/ansible/playbook/task.py index 6ab9231ba1..a3324bbba3 100644 --- a/v2/ansible/playbook/task.py +++ b/v2/ansible/playbook/task.py @@ -87,9 +87,6 @@ class Task(Base): _transport = FieldAttribute(isa='string') _until = FieldAttribute(isa='list') # ? - _role = Attribute() - _block = Attribute() - def __init__(self, block=None, role=None): ''' constructors a task, without the Task.load classmethod, it will be pretty blank ''' self._block = block @@ -99,10 +96,24 @@ class Task(Base): def get_name(self): ''' return the name of the task ''' - if self.role: - return "%s : %s" % (self.role.get_name(), self.name) - else: + if self._role and self.name: + return "%s : %s" % (self._role.name, self.name) + elif 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 def load(data, block=None, role=None):