Parse the initial line of a device array stanza in
    ``/proc/mdstat``.

    Lines are expected to be like:

        md2 : active raid1 sdb3[1] sda3[0]

    If they do not have this format, an error will be raised since it
    would be considered an unexpected parsing error.

    Parameters
    ----------

    md_line : str
        A single line from the start of a device array stanza from a
        ``/proc/mdstat`` file.

    Returns
    -------
        A list of dictionaries, one dictionrary for each component
        device making up the array.

def parse_array_start(md_line):
    """Parse the initial line of a device array stanza in
    ``/proc/mdstat``.

    Lines are expected to be like:

        md2 : active raid1 sdb3[1] sda3[0]

    If they do not have this format, an error will be raised since it
    would be considered an unexpected parsing error.

    Parameters
    ----------

    md_line : str
        A single line from the start of a device array stanza from a
        ``/proc/mdstat`` file.

    Returns
    -------
        A list of dictionaries, one dictionrary for each component
        device making up the array.
    """
    components = []
    tokens = md_line.split()
    device_name = tokens.pop(0)
    assert device_name.startswith("md")
    assert tokens.pop(0) == ":"

    active_string = tokens.pop(0)
    active = False
    if active_string == "active":
        active = True
    else:
        assert active_string == "inactive"

    raid_read_only_token = tokens.pop(0)
    auto_read_only = False
    raid = None
    if raid_read_only_token == "(auto-read-only)":
        auto_read_only = True
        raid = tokens.pop(0)
    elif active:
        raid = raid_read_only_token
    else:  # Inactive devices don't list the raid type
        raid = None
        tokens.insert(0, raid_read_only_token)

    for token in tokens:
        subtokens = re.split(r"\[|]", token)
        assert len(subtokens) > 1
        comp_name = subtokens[0]
        assert comp_name

        role = int(subtokens[1])

        device_flag = None
        if len(subtokens) > 2:
            device_flag = subtokens[2]
            if device_flag:
                device_flag = device_flag.strip('()')

        component_row = {"device_name": device_name,
                         "raid": raid,
                         "active": active,
                         "auto_read_only": auto_read_only,
                         "component_name": comp_name,
                         "role": role,
                         "device_flag": device_flag}
        components.append(component_row)
    return components