Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log ImportError exception. #261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ntfy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ def notify(message, title, config=None, **kwargs):
del backend_config['title']

try:
notifier = import_module('ntfy.backends.{}'.format(backend))
except ImportError:
backend_notifier_path = 'ntfy.backends.{}'.format(backend)
notifier = import_module(backend_notifier_path)
except ImportError as e:
logging.getLogger(__name__).error(
"Could not import notifier from '{}'. Reason: {}"
.format(backend_notifier_path, e))
try:
notifier = import_module(backend)
except ImportError:
Expand Down
92 changes: 92 additions & 0 deletions ntfy/__init__.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import logging
from getpass import getuser
from os import getcwd, path, name
from socket import gethostname
from importlib import import_module
from inspect import getargspec
from .backends.default import DefaultNotifierError

__version__ = '2.7.1'

_user_home = path.expanduser('~')
_cwd = getcwd()
if name != 'nt' and _cwd.startswith(_user_home):
default_title = '{}@{}:{}'.format(
getuser(), gethostname(), path.join('~', _cwd[len(_user_home) + 1:]))
else:
default_title = '{}@{}:{}'.format(getuser(), gethostname(), _cwd)


def notify(message, title, config=None, **kwargs):
from .config import load_config

if config is None:
config = load_config()

ret = 0
retcode = kwargs.pop('retcode', None)

for backend in config.get('backends', ['default']):
backend_config = config.get(backend, {})
backend_config.update(kwargs)
if 'backend' in backend_config:
backend = backend_config.pop('backend')

if title is None:
title = backend_config.pop('title',
config.get('title', default_title))
elif 'title' in backend_config:
del backend_config['title']

try:
backend_notifier_path = 'ntfy.backends.{}'.format(backend)
notifier = import_module(backend_notifier_path)
except ImportError as e:
logging.getLogger(__name__).error(
'Could not import notifier from {}. Reason: {}'
.format(backend_notifier_path, e))
try:
notifier = import_module(backend)
except ImportError:
logging.getLogger(__name__).error(
'Invalid backend {}'.format(backend))
ret = 1
continue

try:
notify_ret = notifier.notify(
message=message,
title=title,
retcode=retcode,
**backend_config)
if notify_ret:
ret = notify_ret
except (SystemExit, KeyboardInterrupt):
raise
except Exception as e:
ret = 1
if isinstance(e, DefaultNotifierError):
notifier = e.module
e = e.exception

args, _, _, defaults = getargspec(notifier.notify)
possible_args = set(args)
required_args = set(args) if defaults is None else set(args[:-len(defaults)])
required_args -= set(['title', 'message', 'retcode'])
unknown_args = set(backend_config) - possible_args
missing_args = required_args - set(backend_config)

if unknown_args:
logging.getLogger(__name__).error(
'Got unknown arguments: {}'.format(unknown_args))

if missing_args:
logging.getLogger(__name__).error(
'Missing arguments: {}'.format(missing_args))

if not any([unknown_args, missing_args]):
logging.getLogger(__name__).error(
'Failed to send notification using {}'.format(backend),
exc_info=True)

return ret