fdutils/files.py
def cmd_output_split_parser(out, columns=(), starts_at=0, stops_at=0, split_re=r'\s+', column_info_at=-1,
                            start_after_columns_line=False):
    """  Function to parse the output from a command in a shell

    :param out: list with the values separated by split_re regex
    :param columns: list of column names
    :param starts_at: line where to start reading from
    :param stops_at: line where to stop reading at. This will usually be zero to mean read all or negative to read until the last N line
    :param split_re: the regex expression to use to split the values
    :param column_info_at: if we want to retrieve the information about the columns from a line instead of from the columns argument
    :param start_after_columns_line: this will try to find the start where the columns line is by finding a line with the columns values given on the argument columns
    :return: dictionary with the key being the first column and the value being either a dictionary with the keys being the columns from 1
    """
    def add_columns(key, values):
        if not key in ret:
            ret[key] = dict()
        for i, c in enumerate(columns[1:]):
            if i > len(values):
                return
            ret[key][c] = values[i]

    continuation = re.compile(r'^\s{4,}')
    ret = collections.OrderedDict()
    last = ''
    split_number = len(columns) - 1
    stop = len(out) if not stops_at else stops_at

    if column_info_at != -1:
        columns = re.split(split_re, out[column_info_at])
    elif start_after_columns_line:
        column_i = [v.lower() for v in columns]
        found = False
        for starts_at, line in enumerate(out):
            if column_i == [v.lower() for v in re.split(split_re, line, split_number)]:
                found = True
                starts_at += 1
                break
        if not found:
            raise Exception('Header Columns not found')

    if len(out) > starts_at:
        for v in out[starts_at:stop]:
            # check if line is empty:
            if not v.strip():
                continue
            # check if line is continuation:
            if continuation.match(v):
                add_columns(last, re.split(split_re, v, split_number))
                continue
            a = re.split(split_re, v, split_number)
            last = a[0]
            if len(a) > split_number:
                add_columns(last, a[1:])
            elif len(a) == 1:
                ret[a[0]] = dict()
    # as the prompt changes with the cd we need a way to remove the prompt element added that was not removed during the expect processing
    return ret
  Function to parse the output from a command in a shell

    :param out: list with the values separated by split_re regex
    :param columns: list of column names
    :param starts_at: line where to start reading from
    :param stops_at: line where to stop reading at. This will usually be zero to mean read all or negative to read until the last N line
    :param split_re: the regex expression to use to split the values
    :param column_info_at: if we want to retrieve the information about the columns from a line instead of from the columns argument
    :param start_after_columns_line: this will try to find the start where the columns line is by finding a line with the columns values given on the argument columns
    :return: dictionary with the key being the first column and the value being either a dictionary with the keys being the columns from 1
    
Function to parse the output from a command in a shell
Function to parse the output from a command in a shell
def cmd_output_split_parser(out, columns=(), starts_at=0, stops_at=0, split_re=r'\s+', column_info_at=-1,
                            start_after_columns_line=False):
    def add_columns(key, values):
        if not key in ret:
            ret[key] = dict()
        for i, c in enumerate(columns[1:]):
            if i > len(values):
                return
            ret[key][c] = values[i]
    continuation = re.compile(r'^\s{4,}')
    ret = collections.OrderedDict()
    last = ''
    split_number = len(columns) - 1
    stop = len(out) if not stops_at else stops_at
    if column_info_at != -1:
        columns = re.split(split_re, out[column_info_at])
    elif start_after_columns_line:
        column_i = [v.lower() for v in columns]
        found = False
        for starts_at, line in enumerate(out):
            if column_i == [v.lower() for v in re.split(split_re, line, split_number)]:
                found = True
                starts_at += 1
                break
        if not found:
            raise Exception('Header Columns not found')
    if len(out) > starts_at:
        for v in out[starts_at:stop]:
            if not v.strip():
                continue
            if continuation.match(v):
                add_columns(last, re.split(split_re, v, split_number))
                continue
            a = re.split(split_re, v, split_number)
            last = a[0]
            if len(a) > split_number:
                add_columns(last, a[1:])
            elif len(a) == 1:
                ret[a[0]] = dict()
    return ret
def cmd_output_split_parser ( out , columns = ( ) , starts_at = 0 , stops_at = 0 , split_re = r'\s+' , column_info_at = - 1 , start_after_columns_line = False ) : def add_columns ( key , values ) : if not key in ret : ret [ key ] = dict ( ) for i , c in enumerate ( columns [ 1 : ] ) : if i > len ( values ) : return ret [ key ] [ c ] = values [ i ] continuation = re . compile ( r'^\s{4,}' ) ret = collections . OrderedDict ( ) last = '' split_number = len ( columns ) - 1 stop = len ( out ) if not stops_at else stops_at if column_info_at != - 1 : columns = re . split ( split_re , out [ column_info_at ] ) elif start_after_columns_line : column_i = [ v . lower ( ) for v in columns ] found = False for starts_at , line in enumerate ( out ) : if column_i == [ v . lower ( ) for v in re . split ( split_re , line , split_number ) ] : found = True starts_at += 1 break if not found : raise Exception ( 'Header Columns not found' ) if len ( out ) > starts_at : for v in out [ starts_at : stop ] : if not v . strip ( ) : continue if continuation . match ( v ) : add_columns ( last , re . split ( split_re , v , split_number ) ) continue a = re . split ( split_re , v , split_number ) last = a [ 0 ] if len ( a ) > split_number : add_columns ( last , a [ 1 : ] ) elif len ( a ) == 1 : ret [ a [ 0 ] ] = dict ( ) return ret
Function to parse the output from a command in a shell
Function to parse the output from a command in a shell
"""  Function to parse the output from a command in a shell

    :param out: list with the values separated by split_re regex
    :param columns: list of column names
    :param starts_at: line where to start reading from
    :param stops_at: line where to stop reading at. This will usually be zero to mean read all or negative to read until the last N line
    :param split_re: the regex expression to use to split the values
    :param column_info_at: if we want to retrieve the information about the columns from a line instead of from the columns argument
    :param start_after_columns_line: this will try to find the start where the columns line is by finding a line with the columns values given on the argument columns
    :return: dictionary with the key being the first column and the value being either a dictionary with the keys being the columns from 1
    """ # check if line is empty: # check if line is continuation: # as the prompt changes with the cd we need a way to remove the prompt element added that was not removed during the expect processing
dictionary with the key being the first column and the value being either a dictionary with the keys being the columns from 1
dictionary with the key being the first column and the value being either a dictionary with the keys being the columns from 1
out
list with the values separated by split_re regex
list with the values separated by split_re regex
columns
list of column names
list of column names
starts_at
line where to start reading from
line where to start reading from
stops_at
line where to stop reading at. This will usually be zero to mean read all or negative to read until the last N line
line where to stop reading at . This will usually be zero to mean read all or negative to read until the last N line
split_re
the regex expression to use to split the values
the regex expression to use to split the values
column_info_at
if we want to retrieve the information about the columns from a line instead of from the columns argument
if we want to retrieve the information about the columns from a line instead of from the columns argument
start_after_columns_line
this will try to find the start where the columns line is by finding a line with the columns values given on the argument columns
this will try to find the start where the columns line is by finding a line with the columns values given on the argument columns