Get rid of click. Use cmd and shlex instead.

This commit is contained in:
Maurizio Porrato 2019-02-11 12:41:14 +00:00
parent 8bf5da8ccc
commit 33250ee1cb
1 changed files with 96 additions and 41 deletions

137
eufy.py
View File

@ -1,7 +1,15 @@
#!/usr/bin/env python3
import click
from datetime import datetime
import cmd
from datetime import datetime, time
from inspect import getargspec
from shlex import shlex
from time import sleep
def drive_ir(pulses):
# Debugging placeholder
print(','.join([str(x) for x in pulses]))
class EufyRawIR:
@ -24,7 +32,7 @@ class EufyRawIR:
return 3*signal
def sendRaw(self, message):
print(self._modulate(message))
drive_ir(self._modulate(message))
class EufyIR(EufyRawIR):
@ -122,47 +130,94 @@ class EufyIR(EufyRawIR):
self.send(self.RETURN_BASE)
@click.group()
def cli():
pass
def lexer(f):
def g(self, args):
argv = tuple(x.lower() for x in shlex(args))
s = getargspec(f)
maxargs = len(s.args or [])
minargs = maxargs - len(s.defaults or [])
if minargs <= len(argv) + 1 <= maxargs:
return f(self, *argv)
else:
self.stdout.write("*** Invalid arguments for {}\n".format(f.__name__[3:]))
return g
@cli.command()
@click.argument('mode', required=False,
type=click.Choice(['auto', 'spot', 'edge', 'room']))
def clean(mode):
eufy = EufyIR()
dict(
auto=eufy.cleanAuto,
spot=eufy.cleanSpot,
edge=eufy.cleanEdge,
room=eufy.cleanRoom
).get(mode, eufy.start)()
@cli.command()
@click.argument('mode', required=True,
type=click.Choice(['standard', 'boostiq', 'max']))
def power(mode):
eufy = EufyIR()
{
'standard': eufy.powerStandard,
'boostiq': eufy.powerBoostIQ,
'max': eufy.powerMax
}.get(mode)()
class Eufy(cmd.Cmd):
@cli.command()
@click.argument('mode', required=True,
type=click.Choice(['forward', 'backward', 'ccw', 'cw', 'left', 'right']))
def move(mode):
eufy = EufyIR()
dict(
forward=eufy.moveForward,
backward=eufy.moveBackward,
ccw=eufy.moveCCW,
cw=eufy.moveCW,
left=eufy.moveLeft,
right=eufy.moveRight
).get(mode)()
prompt = 'eufy> '
def __init__(self, *args, **kwargs):
super(Eufy, self).__init__(*args, **kwargs)
self.ir = EufyIR()
@lexer
def do_clean(self, mode='auto'):
{
'auto': self.ir.cleanAuto,
'spot': self.ir.cleanSpot,
'edge': self.ir.cleanEdge,
'room': self.ir.cleanRoom
}.get(mode)()
@lexer
def do_start(self):
self.ir.cleanAuto()
@lexer
def do_stop(self):
self.ir.stop()
@lexer
def do_power(self, mode):
{
'standard': self.ir.powerStandard,
'boostiq': self.ir.powerBoostIQ,
'max': self.ir.powerMax
}.get(mode)()
@lexer
def do_move(self, direction):
{
'forward': self.ir.moveForward,
'backward': self.ir.moveBackward,
'ccw': self.ir.moveCCW,
'cw': self.ir.moveCW,
'left': self.ir.moveCCW,
'right': self.ir.moveCW
}.get(direction)()
@lexer
def do_time(self):
self.ir.setTime()
@lexer
def do_schedule(self, t):
newtime = datetime.now()
newtime.strptime(t, "%H:%M")
self.ir.setSchedule(newtime.time())
@lexer
def do_base(self):
self.ir.returnBase()
@lexer
def do_pause(self, delay=1):
sleep(int(delay))
@lexer
def do_quit(self):
return True
@lexer
def do_EOF(self):
return True
if __name__ == '__main__':
cli()
import sys
eufy = Eufy()
if len(sys.argv) > 1:
eufy.cmdqueue = ' '.join(sys.argv[1:]).split(',') + ['quit']
eufy.cmdloop()