Skip to content

Commit

Permalink
Adding checkUserInput() per FaradayRF#30 to start validating user input
Browse files Browse the repository at this point in the history
* Added checkUserInput()
* Checked callsign
* Checked ID
* Checked IP Address
  • Loading branch information
kb1lqc committed Mar 11, 2018
1 parent a83086d commit 12334dc
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions faradayio_cli/faradayio_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import threading
import time
import pytun
import ipaddress

from faradayio.faraday import Monitor
from faradayio.faraday import SerialTestClass
Expand Down Expand Up @@ -47,6 +48,36 @@ def setupArgparse():
# Parse and return arguments
return parser.parse_args()

def checkUserInput(args):
"""Checks user input for validity
Args:
args: argparse arguments
"""
# Check callsign
# Expect a string
if not isinstance(args.callsign, str):
raise TypeError
# Callsigns are at most seven characters long
if len(args.callsign) > 7:
raise ValueError

# Check ID
# Expect and integer
if not isinstance(args.id, int):
raise TypeError
# Expect a value between 0-255
if args.id < 0 or args.id > 255:
raise ValueError

# Check IP Address
# Expect a string
if not isinstance(args.addr, str):
raise TypeError
# Expect an IP address that is valid
ipaddress.IPv4Address(args.addr)


def setupSerialPort(loopback, port, baud, readtimeout, writetimeout):
"""Sets up serial port by connecting to phsyical or software port.
Expand Down Expand Up @@ -95,6 +126,8 @@ def main():
except argparse.ArgumentError as error:
raise SystemExit(error)

checkUserInput(args)

# Setup serial port
try:
serialPort = setupSerialPort(loopback=args.loopback,
Expand Down

0 comments on commit 12334dc

Please sign in to comment.