mirror of
https://github.com/caronc/apprise.git
synced 2024-11-22 08:04:02 +01:00
refactored cli; renaming notify.py to apprise + testing
This commit is contained in:
parent
1002f60be1
commit
2a64c6bafb
6
README
6
README
@ -157,15 +157,15 @@ The easiest way is to install from pypi:
|
||||
|
||||
Command Line
|
||||
============
|
||||
A small command line tool is also provided with this package called notify. If you know the server url's you wish to notify, you can simply provide them all on the command line and send your notifications that way:
|
||||
A small command line tool is also provided with this package called apprise. If you know the server url's you wish to notify, you can simply provide them all on the command line and send your notifications that way:
|
||||
|
||||
Send a notification to as many servers as you want to specify
|
||||
notify -t 'my title' -b 'my notification body' \
|
||||
apprise -t 'my title' -b 'my notification body' \
|
||||
'mailto://myemail:mypass@gmail.com' \
|
||||
'pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b'
|
||||
|
||||
If you don't specify a --body (-b) then stdin is used allowing you to use the tool as part of your every day administration:
|
||||
cat /proc/cpuinfo | notify -t 'cpu info' \
|
||||
cat /proc/cpuinfo | apprise -t 'cpu info' \
|
||||
'mailto://myemail:mypass@gmail.com'
|
||||
|
||||
Developers
|
||||
|
@ -64,16 +64,16 @@ The easiest way is to install from pypi:
|
||||
pip install apprise
|
||||
```
|
||||
## Command Line
|
||||
A small command line tool is also provided with this package called *notify*. If you know the server url's you wish to notify, you can simply provide them all on the command line and send your notifications that way:
|
||||
A small command line tool is also provided with this package called *apprise*. If you know the server url's you wish to notify, you can simply provide them all on the command line and send your notifications that way:
|
||||
```bash
|
||||
# Send a notification to as many servers as you want to specify
|
||||
notify -t 'my title' -b 'my notification body' \
|
||||
apprise -t 'my title' -b 'my notification body' \
|
||||
'mailto://myemail:mypass@gmail.com' \
|
||||
'pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b'
|
||||
|
||||
# If you don't specify a --body (-b) then stdin is used allowing
|
||||
# you to use the tool as part of your every day administration:
|
||||
cat /proc/cpuinfo | notify -t 'cpu info' \
|
||||
cat /proc/cpuinfo | apprise -t 'cpu info' \
|
||||
'mailto://myemail:mypass@gmail.com'
|
||||
```
|
||||
|
||||
|
33
cli/notify.py → apprise/cli.py
Executable file → Normal file
33
cli/notify.py → apprise/cli.py
Executable file → Normal file
@ -1,12 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Apprise notify CLI tool
|
||||
# Apprise CLI Tool
|
||||
#
|
||||
#
|
||||
# Apprise Core
|
||||
#
|
||||
# Copyright (C) 2017 Chris Caron <lead2gold@gmail.com>
|
||||
# Copyright (C) 2017-2018 Chris Caron <lead2gold@gmail.com>
|
||||
#
|
||||
# This file is part of apprise.
|
||||
#
|
||||
@ -27,8 +24,9 @@ import click
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from apprise import NotifyType
|
||||
import apprise
|
||||
from . import NotifyType
|
||||
from . import Apprise
|
||||
from . import AppriseAsset
|
||||
|
||||
# Logging
|
||||
logger = logging.getLogger('apprise.plugins.NotifyBase')
|
||||
@ -59,12 +57,15 @@ def print_help_msg(command):
|
||||
@click.option('-v', '--verbose', count=True)
|
||||
@click.argument('urls', nargs=-1,
|
||||
metavar='SERVER_URL [SERVER_URL2 [SERVER_URL3]]',)
|
||||
def _main(title, body, urls, notification_type, theme, verbose):
|
||||
def main(title, body, urls, notification_type, theme, verbose):
|
||||
"""
|
||||
Send a notification to all of the specified servers identified by their
|
||||
URLs the content provided within the title, body and notification-type.
|
||||
|
||||
"""
|
||||
# Note: Click ignores the return values of functions it wraps, If you
|
||||
# want to return a specific error code, you must call sys.exit()
|
||||
# as you will see below.
|
||||
|
||||
# Logging
|
||||
ch = logging.StreamHandler(sys.stdout)
|
||||
@ -83,14 +84,14 @@ def _main(title, body, urls, notification_type, theme, verbose):
|
||||
|
||||
if not urls:
|
||||
logger.error('You must specify at least one server URL.')
|
||||
print_help_msg(_main)
|
||||
return 1
|
||||
print_help_msg(main)
|
||||
sys.exit(1)
|
||||
|
||||
# Prepare our asset
|
||||
asset = apprise.AppriseAsset(theme=theme)
|
||||
asset = AppriseAsset(theme=theme)
|
||||
|
||||
# Create our object
|
||||
a = apprise.Apprise(asset=asset)
|
||||
a = Apprise(asset=asset)
|
||||
|
||||
# Load our inventory up
|
||||
for url in urls:
|
||||
@ -102,9 +103,5 @@ def _main(title, body, urls, notification_type, theme, verbose):
|
||||
|
||||
# now print it out
|
||||
if a.notify(title=title, body=body, notify_type=notification_type):
|
||||
return 0
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(_main())
|
||||
sys.exit(0)
|
||||
sys.exit(1)
|
5
setup.py
5
setup.py
@ -29,8 +29,10 @@ install_options = os.environ.get("APPRISE_INSTALL", "").split(",")
|
||||
libonly_flags = set(["lib-only", "libonly", "no-cli", "without-cli"])
|
||||
if libonly_flags.intersection(install_options):
|
||||
console_scripts = []
|
||||
|
||||
else:
|
||||
console_scripts = ['apprise = apprise:_main']
|
||||
# Load our CLI
|
||||
console_scripts = ['apprise = apprise.cli:main']
|
||||
|
||||
setup(
|
||||
name='apprise',
|
||||
@ -52,7 +54,6 @@ setup(
|
||||
'assets/themes/default/*.png',
|
||||
],
|
||||
},
|
||||
scripts=['cli/notify.py', ],
|
||||
install_requires=open('requirements.txt').readlines(),
|
||||
classifiers=(
|
||||
'Development Status :: 5 - Production/Stable',
|
||||
|
87
test/test_cli.py
Normal file
87
test/test_cli.py
Normal file
@ -0,0 +1,87 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Apprise CLI Unit Tests
|
||||
#
|
||||
# Copyright (C) 2017-2018 Chris Caron <lead2gold@gmail.com>
|
||||
#
|
||||
# This file is part of apprise.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
from apprise import cli
|
||||
from apprise import NotifyBase
|
||||
from click.testing import CliRunner
|
||||
from apprise.Apprise import SCHEMA_MAP
|
||||
|
||||
|
||||
def test_apprise_cli():
|
||||
"""
|
||||
API: Apprise() CLI
|
||||
|
||||
"""
|
||||
|
||||
class GoodNotification(NotifyBase):
|
||||
def __init__(self, **kwargs):
|
||||
super(GoodNotification, self).__init__()
|
||||
|
||||
def notify(self, **kwargs):
|
||||
# Pretend everything is okay
|
||||
return True
|
||||
|
||||
class BadNotification(NotifyBase):
|
||||
def __init__(self, **kwargs):
|
||||
super(BadNotification, self).__init__()
|
||||
|
||||
def notify(self, **kwargs):
|
||||
# Pretend everything is okay
|
||||
return False
|
||||
|
||||
# Set up our notification types
|
||||
SCHEMA_MAP['good'] = GoodNotification
|
||||
SCHEMA_MAP['bad'] = BadNotification
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli.main)
|
||||
# no servers specified; we return 1 (non-zero)
|
||||
assert result.exit_code == 1
|
||||
|
||||
result = runner.invoke(cli.main, ['-v'])
|
||||
assert result.exit_code == 1
|
||||
|
||||
result = runner.invoke(cli.main, ['-vv'])
|
||||
assert result.exit_code == 1
|
||||
|
||||
result = runner.invoke(cli.main, ['-vvv'])
|
||||
assert result.exit_code == 1
|
||||
|
||||
result = runner.invoke(cli.main, [
|
||||
'-t', 'test title',
|
||||
'-b', 'test body',
|
||||
'good://localhost',
|
||||
])
|
||||
assert result.exit_code == 0
|
||||
|
||||
result = runner.invoke(cli.main, [
|
||||
'-t', 'test title',
|
||||
'good://localhost',
|
||||
],
|
||||
input='test stdin body\n',
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
|
||||
result = runner.invoke(cli.main, [
|
||||
'-t', 'test title',
|
||||
'-b', 'test body',
|
||||
'bad://localhost',
|
||||
])
|
||||
assert result.exit_code == 1
|
Loading…
Reference in New Issue
Block a user