def _write_subnets(self, net, nets): """Create a subnet structure :param net: IP instance of prefix to display :param nets: List of nets (rows) that have subnets. The subnets are located in self.matrix_nets """ large_subnets = [] # When displaying unused addresses, we need to know # about the subnets that span more than one row subnet_matrix = [] # The resulting list of rows to display # Initially, create the rows (subnets) we're going to display if self.show_unused_addresses: row_size = self._get_row_size() subnets = IPtools.create_subnet_range(net, row_size) large_subnets = [ x for x in nets.keys() if x.prefixlen() < row_size ] else: subnets = IPtools.sort_nets_by_address(nets.keys()) while subnets: subnet = subnets.pop(0) matrix_row = [] # contains all cells in the row extra_rows = [] # For large nets matrix_row.append(self._create_index_cell(subnet)) if subnet in self.matrix_nets: # We have data for this subnet, create cells for that data matrix_row.extend(self._create_data_row(subnet)) else: # Either this subnet is bigger then a row or no subnet # exists here - we need to find out if self.show_unused_addresses: # Find out if this subnet is part of a bigger subnet that # should be displayed here index = self._find_large_net(subnet, large_subnets) if index is not None: num_extra_rows = self._add_large_subnet( large_subnets.pop(index), matrix_row) extra_rows = self._get_extra_rows( num_extra_rows, subnets) else: matrix_row.append(self._create_empty_cell()) else: # This net spans more then one row num_extra_rows = self._add_large_subnet(subnet, matrix_row) extra_rows = self._get_extra_rows(num_extra_rows, subnet) subnet_matrix.append(matrix_row) # These rows needs to be added after the main row is created for # nets that span more then one row subnet_matrix.extend(extra_rows) return subnet_matrix
def _create_extra_rows(self, num_extra_rows, subnet): extra_nets = [] row_net = IPy.IP('{}/{}'.format(subnet.net(), self._get_row_size())) for _ in range(num_extra_rows): row_net = IPtools.get_next_subnet(row_net) extra_nets.append([self._create_index_cell(row_net, link=False)]) return extra_nets
def build(self): nets = IPtools.sort_nets_by_address(self.tree_nets.keys()) self.nodes = [ self.Node(net, self._write_subnets(self.tree_nets[net], 1)) for net in nets ]
def __init__(self, start_net, end_net=None, bits_in_matrix=3): if end_net is None: end_net = IPtools.getLastSubnet(start_net) self.start_net = start_net self.end_net = end_net self.bits_in_matrix = bits_in_matrix self.tree = IPtree.build_tree(start_net, end_net, bits_in_matrix=bits_in_matrix, add_missing_nets=True) self.tree_nets = self.extract_tree_nets() self.matrix_nets = self.extract_matrix_nets() self.heading_colspan = 1 self.nodes = None self.num_columns = None self.column_headings = None
def _create_data_row(self, subnet): """Create a data row containing a list of cells :rtype: list[Cell] """ if self.has_too_small_nets(subnet): return [self._create_too_small_subnets_cell()] elif self.matrix_nets[subnet]: # this subnet is divided into parts host_nybbles_map = IPtools.getLastbitsIpMap( self.matrix_nets[subnet].keys()) return self._add_child_nets(host_nybbles_map) else: # this subnet spans the whole row meta = metaIP.MetaIP(subnet) return [self._create_cell(subnet, meta)]
def _write_subnets(self, net): nodes = IPtools.sort_nets_by_address(net.keys()) subnet_matrix = [] # The resulting list of rows to display for subnet in nodes: matrix_row = [] # contains all cells in the row extra_rows = [] # For large nets matrix_row.append(self._create_index_cell(subnet)) if subnet in self.matrix_nets: # We have data for this subnet, create cells for that data matrix_row.extend(self._create_data_row(subnet)) else: # subnet is larger than row size num_extra_rows = self._add_large_subnet(subnet, matrix_row) extra_rows = self._create_extra_rows(num_extra_rows, subnet) subnet_matrix.append(matrix_row) subnet_matrix.extend(extra_rows) return subnet_matrix
def _write_subnets(self, net, depth): nodes = IPtools.sort_nets_by_address(net.keys()) lastnet = None subnet_matrix = [] for subnet in nodes: if lastnet is None: lastnet = subnet if subnet in self.matrix_nets: if self.show_unused_addresses: subnet_matrix.extend( self._nets_in_range( lastnet, subnet, depth)) lastnet = subnet matrix_row = [ self.Cell( colspan=1, color=None, content='{0} {1}'.format( Matrix.print_depth(depth), _netlink(subnet))) ] host_nybbles_map = IPtools.getLastbitsIpMap( self.matrix_nets[subnet].keys()) next_header_idx = -1 if self.has_too_small_nets(subnet): matrix_row.append( self.Cell( colspan=self.num_columns, color=self._load_color(0, 'large'), content='Too many small nets') ) elif host_nybbles_map is None: # i.e. there exist a net with no # subnets <==> net spans whole row ip = IPy.IP(subnet) meta = metaIP.MetaIP(ip) matrix_row.append( self.Cell( colspan=self.num_columns, color=self._load_color( meta.usage_percent, meta.nettype), content=_matrixlink(0, ip)) ) else: # The net exists and have subnets for i in self.column_headings: if self.column_headings.index(i) < next_header_idx: continue key = i.lower() if key in host_nybbles_map: ip = host_nybbles_map[key] meta = metaIP.MetaIP(ip) matrix_cell = self.Cell( colspan=self._colspan(ip), color=self._load_color( meta.usage_percent, meta.nettype), content=_matrixlink(key, ip)) next_header_idx = (self.column_headings.index(i) + int(self._colspan(ip))) else: matrix_cell = self.Cell( colspan=1, color=None, content=' ') matrix_row.append(matrix_cell) subnet_matrix.append(matrix_row) else: if (self.matrix_nets and lastnet.prefixlen() < self.matrix_nets.keys()[0].prefixlen()): mnets = self.generate_matrix_nets(lastnet) subnet_extended = IPy.IP( '/'.join([ subnet.net().strNormal(), str(self.matrix_nets.keys()[0].prefixlen()) ])) subnet_matrix.extend( self._nets_in_range( mnets[-1], subnet_extended, depth)) lastnet = subnet meta = metaIP.MetaIP(subnet) matrix_row = [ self.Cell( colspan=1, color=None, content='{0} {1}'.format( Matrix.print_depth(depth), _netlink(subnet, True))), self.Cell( colspan=self.num_columns, color=self._load_color( meta.usage_percent, meta.nettype), content=_supernet_matrixlink(subnet)) ] subnet_matrix.append(matrix_row) subnet_matrix.extend( self._write_subnets(net[subnet], depth + 1)) subnet_matrix.extend( self._remaining_blank_nets(subnet, depth + 1)) return subnet_matrix
def _write_subnets(self, net, depth): nodes = IPtools.sort_nets_by_address(net.keys()) lastnet = None subnet_matrix = [] for subnet in nodes: if lastnet is None: lastnet = subnet if subnet in self.matrix_nets: if IPtools.isIntermediateNets(lastnet, subnet): subnet_matrix.append(None) lastnet = subnet matrix_row = [ self.Cell( colspan=1, color=None, content='{0}{1}'.format( Matrix.print_depth(depth), _netlink(subnet))) ] host_nybbles_map = IPtools.getLastbitsIpMap( self.matrix_nets[subnet].keys()) next_header_idx = -1 for i in self.column_headings: if self.column_headings.index(i) < next_header_idx: continue key = i.lower() if key in host_nybbles_map: meta = metaIP.MetaIP(host_nybbles_map[key]) ip = host_nybbles_map[key] matrix_cell = self.Cell( colspan=self._colspan(ip), color=meta.ipv6_color, content=_matrixlink(key, ip)) next_header_idx = self.column_headings.index( i) + int(self._colspan(ip)) else: matrix_cell = self.Cell( colspan=1, color=None, content=' ') matrix_row.append(matrix_cell) subnet_matrix.append(matrix_row) else: subnet_matrix.append(None) lastnet = subnet matrix_row = [ self.Cell( colspan=1, color=None, content='{0}{1}'.format( Matrix.print_depth(depth), _netlink(subnet, True))), self.Cell( colspan=self.num_columns, color=None, content=' ') ] subnet_matrix.append(matrix_row) subnet_matrix.extend( self._write_subnets(net[subnet], depth + 1)) return subnet_matrix
def _write_subnets(self, net, depth): nodes = IPtools.sort_nets_by_address(net.keys()) lastnet = None subnet_matrix = [] for subnet in nodes: if lastnet is None: lastnet = subnet if subnet in self.matrix_nets: if self.show_unused_addresses: subnet_matrix.extend( self._nets_in_range(lastnet, subnet, depth)) lastnet = subnet matrix_row = [ self.Cell(colspan=1, color=None, content='{0} {1}'.format( Matrix.print_depth(depth), _netlink(subnet))) ] host_nybbles_map = IPtools.getLastbitsIpMap( self.matrix_nets[subnet].keys()) next_header_idx = -1 if self.has_too_small_nets(subnet): matrix_row.append( self.Cell(colspan=self.num_columns, color=self._load_color(0, 'large'), content='Too many small nets')) elif host_nybbles_map is None: # i.e. there exist a net with no # subnets <==> net spans whole row ip = IPy.IP(subnet) meta = metaIP.MetaIP(ip) matrix_row.append( self.Cell(colspan=self.num_columns, color=self._load_color( meta.usage_percent, meta.nettype), content=_matrixlink(0, ip))) else: # The net exists and have subnets for i in self.column_headings: if self.column_headings.index(i) < next_header_idx: continue key = i.lower() if key in host_nybbles_map: ip = host_nybbles_map[key] meta = metaIP.MetaIP(ip) matrix_cell = self.Cell( colspan=self._colspan(ip), color=self._load_color(meta.usage_percent, meta.nettype), content=_matrixlink(key, ip)) next_header_idx = (self.column_headings.index(i) + int(self._colspan(ip))) else: matrix_cell = self.Cell(colspan=1, color=None, content=' ') matrix_row.append(matrix_cell) subnet_matrix.append(matrix_row) else: if (self.matrix_nets and lastnet.prefixlen() < self.matrix_nets.keys()[0].prefixlen()): mnets = self.generate_matrix_nets(lastnet) subnet_extended = IPy.IP('/'.join([ subnet.net().strNormal(), str(self.matrix_nets.keys()[0].prefixlen()) ])) subnet_matrix.extend( self._nets_in_range(mnets[-1], subnet_extended, depth)) lastnet = subnet meta = metaIP.MetaIP(subnet) matrix_row = [ self.Cell(colspan=1, color=None, content='{0} {1}'.format( Matrix.print_depth(depth), _netlink(subnet, True))), self.Cell(colspan=self.num_columns, color=self._load_color(meta.usage_percent, meta.nettype), content=_supernet_matrixlink(subnet)) ] subnet_matrix.append(matrix_row) subnet_matrix.extend( self._write_subnets(net[subnet], depth + 1)) subnet_matrix.extend( self._remaining_blank_nets(subnet, depth + 1)) return subnet_matrix