#3 - Simplified command-line interface for pusher

This commit is contained in:
Fabio Manganiello 2017-11-04 14:02:56 +01:00
parent 12abef5b05
commit 5d2a0cffb5
2 changed files with 22 additions and 25 deletions

View file

@ -62,7 +62,10 @@ class LightBatsignalPlugin(LightPlugin):
def _init(self): def _init(self):
self.batsignal = batman.Batsignal(self.config['intensity']) self.batsignal = batman.Batsignal(self.config['intensity'])
def on(self): def on(self, urgent=False):
if distress:
self.batsignal.notify_robin()
self.batsignal.on() self.batsignal.on()
def off(self): def off(self):
@ -82,5 +85,6 @@ class LightBatsignalPlugin(LightPlugin):
8. Test your new plugin by sending some bullets to it: 8. Test your new plugin by sending some bullets to it:
```shell ```shell
echo '{}' | pusher --target your_pc --action light.batsignal.on pusher --target your_pc --action light.batsignal.on --urgent 1
``` ```

View file

@ -1,11 +1,12 @@
#!python #!python
import argparse
import json import json
import os import os
import re
import sys import sys
import yaml import yaml
from getopt import getopt
from pushbullet import Pushbullet from pushbullet import Pushbullet
from runbullet import parse_config_file from runbullet import parse_config_file
@ -34,34 +35,26 @@ def main():
'your PushBullet account'.format(config['pushbullet']['device'])) 'your PushBullet account'.format(config['pushbullet']['device']))
return return
optlist, args = getopt(sys.argv[1:], 'ht:a:', parser = argparse.ArgumentParser()
['help', 'target=', 'action=']) parser.add_argument('--target', '-t', dest='target', required=True,
help="Destination of the command")
target = None parser.add_argument('--action', '-a', dest='action', required=True,
action = None help="Action to execute, as package.method")
opts, args = parser.parse_known_args(sys.argv[1:])
payload = {} payload = {}
for opt, arg in optlist: if len(args) % 2 != 0:
if opt == 'h' or opt == '--help': raise RuntimeError('Odd number of key-value options passed: {}'.
print_usage() format(args))
return
elif opt == 't' or opt == '--target':
target = arg
elif opt == 'a' or opt == '--action':
action = arg
if len(args): for i in range(0, len(args), 2):
payload = json.loads(args[0]) payload[re.sub('^-+', '', args[i])] = args[i+1]
else:
payload = json.loads(sys.stdin.read())
if not (target and action):
print_usage()
return
msg = { msg = {
'target': target, 'target': opts.target,
'action': action, 'action': opts.action,
**payload, **payload,
} }