示例#1
0
    def load(self, filename):   
        """Loads the network description from a graph file.
        Note this is done automatically if a filename is given to
        the Internet constructor.

        Args:
           filename:    The file to load from

        Returns:
           None

        Example usage:

        >>> inet = ank.internet.Internet()
        >>> inet.load("simple")
        >>> sorted(inet.network.graph.nodes())
        [RouterB.AS1, RouterA.AS1, RouterD.AS2, RouterC.AS1, RouterA.AS2, RouterA.AS3, RouterB.AS2, RouterC.AS2]

        >>> inet = ank.internet.Internet()
        >>> inet.load("singleas")
        >>> sorted(inet.network.graph.nodes())
        [1a.AS1, 1b.AS1, 1d.AS1, 1c.AS1]

        >>> inet = ank.internet.Internet()
        >>> inet.load("multias")
        >>> sorted(inet.network.graph.nodes())
        [1b.AS1, 1a.AS1, 2d.AS2, 1c.AS1, 2a.AS2, 3a.AS3, 2b.AS2, 2c.AS2]

        """
        LOG.info("Loading")
        ext = os.path.splitext(filename)[1]
        if ext == "":
            #TODO: use try/except block here
            self.network.graph = ank.load_example(filename)

#TODO: allow url to be entered, eg from zoo, if so then download the file and proceed on as normal

        elif ext == ".gml":
            # GML file from Topology Zoo
            ank.load_zoo(self.network, filename)
        elif ext == ".graphml":
            self.network.graph = ank.load_graphml(filename)
        elif ext == ".pickle":
            LOG.warn("AutoNetkit no longer supports pickle file format, please use GraphML")
        elif ext == ".yaml":
            # Legacy ANK file format
            LOG.warn("AutoNetkit no longer supports YAML file format, please use GraphML")
        else:
            LOG.warn("AutoNetkit does not support file format %s" % ext)

        #TODO: check that loaded network has at least one node, if not throw exception
        self.network.instantiate_nodes()
def load_example(filename):
    """
    Load example network
    """
    # No extension, see if filename is an included example Topology
    topology_dir = resource_filename("AutoNetkit", os.path.join("lib", "examples", "topologies"))
    test_filename = os.path.join(topology_dir, "%s.graphml" % filename)
    if os.path.isfile(test_filename):
        LOG.info("Loading example topology %s " % filename)
        return ank.load_graphml(test_filename)
    else:
        example_files = glob.glob(topology_dir + os.sep + "*.graphml")
        # Remove path
        example_files = (os.path.split(filename)[1] for filename in example_files)
        # Remove extension
        example_files = (os.path.splitext(filename)[0] for filename in example_files)
        LOG.warn("Unable to find example topology %s" % filename)
        LOG.info("Valid example topologies are: " + ", ".join(example_files))