netutils/vlan.py
def vlanlist_to_config(vlan_list, first_line_len=48, other_line_len=44, min_grouping_size=3):
    """Given a List of VLANs, build the IOS-like vlan list of configurations.

    Args:
        vlan_list (list): Unsorted list of vlan integers.
        first_line_len (int, optional): The maximum length of the line of the first element of within the return list. Defaults to 48.
        other_line_len (int, optional): The maximum length of the line of all other elements of within the return list. Defaults to 44.
        min_grouping_size (int, optional): The minimum consecutive VLANs to aggregate with a hyphen . Defaults to Cisco's minimum grouping size of 3.

    Returns:
        list: Sorted string list of integers according to IOS-like vlan list rules

    Example:
        >>> from netutils.vlan import vlanlist_to_config
        >>> vlanlist_to_config([1, 2, 3, 5, 6, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018])
        ['1-3,5,6,1000,1002,1004,1006,1008,1010,1012,1014', '1016,1018']
        >>> vlanlist_to_config([1,3,5,6,100,101,102,103,104,105,107,109], min_grouping_size=2)
        ['1,3,5-6,100-105,107,109']
        >>> vlanlist_to_config([1,3,5,6,100,101,102,103,104,105,107,109], min_grouping_size=1)
        ['1,3,5,6,100,101,102,103,104,105,107,109']
    """

    def build_final_vlan_cfg(vlan_cfg):
        if len(vlan_cfg) <= first_line_len:
            return [vlan_cfg]

        # Split VLAN config if lines are too long
        first_line = re.match(f"^.{{0,{first_line_len}}}(?=,)", vlan_cfg)
        vlan_cfg_lines = [first_line.group(0)]
        next_lines = next_lines = re.compile(f"(?<=,).{{0,{other_line_len}}}(?=,|$)")
        for line in next_lines.findall(vlan_cfg, first_line.end()):
            vlan_cfg_lines.append(line)
        return vlan_cfg_lines

    # Fail if min_grouping_size is less than 1.
    if min_grouping_size < 1:
        raise ValueError("Minimum grouping size must be equal to or greater than one.")

    # Sort and de-dup VLAN list
    vlan_list = sorted(set(vlan_list))

    # If grouping size is zero, sort, and return the config list as no other processing is required.
    if min_grouping_size == 1:
        return build_final_vlan_cfg(",".join([str(vlan) for vlan in vlan_list]))

    # Group consecutive VLANs
    vlan_groups = []
    for _, vlan in groupby(enumerate(vlan_list), lambda vlan: vlan[0] - vlan[1]):
        vlan_groups.append(list(map(itemgetter(1), vlan)))

    # Check for invalid VLAN IDs
    if vlan_list[0] < 1 or vlan_list[-1] > 4094:
        raise ValueError("Valid VLAN range is 1-4094")

    # Create VLAN portion of config
    vlan_strings = []
    for group in vlan_groups:
        group_length = len(group)
        group_string = f"{group[0]}"
        # Compress based on grouping_size
        if group_length >= min_grouping_size:
            group_string += f"-{group[-1]}"
        # If it does not match grouping_size, and is greater than one
        elif group_length != 1:
            group_string += f",{group[1]}"
        vlan_strings.append(group_string)

    return build_final_vlan_cfg(",".join(vlan_strings))
Given a List of VLANs, build the IOS-like vlan list of configurations.

    Args:
        vlan_list (list): Unsorted list of vlan integers.
        first_line_len (int, optional): The maximum length of the line of the first element of within the return list. Defaults to 48.
        other_line_len (int, optional): The maximum length of the line of all other elements of within the return list. Defaults to 44.
        min_grouping_size (int, optional): The minimum consecutive VLANs to aggregate with a hyphen . Defaults to Cisco's minimum grouping size of 3.

    Returns:
        list: Sorted string list of integers according to IOS-like vlan list rules

    Example:
        >>> from netutils.vlan import vlanlist_to_config
        >>> vlanlist_to_config([1, 2, 3, 5, 6, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018])
        ['1-3,5,6,1000,1002,1004,1006,1008,1010,1012,1014', '1016,1018']
        >>> vlanlist_to_config([1,3,5,6,100,101,102,103,104,105,107,109], min_grouping_size=2)
        ['1,3,5-6,100-105,107,109']
        >>> vlanlist_to_config([1,3,5,6,100,101,102,103,104,105,107,109], min_grouping_size=1)
        ['1,3,5,6,100,101,102,103,104,105,107,109']
    
Given a List of VLANs, build the IOS-like vlan list of configurations.
Given a List of VLANs build the IOS - like vlan list of configurations .
def vlanlist_to_config(vlan_list, first_line_len=48, other_line_len=44, min_grouping_size=3):
    def build_final_vlan_cfg(vlan_cfg):
        if len(vlan_cfg) <= first_line_len:
            return [vlan_cfg]
        first_line = re.match(f"^.{{0,{first_line_len}}}(?=,)", vlan_cfg)
        vlan_cfg_lines = [first_line.group(0)]
        next_lines = next_lines = re.compile(f"(?<=,).{{0,{other_line_len}}}(?=,|$)")
        for line in next_lines.findall(vlan_cfg, first_line.end()):
            vlan_cfg_lines.append(line)
        return vlan_cfg_lines
    if min_grouping_size < 1:
        raise ValueError("Minimum grouping size must be equal to or greater than one.")
    vlan_list = sorted(set(vlan_list))
    if min_grouping_size == 1:
        return build_final_vlan_cfg(",".join([str(vlan) for vlan in vlan_list]))
    vlan_groups = []
    for _, vlan in groupby(enumerate(vlan_list), lambda vlan: vlan[0] - vlan[1]):
        vlan_groups.append(list(map(itemgetter(1), vlan)))
    if vlan_list[0] < 1 or vlan_list[-1] > 4094:
        raise ValueError("Valid VLAN range is 1-4094")
    vlan_strings = []
    for group in vlan_groups:
        group_length = len(group)
        group_string = f"{group[0]}"
        if group_length >= min_grouping_size:
            group_string += f"-{group[-1]}"
        elif group_length != 1:
            group_string += f",{group[1]}"
        vlan_strings.append(group_string)
    return build_final_vlan_cfg(",".join(vlan_strings))
def vlanlist_to_config ( vlan_list , first_line_len = 48 , other_line_len = 44 , min_grouping_size = 3 ) : def build_final_vlan_cfg ( vlan_cfg ) : if len ( vlan_cfg ) <= first_line_len : return [ vlan_cfg ] first_line = re . match ( f"^.{{0,{first_line_len}}}(?=,)" , vlan_cfg ) vlan_cfg_lines = [ first_line . group ( 0 ) ] next_lines = next_lines = re . compile ( f"(?<=,).{{0,{other_line_len}}}(?=,|$)" ) for line in next_lines . findall ( vlan_cfg , first_line . end ( ) ) : vlan_cfg_lines . append ( line ) return vlan_cfg_lines if min_grouping_size < 1 : raise ValueError ( "Minimum grouping size must be equal to or greater than one." ) vlan_list = sorted ( set ( vlan_list ) ) if min_grouping_size == 1 : return build_final_vlan_cfg ( "," . join ( [ str ( vlan ) for vlan in vlan_list ] ) ) vlan_groups = [ ] for _ , vlan in groupby ( enumerate ( vlan_list ) , lambda vlan : vlan [ 0 ] - vlan [ 1 ] ) : vlan_groups . append ( list ( map ( itemgetter ( 1 ) , vlan ) ) ) if vlan_list [ 0 ] < 1 or vlan_list [ - 1 ] > 4094 : raise ValueError ( "Valid VLAN range is 1-4094" ) vlan_strings = [ ] for group in vlan_groups : group_length = len ( group ) group_string = f"{group[0]}" if group_length >= min_grouping_size : group_string += f"-{group[-1]}" elif group_length != 1 : group_string += f",{group[1]}" vlan_strings . append ( group_string ) return build_final_vlan_cfg ( "," . join ( vlan_strings ) )
Given a List of VLANs, build the IOS-like vlan list of configurations.
Given a List of VLANs build the IOS - like vlan list of configurations .
"""Given a List of VLANs, build the IOS-like vlan list of configurations.

    Args:
        vlan_list (list): Unsorted list of vlan integers.
        first_line_len (int, optional): The maximum length of the line of the first element of within the return list. Defaults to 48.
        other_line_len (int, optional): The maximum length of the line of all other elements of within the return list. Defaults to 44.
        min_grouping_size (int, optional): The minimum consecutive VLANs to aggregate with a hyphen . Defaults to Cisco's minimum grouping size of 3.

    Returns:
        list: Sorted string list of integers according to IOS-like vlan list rules

    Example:
        >>> from netutils.vlan import vlanlist_to_config
        >>> vlanlist_to_config([1, 2, 3, 5, 6, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018])
        ['1-3,5,6,1000,1002,1004,1006,1008,1010,1012,1014', '1016,1018']
        >>> vlanlist_to_config([1,3,5,6,100,101,102,103,104,105,107,109], min_grouping_size=2)
        ['1,3,5-6,100-105,107,109']
        >>> vlanlist_to_config([1,3,5,6,100,101,102,103,104,105,107,109], min_grouping_size=1)
        ['1,3,5,6,100,101,102,103,104,105,107,109']
    """ # Split VLAN config if lines are too long # Fail if min_grouping_size is less than 1. # Sort and de-dup VLAN list # If grouping size is zero, sort, and return the config list as no other processing is required. # Group consecutive VLANs # Check for invalid VLAN IDs # Create VLAN portion of config # Compress based on grouping_size # If it does not match grouping_size, and is greater than one
Sorted string list of integers according to IOS-like vlan list rules
Sorted string list of integers according to IOS - like vlan list rules
list
vlan_list
Unsorted list of vlan integers.
Unsorted list of vlan integers .
first_line_len
The maximum length of the line of the first element of within the return list. Defaults to 48.
The maximum length of the line of the first element of within the return list . Defaults to 48 .
48
other_line_len
The maximum length of the line of all other elements of within the return list. Defaults to 44.
The maximum length of the line of all other elements of within the return list . Defaults to 44 .
44
min_grouping_size
The minimum consecutive VLANs to aggregate with a hyphen . Defaults to Cisco's minimum grouping size of 3.
The minimum consecutive VLANs to aggregate with a hyphen . Defaults to Cisco ' s minimum grouping size of 3 .
Cisco's minimum grouping size of 3
examples
None