<commit_before>import argparse
import contextlib
import logging
import time

from . import ControlUnit

if __name__ == "__main__":

    parser = argparse.ArgumentParser(prog="python -m carreralib.fw")
    parser.add_argument("device", metavar="DEVICE")
    parser.add_argument("file", metavar="FILE", nargs="?", default=None)
    parser.add_argument("-l", "--logfile", default="carreralib.log")
    parser.add_argument("-t", "--timeout", default=1.0, type=float)
    parser.add_argument("-v", "--verbose", action="store_true")
    args = parser.parse_args()

    logging.basicConfig(
        level=logging.DEBUG if args.verbose else logging.INFO,
        filename=args.logfile,
        format="%(asctime)s: %(message)s",
    )

    with contextlib.closing(ControlUnit(args.device, timeout=args.timeout)) as cu:
        print("CU version %s" % cu.version())

        if args.file:
            with open(args.file) as f:
                # remove double quotes wrapping each line
                lines = [line.rstrip().replace('"', "") for line in f.readlines()]
                # skip empty lines
                lines = [line for line in lines if len(line)]
            if lines:
                print("Starting firmware update")
                cu.fwu_start()
                time.sleep(1.0)  # wait a little...
                for n, line in enumerate(lines):
                    # encode to bytes and write to CU
                    cu.fwu_write(line.encode())
                    # print progress
                    print("Writing firmware update block %d/%d" % (n + 1, len(lines)))
                time.sleep(1.0)  # wait some more...
                print("CU version %s" % cu.version())
<commit_msg>Improve firmware option and error handling.<commit_after>import argparse
import contextlib
import logging
import time

from . import ControlUnit

if __name__ == "__main__":

    parser = argparse.ArgumentParser(prog="python -m carreralib.fw")
    parser.add_argument(
        "device",
        metavar="DEVICE",
        help="the Control Unit device, e.g. a serial port or MAC address",
    )
    parser.add_argument(
        "file",
        metavar="FILE",
        nargs="?",
        default=None,
        help="a Control Unit firmware update file",
    )
    parser.add_argument(
        "-f", "--force", action="store_true", help="force firmware update"
    )
    parser.add_argument(
        "-l", "--logfile", default="carreralib.log", help="where to write log messages"
    )
    parser.add_argument(
        "-t",
        "--timeout",
        default=5.0,
        type=float,
        help="maximum time in seconds to wait for Control Unit",
    )
    parser.add_argument(
        "-v", "--verbose", action="store_true", help="write more log messages"
    )
    args = parser.parse_args()

    logging.basicConfig(
        level=logging.DEBUG if args.verbose else logging.INFO,
        filename=args.logfile,
        format="%(asctime)s: %(message)s",
    )

    with contextlib.closing(ControlUnit(args.device, timeout=args.timeout)) as cu:
        if not args.file or not args.force:
            print("CU version %s" % cu.version())

        if args.file:
            with open(args.file) as f:
                # remove double quotes wrapping each line
                lines = [line.rstrip().replace('"', "") for line in f.readlines()]
                # skip empty lines
                lines = [line for line in lines if len(line)]
            if lines:
                print("Starting firmware update")
                cu.fwu_start()
                time.sleep(2.0)  # wait for two seconds...
                for n, line in enumerate(lines):
                    # encode to bytes and write to CU
                    cu.fwu_write(line.encode())
                    # print progress
                    print("Writing firmware update block %d/%d" % (n + 1, len(lines)))
                print("Firmware update done")

{'fraction_non_alphanumeric': 0.12363266344441617, 'fraction_numerical': 0.0030526583566522513, 'mean_word_length': 2.0222905457340508, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '74114', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1080, 'n_words': 302}