Allow field attributes to have priorities

So that roles are loaded ahead of all other play fields, meaning any
plugins contained within the roles are loaded first before tasks.

Fixes #11858
This commit is contained in:
James Cammarata 2015-08-05 13:54:00 -04:00
parent fad44862a5
commit 37e38924de
3 changed files with 13 additions and 14 deletions

View file

@ -21,13 +21,17 @@ __metaclass__ = type
class Attribute: class Attribute:
def __init__(self, isa=None, private=False, default=None, required=False, listof=None): def __init__(self, isa=None, private=False, default=None, required=False, listof=None, priority=0):
self.isa = isa self.isa = isa
self.private = private self.private = private
self.default = default self.default = default
self.required = required self.required = required
self.listof = listof self.listof = listof
self.priority = priority
def __cmp__(self, other):
return cmp(other.priority, self.priority)
class FieldAttribute(Attribute): class FieldAttribute(Attribute):
pass pass

View file

@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import itertools import itertools
import operator
import uuid import uuid
from functools import partial from functools import partial
@ -153,24 +154,18 @@ class Base:
else: else:
self._loader = DataLoader() self._loader = DataLoader()
# FIXME: is this required anymore? This doesn't seem to do anything
# helpful, and was added in very early stages of the base class
# development.
#if isinstance(ds, string_types) or isinstance(ds, FileIO):
# ds = self._loader.load(ds)
# call the preprocess_data() function to massage the data into # call the preprocess_data() function to massage the data into
# something we can more easily parse, and then call the validation # something we can more easily parse, and then call the validation
# function on it to ensure there are no incorrect key values # function on it to ensure there are no incorrect key values
ds = self.preprocess_data(ds) ds = self.preprocess_data(ds)
self._validate_attributes(ds) self._validate_attributes(ds)
# Walk all attributes in the class. # Walk all attributes in the class. We sort them based on their priority
# # so that certain fields can be loaded before others, if they are dependent.
# FIXME: we currently don't do anything with private attributes but # FIXME: we currently don't do anything with private attributes but
# may later decide to filter them out of 'ds' here. # may later decide to filter them out of 'ds' here.
base_attributes = self._get_base_attributes()
for name in self._get_base_attributes(): for name, attr in sorted(base_attributes.items(), key=operator.itemgetter(1)):
# copy the value over unless a _load_field method is defined # copy the value over unless a _load_field method is defined
if name in ds: if name in ds:
method = getattr(self, '_load_%s' % name, None) method = getattr(self, '_load_%s' % name, None)

View file

@ -69,15 +69,15 @@ class Play(Base, Taggable, Become):
_vars_prompt = FieldAttribute(isa='list', default=[]) _vars_prompt = FieldAttribute(isa='list', default=[])
_vault_password = FieldAttribute(isa='string') _vault_password = FieldAttribute(isa='string')
# Role Attributes
_roles = FieldAttribute(isa='list', default=[], priority=100)
# Block (Task) Lists Attributes # Block (Task) Lists Attributes
_handlers = FieldAttribute(isa='list', default=[]) _handlers = FieldAttribute(isa='list', default=[])
_pre_tasks = FieldAttribute(isa='list', default=[]) _pre_tasks = FieldAttribute(isa='list', default=[])
_post_tasks = FieldAttribute(isa='list', default=[]) _post_tasks = FieldAttribute(isa='list', default=[])
_tasks = FieldAttribute(isa='list', default=[]) _tasks = FieldAttribute(isa='list', default=[])
# Role Attributes
_roles = FieldAttribute(isa='list', default=[])
# Flag/Setting Attributes # Flag/Setting Attributes
_any_errors_fatal = FieldAttribute(isa='bool', default=False) _any_errors_fatal = FieldAttribute(isa='bool', default=False)
_force_handlers = FieldAttribute(isa='bool') _force_handlers = FieldAttribute(isa='bool')