From b9223e599567d43af2c1a7380a14787150db49ef Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 3 Oct 2014 15:01:59 -0400 Subject: [PATCH] Add attribute starter notes --- v2/ansible/__init__.py | 1 + v2/ansible/playbook/attribute.py | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 v2/ansible/__init__.py create mode 100644 v2/ansible/playbook/attribute.py diff --git a/v2/ansible/__init__.py b/v2/ansible/__init__.py new file mode 100644 index 00000000000..44026bdff03 --- /dev/null +++ b/v2/ansible/__init__.py @@ -0,0 +1 @@ +# TODO: header diff --git a/v2/ansible/playbook/attribute.py b/v2/ansible/playbook/attribute.py new file mode 100644 index 00000000000..ae1edea943f --- /dev/null +++ b/v2/ansible/playbook/attribute.py @@ -0,0 +1,52 @@ +# (c) 2012-2014, Michael DeHaan +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +#from ansible.common.errors import AnsibleError + +class MyMeta(type): + + def __call__(self, *args, **kwargs): + + obj = type.__call__(self, *args) + for name, value in kwargs.items(): + setattr(obj, name, value) + return obj + +class Attribute(object): + + __metaclass__ = MyMeta + + def load(self, data, base_object): + ''' the loader is called to store the attribute from a datastructure when key names match. The default is very basic ''' + self._validate(base_object, data) + setattr(base_object, self.name, data) + + def _validate(self, data, base_object): + ''' validate is called after loading an object data structure to massage any input or raise errors on any incompatibilities ''' + if self.validator: + self.validator(base_object) + + def post_validate(self, base_object): + ''' post validate is called after templating the context of a Task (usually in Runner) to validate the types of arguments ''' + if self.post_validator: + self.post_validator(base_object) + +class FieldAttribute(Attribute): + + __metaclass__ = MyMeta + pass +