Skip to content

Commit

Permalink
added piping support
Browse files Browse the repository at this point in the history
  • Loading branch information
jrodal98 committed Mar 19, 2020
1 parent 81cab25 commit 791ba37
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# nma
# *N*otify *M*e *A*fter

## About

*N*otify *M*e *A*fter (nma) is a python package that sends a desktop notification whenever a shell process or decorated function finishes execution. This repository provides a python shell script for general use and a decorator for use within python code.

## Requirements:

Expand All @@ -13,10 +17,6 @@

enter the `nma` directory and run `python3 -m pip install .`

## About

This python package contains a python shell script (and decorator) that sends a desktop notification whenever a shell process or decorated function finishes execution.

## Examples

### In shell scripts
Expand All @@ -29,18 +29,14 @@ This python package contains a python shell script (and decorator) that sends a

`nma "sleep 1; echo test"`

or in actual scripts:
`sleep 1; echo test | nma -`

```bash
my_func () {
ls *
sleep 2
echo "nice"
my_func() {
sleep .1;
}

nma my_func
my_func | nma -
```

### Decorator

```python
Expand All @@ -50,3 +46,8 @@ def test():
print("basic test")
```

## Unimplemented Features

* ~~Call script via piping (ex: `sleep 1 | nma -`)~~
* Email notifications
* Text notifications
7 changes: 6 additions & 1 deletion nma/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def call_from_shell(argv):
try:
if len(argv) < 2:
raise IndexError
return subprocess.run(argv[1], shell=True, check=True)
if argv[1] == "-":
for line in sys.stdin:
sys.stdout.write(line)
return 0
else:
return subprocess.run(argv[1], shell=True, check=True)
except IndexError:
send_notification("nma error",
"shell command not provided", error=True)
Expand Down
8 changes: 5 additions & 3 deletions nma/notify_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def notify_after(wrapped_func):
def wrapper(*args, **kwargs):
res = wrapped_func(*args, **kwargs)
try:
process = "Python"
name = wrapped_func.__name__
if name == "call_from_shell":
name = process = args[0][1]
title = f"{process} execution completed"
if args[0][1] == "-":
name = "nma from stdin"
else:
name = args[0][1]
title = "nma notification"
message = f"'{name}' has finished executing."
send_notification(title, message)
except Exception as e:
Expand Down

0 comments on commit 791ba37

Please sign in to comment.