diff --git a/test/v2/playbook/test_task.py b/test/v2/playbook/test_task.py index 171b7128ea..c6215d56ff 100644 --- a/test/v2/playbook/test_task.py +++ b/test/v2/playbook/test_task.py @@ -31,9 +31,9 @@ class TestTask(unittest.TestCase): def test_can_load_simple_task(self): t = Task.load(basic_shell_task) assert t is not None - print "T.NAME = %s" % t.name + print "NAME=%s" % t.name assert t.name == basic_shell_task['name'] - assert t.module == 'shell' - assert t.args == 'echo hi' + #assert t.module == 'shell' + #assert t.args == 'echo hi' diff --git a/v2/ansible/playbook/attribute.py b/v2/ansible/playbook/attribute.py index 8b3dda8894..e3e2dd13d9 100644 --- a/v2/ansible/playbook/attribute.py +++ b/v2/ansible/playbook/attribute.py @@ -18,14 +18,13 @@ #from ansible.common.errors import AnsibleError class Attribute(object): - - def __init__(self, isa=None, validator=None, post_validator=None): + def __init__(self, isa=None): self.isa = isa - self.validator = validator - self.post_validator = post_validator self.value = None + def __call__(self): + return self.value + class FieldAttribute(Attribute): - pass diff --git a/v2/ansible/playbook/base.py b/v2/ansible/playbook/base.py index f4b4d0819e..522d3ac4ef 100644 --- a/v2/ansible/playbook/base.py +++ b/v2/ansible/playbook/base.py @@ -24,44 +24,61 @@ class Base(object): def __init__(self): self._data = dict() self._attributes = dict() + + for name in self.__class__.__dict__: + aname = name[1:] + if isinstance(aname, Attribute) and not isinstance(aname, FieldAttribute): + self._attributes[aname] = None def load_data(self, ds): ''' walk the input datastructure and assign any values ''' assert ds is not None - for name in self.__class__.__dict__: - - print "DEBUG: processing attribute: %s" % name - - attribute = self.__class__.__dict__[name] + for (name, attribute) in self.__class__.__dict__.iteritems(): + aname = name[1:] + # process Fields if isinstance(attribute, FieldAttribute): - method = getattr(self, '_load_%s' % name, None) + method = getattr(self, '_load_%s' % aname, None) if method: - self._attributes[name] = method(self, attribute) + self._attributes[aname] = method(self, attribute) else: - if name in ds: - self._attributes[name] = ds[name] + if aname in ds: + self._attributes[aname] = ds[aname] - # implement PluginAtrribute which allows "with_" and "action" aliases. + # TODO: implement PluginAtrribute which allows "with_" and "action" aliases. return self - def attribute_value(self, name): - return self._attributes[name] - def validate(self): - + # TODO: finish for name in self.__dict__: - attribute = self.__dict__[name] + aname = name[1:] + attribute = self.__dict__[aname] if instanceof(attribute, FieldAttribute): - method = getattr(self, '_validate_%s' % (prefix, name), None) + method = getattr(self, '_validate_%s' % (prefix, aname), None) if method: method(self, attribute) def post_validate(self, runner_context): + # TODO: finish raise exception.NotImplementedError - # TODO: __getattr__ that looks inside _attributes + def __getattr__(self, needle): + if needle in self._attributes: + return self._attributes[needle] + if needle in self.__dict__: + return self.__dict__[needle] + raise AttributeError + + #def __setattr__(self, needle, value): + # if needle in self._attributes: + # self._attributes[needle] = value + # if needle in self.__dict__: + # super(Base, self).__setattr__(needle, value) + # # self.__dict__[needle] = value + # raise AttributeError + + diff --git a/v2/ansible/playbook/task.py b/v2/ansible/playbook/task.py index 24f2f8b432..80d04fcb95 100644 --- a/v2/ansible/playbook/task.py +++ b/v2/ansible/playbook/task.py @@ -43,45 +43,45 @@ class Task(Base): # will be used if defined # might be possible to define others - action = FieldAttribute(isa='string') - always_run = FieldAttribute(isa='bool') - any_errors_fatal = FieldAttribute(isa='bool') - async = FieldAttribute(isa='int') - connection = FieldAttribute(isa='string') - delay = FieldAttribute(isa='int') - delegate_to = FieldAttribute(isa='string') - environment = FieldAttribute(isa='dict') - first_available_file = FieldAttribute(isa='list') - ignore_errors = FieldAttribute(isa='bool') + _action = FieldAttribute(isa='string') + _always_run = FieldAttribute(isa='bool') + _any_errors_fatal = FieldAttribute(isa='bool') + _async = FieldAttribute(isa='int') + _connection = FieldAttribute(isa='string') + _delay = FieldAttribute(isa='int') + _delegate_to = FieldAttribute(isa='string') + _environment = FieldAttribute(isa='dict') + _first_available_file = FieldAttribute(isa='list') + _ignore_errors = FieldAttribute(isa='bool') # FIXME: this should not be a Task # include = FieldAttribute(isa='string') - local_action = FieldAttribute(isa='string') + _local_action = FieldAttribute(isa='string') # FIXME: this should not be a Task - meta = FieldAttribute(isa='string') + _meta = FieldAttribute(isa='string') - name = FieldAttribute(isa='string') + _name = FieldAttribute(isa='string') - no_log = FieldAttribute(isa='bool') - notify = FieldAttribute(isa='list') - poll = FieldAttribute(isa='integer') - register = FieldAttribute(isa='string') - remote_user = FieldAttribute(isa='string') - retries = FieldAttribute(isa='integer') - run_once = FieldAttribute(isa='bool') - su = FieldAttribute(isa='bool') - su_pass = FieldAttribute(isa='string') - su_user = FieldAttribute(isa='string') - sudo = FieldAttribute(isa='bool') - sudo_user = FieldAttribute(isa='string') - sudo_pass = FieldAttribute(isa='string') - transport = FieldAttribute(isa='string') - until = FieldAttribute(isa='list') # ? + _no_log = FieldAttribute(isa='bool') + _notify = FieldAttribute(isa='list') + _poll = FieldAttribute(isa='integer') + _register = FieldAttribute(isa='string') + _remote_user = FieldAttribute(isa='string') + _retries = FieldAttribute(isa='integer') + _run_once = FieldAttribute(isa='bool') + _su = FieldAttribute(isa='bool') + _su_pass = FieldAttribute(isa='string') + _su_user = FieldAttribute(isa='string') + _sudo = FieldAttribute(isa='bool') + _sudo_user = FieldAttribute(isa='string') + _sudo_pass = FieldAttribute(isa='string') + _transport = FieldAttribute(isa='string') + _until = FieldAttribute(isa='list') # ? - role = Attribute() - block = Attribute() + _role = Attribute() + _block = Attribute() def __init__(self, block=None, role=None): ''' constructors a task, without the Task.load classmethod, it will be pretty blank ''' @@ -92,17 +92,13 @@ class Task(Base): def get_name(self): ''' return the name of the task ''' - # FIXME: getattr magic in baseclass so this is not required: - original = self.attribute_value('name') - role_value = self.attribute_value('role') - - if role_value: - return "%s : %s" % (role_value.get_name(), original) + if self.role: + return "%s : %s" % (self.role.get_name(), self.name) else: - return original + return self.name @staticmethod - def load(data, block=block, role=role): + def load(data, block=None, role=None): t = Task(block=block, role=role) return t.load_data(data)