#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):
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
```

View file

@ -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,
}