'extract host and port from an URL string'

def from_url(self, url):

    if url.lower().startswith('hwgrep://'):
        url = url[9:]
    n = 0
    test_open = False
    args = url.split('&')
    regexp = args.pop(0)
    for arg in args:
        if ('=' in arg):
            (option, value) = arg.split('=', 1)
        else:
            option = arg
            value = None
        if (option == 'n'):
            n = (int(value) - 1)
            if (n < 1):
                raise ValueError('option    "n"    expects    a    positive    integer    larger    than    1:    {!r}'.format(value))
        elif (option == 'skip_busy'):
            test_open = True
        else:
            raise ValueError('unknown    option:    {!r}'.format(option))
    for (port, desc, hwid) in sorted(serial.tools.list_ports.grep(regexp)):
        if test_open:
            try:
                s = serial.Serial(port)
            except serial.SerialException:
                continue
            else:
                s.close()
        if n:
            n -= 1
            continue
        return port
    else:
        raise serial.SerialException('no    ports    found    matching    regexp    {!r}'.format(url))
