Skip to content

Commit

Permalink
Allow overriding backend in it's own config, fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
dschep committed Mar 16, 2016
1 parent bb95dcb commit 8d759c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.rst
Expand Up @@ -111,6 +111,18 @@ Title is configurable with the `title` key in the config. Example:
---
title: Customized Title

If you want mulitple configs for the same backend type, you can specify any
name and then specify the backend with a backend key. For example:

::

---
pushover:
user_key: hunter2
cellphone:
backend: pushover
user_key: hunter2


Backends
--------
Expand Down
7 changes: 4 additions & 3 deletions ntfy/__init__.py
Expand Up @@ -24,6 +24,10 @@ def notify(message, title, config=None, **kwargs):
ret = 0

for backend in config.get('backends', ['default']):
backend_config = config.get(backend, {})
backend_config.update(kwargs)
if 'backend' in backend_config:
backend = backend_config['backend']
try:
module = import_module('ntfy.backends.{}'.format(backend))
except ImportError:
Expand All @@ -33,9 +37,6 @@ def notify(message, title, config=None, **kwargs):
exc_info=True)
continue

backend_config = config.get(backend, {})
backend_config.update(kwargs)

try:
module.notify(message=message, title=title, **backend_config)
except (SystemExit, KeyboardInterrupt):
Expand Down
19 changes: 19 additions & 0 deletions tests/ntfy_test/__init__.py
@@ -1 +1,20 @@
from unittest import TestCase

from mock import patch

from ntfy import notify

from . import cli


class OverrideBackendTestCase(TestCase):
@patch('requests.post')
def test_runcmd(self, mock_post):
ret = notify('message', 'title', {
'backends': ['foobar'],
'foobar': {
'backend': 'pushover',
'user_key': 't0k3n',
}
})
self.assertEqual(ret, 0)

0 comments on commit 8d759c9

Please sign in to comment.