def __init__(self): """ Initialize a topology definition """ self._filename = None self._yaml = None self._global_machine = None self._networks = [] self._machines = [] self._state = State(self)
class TopologyRoot(object): """ Topology definition """ def __init__(self): """ Initialize a topology definition """ self._filename = None self._yaml = None self._global_machine = None self._networks = [] self._machines = [] self._state = State(self) def load(self, filename): """ Load the topology from a YAML file """ self._filename = filename if self._state.persisted: self._state.load() else: logger.info('no previous state found for this topology') logger.info('topology: loading from "%s"...', self._filename) with open(self._filename) as infile: content = infile.read() self.load_str(content) def load_str(self, string): """ Load the topology from a YAML string """ try: y = pyaml.yaml.load(string) except yaml.scanner.ScannerError, e: raise TopologyException('malformed topology file: %s' % str(e)) try: self._yaml = y[YAML_ROOT] except KeyError, e: raise TopologyException('topology definition error: "%s" key not found' % str(e))