📜 ⬆️ ⬇️

Notifications of endings of fabric tasks, with decorators and detailed information

In the existing project there are long-playing fab-tasks - receiving dumps from remote servers, data aggregation, is. You start the task, get distracted in the next window, after N minutes (or even an hour) you remember, check ... ineffectively.
I wanted to make pop-up notifications on the desktop at the end of the task, and here the article about notify-send turned up. I decided to make a decorator on fab-functions - the very place for him.

First, we write a simple notification function.
from django.conf import settings as django_settings from fabric.operations import local def _notify(message): if django_settings.FAB_NOTIFY_TASK_ENDS: local(u'notify-send --expire-time=10000 "Fabric notify" "{}"'.format(message)) 

Yes, I have a jung and setting “show hints” in the settings.

Using _notify is also simple:
 from fab_utils import _notify def mongo_get_from_remote(server='', date='', collection=''): u"""   c  """ ... _notify(u" ") 

But in each function to write a call at the end ... somehow it is not "dry" (DRY taxis). We will write a decorator
 def notified(wrapped): def internal(*args, **kwargs): res = wrapped(*args, **kwargs) params = [unicode(a) for a in args] params.extend([u'{}={}'.format(k, v) for k, v in kwargs.iteritems()]) params = [_limit_str(p) for p in params] message = "{}({}) ended!!!".format(wrapped.__name__, ', '.join(params)) _notify(message) return res return internal 


And then the use is
 from fab_utils import notified @notified def mongo_get_from_remote(server='', date='', collection=''): u"""   c  """ ... 

')
All is good, but the command help is no longer displayed :(
 wad@wad-vaio:~/aggregator (develop)$ bin/fab.sh -d mongo_get_from_remote Displaying detailed information for task 'mongo_get_from_remote': No docstring provided Arguments: 


What to do? Need to override the docking!
 def notified(wrapped): def internal(*args, **kwargs): ... return res internal.__doc__ = wrapped.__doc__ return internal 

 wad@wad-vaio:~/aggregator (develop!)$ bin/fab.sh -d mongo_get_from_remote Displaying detailed information for task 'mongo_get_from_remote':    c   Arguments: 

Already better, but where are the arguments? They had to crawl into the fabric guts - how does he determine them?

The file env / local / lib / python2.7 / site-packages / fabric / main.py: 466 was found
 def display_command(name): """ Print command function's docstring, then exit. Invoked with -d/--display. """ ... if hasattr(command, '__details__'): task_details = command.__details__() else: task_details = get_task_details(command) ... 

Yeah, if the function has __details__, then it will be called to define the arguments. Fine!

The final code of the decorator is
 from fabric.tasks import get_task_details def notified(wrapped): def internal(*args, **kwargs): res = wrapped(*args, **kwargs) params = [unicode(a) for a in args] params.extend([u'{}={}'.format(k, v) for k, v in kwargs.iteritems()]) params = [_limit_str(p) for p in params] message = "{}({}) ended!!!".format(wrapped.__name__, ', '.join(params)) _notify(message) return res def _details(): return get_task_details(wrapped) internal.__doc__ = wrapped.__doc__ internal.__details__ = _details return internal 


And a description of the fab-problem in place
 wad@wad-vaio:~/aggregator (develop!)$ bin/fab.sh -d mongo_get_from_remote Displaying detailed information for task 'mongo_get_from_remote':    c   Arguments: server='', date='', collection='' 


I love python :)

Source: https://habr.com/ru/post/214259/


All Articles