From 8c88cdde7f05a631109e8781a945e3ee3f4a22bd Mon Sep 17 00:00:00 2001 From: Mike Place Date: Thu, 13 Nov 2014 14:41:06 -0700 Subject: [PATCH 1/2] Initial sketch of django returner --- salt/returners/django_return.py | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 salt/returners/django_return.py diff --git a/salt/returners/django_return.py b/salt/returners/django_return.py new file mode 100644 index 0000000000..323a7cc3dc --- /dev/null +++ b/salt/returners/django_return.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +''' +A returner that will infor a Django system that +returns are available using Django's signal system. + +https://docs.djangoproject.com/en/dev/topics/signals/ + +It is up to the Django developer to register necessary +handlers with the signals provided by this returner +and process returns as necessary. + +The easiest way to use signals is to import them from +this returner directly and then use a decorator to register +them. + +An example Django module that registers a function called +'returner_callback' with this module's 'returner' function: + + .. code-block:: python + + import salt.returners.django_return + from django.dispatch import receiver + + @receiver(salt.returners.django_return, sender=returner) + def returner_callback(sender, ret): + print('I received {0} from {1}'.format(ret, sender)) + +''' +# Import Python libraries +from __future__ import absolute_import +import logging + +# Import Salt libraries +import salt.returners +import salt.utils + +log = logging.getLogger(__name__) + +HAS_DJANGO = False + +try: + from django import dispatch + HAS_DJANGO = True +except ImportError: + HAS_DJANGO = False + +# Define this module's virtual name +__virtualname__ = 'django' + +def __virtual__(): + if not HAS_DJANGO: + return False + return True + +def returner(ret): + ''' + Signal a Django server that a return is available + ''' + signaled = dispatch.Signal(providing_args=['ret']).send(sender='returner', ret=ret) + + for signal in signaled: + log.debug('Django returner function \'returner\' signaled {0} ' + 'which responded with {1}'.format(signal[0], signal[1])) + +def save_load(jid, load): + ''' + Save the load to the specified jid + ''' + signaled = dispatch.Signal( + providing_args=['jid', 'load']).send( + sender='save_load', jid=jid, load=load) + + for signal in signaled: + log.debug('Django returner function \'save_load\' signaled {0} ' + 'which responded with {1}'.format(signal[0], signal[1])) + +def prep_jid(nocache, passed_jid=None): + ''' + Do any work necessary to prepare a JID, including sending a custom ID + ''' + return passed_jid if passed_jid is not None else salt.utils.gen_jid() From 0f3f3e6ba19a03ba7bef2f414e5e4dfef0144236 Mon Sep 17 00:00:00 2001 From: Thomas S Hatch Date: Fri, 14 Nov 2014 14:40:57 -0700 Subject: [PATCH 2/2] lint --- salt/returners/django_return.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/salt/returners/django_return.py b/salt/returners/django_return.py index 323a7cc3dc..244a12707a 100644 --- a/salt/returners/django_return.py +++ b/salt/returners/django_return.py @@ -45,13 +45,15 @@ except ImportError: HAS_DJANGO = False # Define this module's virtual name -__virtualname__ = 'django' +__virtualname__ = 'django' + def __virtual__(): if not HAS_DJANGO: return False return True + def returner(ret): ''' Signal a Django server that a return is available @@ -62,6 +64,7 @@ def returner(ret): log.debug('Django returner function \'returner\' signaled {0} ' 'which responded with {1}'.format(signal[0], signal[1])) + def save_load(jid, load): ''' Save the load to the specified jid @@ -74,6 +77,7 @@ def save_load(jid, load): log.debug('Django returner function \'save_load\' signaled {0} ' 'which responded with {1}'.format(signal[0], signal[1])) + def prep_jid(nocache, passed_jid=None): ''' Do any work necessary to prepare a JID, including sending a custom ID