Rewrite eepprog.py using the Click framework

This commit is contained in:
Lexi / Zoe 2021-06-11 22:19:09 +02:00
parent f00e52cc4c
commit cedb5bfe81
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
6 changed files with 171 additions and 120 deletions

View file

@ -1,137 +1,64 @@
#!/usr/bin/env python3
import sys
import getopt
import serial
import click
# Global variables for program parameters
verbose = False
serial_device = "/dev/ttyUSB0"
serial_baudrate = 9600
command = ""
# Valid commands
valid_commands = ("test")
from helpers import CliContext, Logger, EepromProgrammer
def usage():
"""Prints help text."""
@click.group()
@click.option('--device', '-d', default='/dev/ttyUSB0', show_default=True,
metavar='DEVICE', help="Set the serial device")
@click.option('--baud', '-b', default=38400, show_default=True,
metavar='BAUDRATE', help="Set the baud rate of the serial device")
@click.option('--verbose', '-v', is_flag=True, help='Print debug output')
@click.pass_context
def eepprog(ctx: click.Context, device: str, baud: int, verbose: bool):
# Create dependencies
logger = Logger(verbose=verbose)
eeprom_programmer = EepromProgrammer(logger=logger, device=device, baudrate=baud)
print(f"""
Usage: {sys.argv[0]} [OPTIONS] COMMAND [ARGS]
logger.debug("Creating CLI context (device: {}, bauds: {})".format(device, baud))
Commands:
test first test command...
# Create CLI context
ctx.obj = CliContext(
logger=logger,
eeprom_programmer=eeprom_programmer,
)
Options:
-h, --help show this help message and exit
-v, --verbose be verbose
-d DEVICE, --device=DEVICE use DEVICE as serial device
(default: /dev/ttyUSB0)
"""[1:-1])
# Define clean up functions
ctx.call_on_close(eeprom_programmer.close)
def parse_args():
"""Parse command line arguments and set global variables."""
@eepprog.command('hello', short_help='Say hello. :)')
@click.pass_obj
def hello(context: CliContext):
"""
Say hello. :)
# Global variables for program options (yes, yes, I know...)
global verbose, serial_device, serial_baudrate, command
try:
# Try to parse argument strings
optlist, args = getopt.gnu_getopt(
sys.argv[1:],
"hvd:",
["help", "verbose", "device="]
)
except getopt.GetoptError as err:
# Invalid option, print help and exit
print(err, "\n")
usage()
sys.exit(2)
# Parse all the options
for opt, val in optlist:
if opt == "-h" or opt == "--help":
# Print help
usage()
sys.exit()
elif opt == "-v" or opt == "--verbose":
# Verbose
verbose = True
elif opt == "-d" or opt == "--device":
# Set device filename
serial_device = val
else:
assert False, "unhandled option"
# Parse command argument
if len(args) == 0:
print("missing command\n")
usage()
sys.exit(2)
else:
# Get command and remove argument
command = args.pop(0)
# Check if command is valid
if command not in valid_commands:
print("invalid command '" + command + "'\n")
usage()
sys.exit(2)
# Check if command has valid arguments
# TODO
Just a test command that tests the logger.
"""
context.logger.debug('hello hello', 413, 'hellooo')
context.logger.info('hello!')
context.logger.info('the same', click.style('but in green!', fg='green'))
context.logger.warn('this is fine')
context.logger.error('ohno')
context.logger.error('error, but a friendly one :)', fg='green')
context.logger.success('yay!')
def setup_serial():
"""Setup serial device."""
@eepprog.command('test', short_help="Send INIT command to the programmer and read answer")
@click.pass_obj
def test(context: CliContext):
"""
Send an 'INIT' command to the programmer and read the answer.
if verbose:
print(f"setting up serial device '{serial_device}' with baudrate "
f"{serial_baudrate}")
Test command for debugging.
"""
context.eeprom_programmer.test()
# Setup serial device
ser = serial.Serial(serial_device, serial_baudrate)
return ser
# TODO command: list-devices -> serial.tools.list_ports
# TODO shell: Run an interactive shell
def command_test(ser):
"""Command 'test': Does some testing."""
if verbose:
print("running command 'test' ...")
# Write a test command
# TODO do a HELLO first
ser.write(b"TESTREAD\n")
# Just read some stuff
while True:
print("read: ", ser.readline(80))
def main():
"""Main function. Does the thing."""
# Parse program arguments
parse_args()
# Setup serial device
ser = setup_serial()
# Run command
if command == "test":
command_test(ser)
else:
assert False, "unhandled command"
# Close serial device
ser.close()
# Run program
main()
if __name__ == '__main__':
eepprog()