#!/usr/bin/python2 -tt from __future__ import (absolute_import, division, print_function) __metaclass__ = type from functools import partial from inspect import getmembers from ansible.playbook import Attribute, FieldAttribute class Base: def __init__(self): self._attributes = dict() for (name, value) in self._get_base_attributes().items(): getter = partial(self._generic_g, name) setter = partial(self._generic_s, name) deleter = partial(self._generic_d, name) setattr(Base, name, property(getter, setter, deleter)) setattr(self, name, value.default) @staticmethod def _generic_g(key, self): return self._attributes[key] @staticmethod def _generic_s(key, self, value): self._attributes[key] = value @staticmethod def _generic_d(key, self): del self._attributes[key] # Verbatim from playbook/base.py::Base def _get_base_attributes(self): ''' Returns the list of attributes for this class (or any subclass thereof). If the attribute name starts with an underscore, it is removed ''' base_attributes = dict() for (name, value) in getmembers(self.__class__): if isinstance(value, Attribute): if name.startswith('_'): name = name[1:] base_attributes[name] = value return base_attributes class FakeTask(Base): _args = FieldAttribute(isa='dict', default=dict()) _action = FieldAttribute(isa='string') _loop = FieldAttribute(isa='string', private=True) _retries = FieldAttribute(isa='int', default=1, private=False) if __name__ == '__main__': t = FakeTask() print(t.args) t.args = dict(one=1, two=2, three=3) print(t.args) t.args['four'] = 4 print(t.args) print(t.action) print(t.loop) print(t.retries)