1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

__getattr__ to hide some of the attribute magic.

This commit is contained in:
Michael DeHaan 2014-10-03 17:08:52 -04:00
parent a175168686
commit 94db7365b9
4 changed files with 75 additions and 63 deletions

View file

@ -31,9 +31,9 @@ class TestTask(unittest.TestCase):
def test_can_load_simple_task(self): def test_can_load_simple_task(self):
t = Task.load(basic_shell_task) t = Task.load(basic_shell_task)
assert t is not None 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.name == basic_shell_task['name']
assert t.module == 'shell' #assert t.module == 'shell'
assert t.args == 'echo hi' #assert t.args == 'echo hi'

View file

@ -18,14 +18,13 @@
#from ansible.common.errors import AnsibleError #from ansible.common.errors import AnsibleError
class Attribute(object): class Attribute(object):
def __init__(self, isa=None):
def __init__(self, isa=None, validator=None, post_validator=None):
self.isa = isa self.isa = isa
self.validator = validator
self.post_validator = post_validator
self.value = None self.value = None
class FieldAttribute(Attribute): def __call__(self):
return self.value
class FieldAttribute(Attribute):
pass pass

View file

@ -25,43 +25,60 @@ class Base(object):
self._data = dict() self._data = dict()
self._attributes = 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): def load_data(self, ds):
''' walk the input datastructure and assign any values ''' ''' walk the input datastructure and assign any values '''
assert ds is not None assert ds is not None
for name in self.__class__.__dict__: for (name, attribute) in self.__class__.__dict__.iteritems():
aname = name[1:]
print "DEBUG: processing attribute: %s" % name
attribute = self.__class__.__dict__[name]
# process Fields
if isinstance(attribute, FieldAttribute): if isinstance(attribute, FieldAttribute):
method = getattr(self, '_load_%s' % name, None) method = getattr(self, '_load_%s' % aname, None)
if method: if method:
self._attributes[name] = method(self, attribute) self._attributes[aname] = method(self, attribute)
else: else:
if name in ds: if aname in ds:
self._attributes[name] = ds[name] self._attributes[aname] = ds[aname]
# implement PluginAtrribute which allows "with_" and "action" aliases. # TODO: implement PluginAtrribute which allows "with_" and "action" aliases.
return self return self
def attribute_value(self, name):
return self._attributes[name]
def validate(self): def validate(self):
# TODO: finish
for name in self.__dict__: for name in self.__dict__:
attribute = self.__dict__[name] aname = name[1:]
attribute = self.__dict__[aname]
if instanceof(attribute, FieldAttribute): if instanceof(attribute, FieldAttribute):
method = getattr(self, '_validate_%s' % (prefix, name), None) method = getattr(self, '_validate_%s' % (prefix, aname), None)
if method: if method:
method(self, attribute) method(self, attribute)
def post_validate(self, runner_context): def post_validate(self, runner_context):
# TODO: finish
raise exception.NotImplementedError 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

View file

@ -43,45 +43,45 @@ class Task(Base):
# will be used if defined # will be used if defined
# might be possible to define others # might be possible to define others
action = FieldAttribute(isa='string') _action = FieldAttribute(isa='string')
always_run = FieldAttribute(isa='bool') _always_run = FieldAttribute(isa='bool')
any_errors_fatal = FieldAttribute(isa='bool') _any_errors_fatal = FieldAttribute(isa='bool')
async = FieldAttribute(isa='int') _async = FieldAttribute(isa='int')
connection = FieldAttribute(isa='string') _connection = FieldAttribute(isa='string')
delay = FieldAttribute(isa='int') _delay = FieldAttribute(isa='int')
delegate_to = FieldAttribute(isa='string') _delegate_to = FieldAttribute(isa='string')
environment = FieldAttribute(isa='dict') _environment = FieldAttribute(isa='dict')
first_available_file = FieldAttribute(isa='list') _first_available_file = FieldAttribute(isa='list')
ignore_errors = FieldAttribute(isa='bool') _ignore_errors = FieldAttribute(isa='bool')
# FIXME: this should not be a Task # FIXME: this should not be a Task
# include = FieldAttribute(isa='string') # include = FieldAttribute(isa='string')
local_action = FieldAttribute(isa='string') _local_action = FieldAttribute(isa='string')
# FIXME: this should not be a Task # 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') _no_log = FieldAttribute(isa='bool')
notify = FieldAttribute(isa='list') _notify = FieldAttribute(isa='list')
poll = FieldAttribute(isa='integer') _poll = FieldAttribute(isa='integer')
register = FieldAttribute(isa='string') _register = FieldAttribute(isa='string')
remote_user = FieldAttribute(isa='string') _remote_user = FieldAttribute(isa='string')
retries = FieldAttribute(isa='integer') _retries = FieldAttribute(isa='integer')
run_once = FieldAttribute(isa='bool') _run_once = FieldAttribute(isa='bool')
su = FieldAttribute(isa='bool') _su = FieldAttribute(isa='bool')
su_pass = FieldAttribute(isa='string') _su_pass = FieldAttribute(isa='string')
su_user = FieldAttribute(isa='string') _su_user = FieldAttribute(isa='string')
sudo = FieldAttribute(isa='bool') _sudo = FieldAttribute(isa='bool')
sudo_user = FieldAttribute(isa='string') _sudo_user = FieldAttribute(isa='string')
sudo_pass = FieldAttribute(isa='string') _sudo_pass = FieldAttribute(isa='string')
transport = FieldAttribute(isa='string') _transport = FieldAttribute(isa='string')
until = FieldAttribute(isa='list') # ? _until = FieldAttribute(isa='list') # ?
role = Attribute() _role = Attribute()
block = Attribute() _block = Attribute()
def __init__(self, block=None, role=None): def __init__(self, block=None, role=None):
''' constructors a task, without the Task.load classmethod, it will be pretty blank ''' ''' constructors a task, without the Task.load classmethod, it will be pretty blank '''
@ -92,17 +92,13 @@ class Task(Base):
def get_name(self): def get_name(self):
''' return the name of the task ''' ''' return the name of the task '''
# FIXME: getattr magic in baseclass so this is not required: if self.role:
original = self.attribute_value('name') return "%s : %s" % (self.role.get_name(), self.name)
role_value = self.attribute_value('role')
if role_value:
return "%s : %s" % (role_value.get_name(), original)
else: else:
return original return self.name
@staticmethod @staticmethod
def load(data, block=block, role=role): def load(data, block=None, role=None):
t = Task(block=block, role=role) t = Task(block=block, role=role)
return t.load_data(data) return t.load_data(data)