Skip to content

Instantly share code, notes, and snippets.

@knowsuchagency knowsuchagency/run.py
Last active Jul 5, 2018

Embed
What would you like to do?
cli script template
#!/usr/bin/env python3
from functools import singledispatch, wraps
from pathlib import Path
import types
import os
from shell_utils import shell, cd
import click
@singledispatch
def notify(message: str, title='run.py', sound=None):
"""Mac os pop-up notification."""
sound_arg = '' if sound is None else '--sound ' + sound
shell(f'notify --title {title} {sound_arg} {message}',
capture=True, show_command=False)
@notify.register(types.FunctionType)
def _(func):
"""
Send notification that task has finished.
Especially useful for long-running tasks
"""
@wraps(func)
def inner(*args, **kwargs):
result = None
message = 'Succeeded!'
try:
result = func(*args, **kwargs)
except Exception:
message = 'Failed'
raise
else:
return result
finally:
notify(message, title=getattr(func, '__name__'))
return inner
@click.group()
def main():
"""
Development tasks for shell-utils
"""
# ensure we're running commands from project root
root = Path(__file__).parent.absolute()
cwd = Path().absolute()
if root != cwd:
click.secho(f'Navigating from {cwd} to {root}',
fg='yellow')
os.chdir(root)
if root != cwd:
click.secho(f'Navigating from {cwd} to {root}', fg='yellow')
os.chdir(root)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.