From 5d2a0cffb553f3ea5c39f97856d29c9e13596883 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 4 Nov 2017 14:02:56 +0100 Subject: [PATCH] #3 - Simplified command-line interface for pusher --- README.md | 8 ++++++-- runbullet/bin/pusher | 39 ++++++++++++++++----------------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4ba369b7c8..f717a46204 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,10 @@ class LightBatsignalPlugin(LightPlugin): def _init(self): self.batsignal = batman.Batsignal(self.config['intensity']) - def on(self): + def on(self, urgent=False): + if distress: + self.batsignal.notify_robin() + self.batsignal.on() def off(self): @@ -82,5 +85,6 @@ class LightBatsignalPlugin(LightPlugin): 8. Test your new plugin by sending some bullets to it: ```shell -echo '{}' | pusher --target your_pc --action light.batsignal.on +pusher --target your_pc --action light.batsignal.on --urgent 1 ``` + diff --git a/runbullet/bin/pusher b/runbullet/bin/pusher index b7fe6aab6b..2f281b9ae8 100755 --- a/runbullet/bin/pusher +++ b/runbullet/bin/pusher @@ -1,11 +1,12 @@ #!python +import argparse import json import os +import re import sys import yaml -from getopt import getopt from pushbullet import Pushbullet from runbullet import parse_config_file @@ -34,34 +35,26 @@ def main(): 'your PushBullet account'.format(config['pushbullet']['device'])) return - optlist, args = getopt(sys.argv[1:], 'ht:a:', - ['help', 'target=', 'action=']) + parser = argparse.ArgumentParser() + parser.add_argument('--target', '-t', dest='target', required=True, + help="Destination of the command") - target = None - action = None + parser.add_argument('--action', '-a', dest='action', required=True, + help="Action to execute, as package.method") + + opts, args = parser.parse_known_args(sys.argv[1:]) payload = {} - for opt, arg in optlist: - if opt == 'h' or opt == '--help': - print_usage() - return - elif opt == 't' or opt == '--target': - target = arg - elif opt == 'a' or opt == '--action': - action = arg + if len(args) % 2 != 0: + raise RuntimeError('Odd number of key-value options passed: {}'. + format(args)) - if len(args): - payload = json.loads(args[0]) - else: - payload = json.loads(sys.stdin.read()) - - if not (target and action): - print_usage() - return + for i in range(0, len(args), 2): + payload[re.sub('^-+', '', args[i])] = args[i+1] msg = { - 'target': target, - 'action': action, + 'target': opts.target, + 'action': opts.action, **payload, }