diff --git a/platypush/__init__.py b/platypush/__init__.py index 7cdfe84c..e9fa0b12 100644 --- a/platypush/__init__.py +++ b/platypush/__init__.py @@ -126,7 +126,7 @@ def parse_config_file(config_file=None): def get_backends(config): - backends = [] + backends = {} for k in config.keys(): if k.startswith('backend.'): @@ -143,7 +143,8 @@ def get_backends(config): try: b = getattr(module, cls_name)(config[k]) - backends.append(b) + name = '.'.join((k.split('.'))[1:]) + backends[name] = b except AttributeError as e: logging.warn('No such class in {}: {}'.format( module.__name__, cls_name)) @@ -151,6 +152,15 @@ def get_backends(config): return backends + +def get_default_pusher_backend(config): + backends = ['.'.join((k.split('.'))[1:]) + for k in config.keys() if k.startswith('backend.') + and 'pusher' in config[k] and config[k]['pusher'] is True] + + return backends[0] if backends else None + + def get_device_id(): global config return config['device_id'] @@ -196,7 +206,7 @@ Usage: {} [-v] [-h] [-c ] mq = Queue() backends = get_backends(config) - for backend in backends: + for backend in backends.values(): backend.mq = mq backend.start() diff --git a/platypush/bin/pusher b/platypush/bin/pusher deleted file mode 100755 index 11c60908..00000000 --- a/platypush/bin/pusher +++ /dev/null @@ -1,105 +0,0 @@ -#!python - -import argparse -import json -import os -import re -import sys -import yaml - -from platypush import parse_config_file -from platypush.backend.kafka import KafkaBackend -from platypush.backend.local import LocalBackend -from platypush.backend.pushbullet import PushbulletBackend - - -def print_usage(): - print ('''Usage: {} [-h|--help] <-t|--target > <-a|--action > payload - -h, --help:\t\tShow this help and exit - -b, --backend:\tBackend to deliver the message [pushbullet|kafka] (default: whatever specified in your config with pusher=True) - -t, --target:\tName of the target device/host - -a, --action\tAction to run, it includes both the package name and the method (e.g. shell.exec or music.mpd.play) - payload:\t\tArguments to the action -'''.format(sys.argv[0])) - -def get_pb_device_by_name(pb, name): - devices = [ - _ for _ in pb.devices if _.nickname == name - ] - - return devices[0] if devices else None - -def get_backend(config): - if 'backend.local' in config \ - and 'pusher' in config['backend.local'] \ - and config['backend.local']['pusher']: - c = config['backend.local'] - return LocalBackend({'fifo': c['fifo']}) - elif 'backend.pushbullet' in config \ - and 'pusher' in config['backend.pushbullet'] \ - and config['backend.pushbullet']['pusher']: - c = config['backend.pushbullet'] - return PushbulletBackend({'token':c['token'], 'device':c['device']}) - elif 'backend.kafka' in config \ - and 'pusher' in config['backend.kafka'] \ - and config['backend.kafka']['pusher']: - c = config['backend.kafka'] - return KafkaBackend({'server':c['server'], 'topic':c['topic']}) - -def main(): - config = parse_config_file() - - parser = argparse.ArgumentParser() - parser.add_argument('--target', '-t', dest='target', required=True, - help="Destination of the command") - - parser.add_argument('--action', '-a', dest='action', required=True, - help="Action to execute, as package.method") - - parser.add_argument('--backend', '-b', dest='backend', required=False, - help="Backend to deliver the message " + - "[pushbullet|kafka|local] (default: whatever " + - "specified in your config with pusher=True)") - - opts, args = parser.parse_known_args(sys.argv[1:]) - payload = {} - - if len(args) % 2 != 0: - raise RuntimeError('Odd number of key-value options passed: {}'. - format(args)) - - if opts.target == 'localhost' and 'backend.local' in config: - cfg = config['backend.local'] - cfg['pusher'] = True - config = { 'backend.local': cfg } - - if opts.backend: - backend_cfg_name = 'backend.' + opts.backend - if backend_cfg_name not in config: - raise RuntimeError('{} backend specified, but no configuration ' + - 'for it available'.format(backend_cfg_name)) - - cfg = config[backend_cfg_name] - cfg['pusher'] = True - config = { backend_cfg_name: cfg } - - backend = get_backend(config) - - for i in range(0, len(args), 2): - payload[re.sub('^-+', '', args[i])] = args[i+1] - - msg = { - 'target': opts.target, - 'action': opts.action, - **payload, - } - - print('msg: {}'.format(msg)) - - backend.send_msg(msg) - -if __name__ == '__main__': - main() - -# vim:sw=4:ts=4:et: - diff --git a/platypush/pusher/__init__.py b/platypush/pusher/__init__.py new file mode 100755 index 00000000..ca00b709 --- /dev/null +++ b/platypush/pusher/__init__.py @@ -0,0 +1,70 @@ +import argparse +import re +import sys + +from platypush import get_backends, get_default_pusher_backend, parse_config_file + +def print_usage(): + print ('''Usage: {} [-h|--help] <-t|--target > <-a|--action > payload + -h, --help:\t\tShow this help and exit + -b, --backend:\tBackend to deliver the message [pushbullet|kafka] (default: whatever specified in your config with pusher=True) + -t, --target:\tName of the target device/host + -a, --action\tAction to run, it includes both the package name and the method (e.g. shell.exec or music.mpd.play) + payload:\t\tArguments to the action +'''.format(sys.argv[0])) + + +def pusher(target, action, backend=None, **kwargs): + config = parse_config_file() + + msg = { + 'target': target, + 'action': action, + **kwargs, + } + + if target == 'localhost': + backend = 'local' + elif not backend: + backend = get_default_pusher_backend(config) + + backends = get_backends(config) + if backend not in backends: + raise RuntimeError('No such backend configured: {}'.format(backend)) + + b = backends[backend] + b.send_msg(msg) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--target', '-t', dest='target', required=True, + help="Destination of the command") + + parser.add_argument('--action', '-a', dest='action', required=True, + help="Action to execute, as package.method") + + parser.add_argument('--backend', '-b', dest='backend', required=False, + help="Backend to deliver the message " + + "[pushbullet|kafka|local] (default: whatever " + + "specified in your config with pusher=True)") + + opts, args = parser.parse_known_args(sys.argv[1:]) + + if len(args) % 2 != 0: + raise RuntimeError('Odd number of key-value options passed: {}'. + format(args)) + + payload = {} + for i in range(0, len(args), 2): + payload[re.sub('^-+', '', args[i])] = args[i+1] + + pusher(target=opts.target, action=opts.action, + backend=opts.backend if 'backend' in opts else None, **payload) + + +if __name__ == '__main__': + main() + +# vim:sw=4:ts=4:et: + diff --git a/setup.py b/setup.py index 32251efa..320cfc54 100755 --- a/setup.py +++ b/setup.py @@ -40,13 +40,11 @@ setup( python_requires = '>= 3', keywords = "pushbullet notifications automation", url = "https://github.com/BlackLight/platypush", - # packages = ['platypush'], packages = find_packages(), - # package_data = {'': plugins}, - scripts = ['platypush/bin/pusher'], entry_points = { 'console_scripts': [ 'platypush=platypush:main', + 'pusher=platypush.pusher:main', ], }, data_files = [