Beispiel #1
0
def grep(regexp):
    """\
    Search for ports using a regular expression. Port name, description and
    hardware ID are searched. The function returns an iterable that returns the
    same tuples as comport() would do.
    """
    r = re.compile(regexp, re.I)
    for info in comports():
        port, desc, hwid = info
        if r.search(port) or r.search(desc) or r.search(hwid):
            yield info
Beispiel #2
0
def main():
    import argparse

    parser = argparse.ArgumentParser(description="Serial port enumeration")

    parser.add_argument("regexp", nargs="?", help="only show ports that match this regex")

    parser.add_argument("-v", "--verbose", action="store_true", help="show more messages")

    parser.add_argument("-q", "--quiet", action="store_true", help="suppress all messages")

    parser.add_argument("-n", type=int, help="only output the N-th entry")

    args = parser.parse_args()

    hits = 0
    # get iteraror w/ or w/o filter
    if args.regexp:
        if not args.quiet:
            sys.stderr.write("Filtered list with regexp: %r\n" % (args.regexp,))
        iterator = sorted(grep(args.regexp))
    else:
        iterator = sorted(comports())
    # list them
    for n, (port, desc, hwid) in enumerate(iterator, 1):
        if args.n is None or args.n == n:
            sys.stdout.write("{:20}\n".format(port))
            if args.verbose:
                sys.stdout.write("    desc: {}\n".format(desc))
                sys.stdout.write("    hwid: {}\n".format(hwid))
        hits += 1
    if not args.quiet:
        if hits:
            sys.stderr.write("{} ports found\n".format(hits))
        else:
            sys.stderr.write("no ports found\n")