from io import StringIO
import csv


def parse_asdcom_stdout(stdout_str):
    """
    The original code in script used by MCU:
    GetMappedUsers()
    {
        $ASDCOM GetMappedUsers | sed '1d' | grep -v "NSS$" 2> /dev/null
    }

    Example result:
    $ /opt/quest/libexec/vas/sugi/asdcom GetMappedUsers
    UPN,ULoginName,UniqueID,NTName,SourceFile
    abaelemer@QASDEV.oi,abaeleloc,73D5FC4F-0B9A-470C-A9B1-7508C2671922,QASDEV\abaelemer,/etc/opt/quest/vas/console_mappings
    abativadar@qasdev.oi,abativloc,B5D09597-4750-4B1F-888F-925BDF43850E,QASDEV\abativadar,/etc/vas-user-map-file
    """

    # Return values
    err = None
    mapped_users = []

    first_row = True

    text_stream = StringIO(stdout_str)
    csv_reader = csv.reader(text_stream)
    for row in csv_reader:
        if first_row:
            first_row = False
            continue
        if row[-1].endswith('NSS'):
            continue
        mapped_users.append([ row[1], row[3] ])

    # Return
    return err, mapped_users