'Save command.'

def saveTopology(self):

    myFormats = [('Mininet    Topology', '*.mn'), ('All    Files', '*')]
    savingDictionary = {}
    fileName = tkFileDialog.asksaveasfilename(filetypes=myFormats, title='Save    the    topology    as...')
    if (len(fileName) > 0):
        savingDictionary['version'] = '2'
        hostsToSave = []
        switchesToSave = []
        controllersToSave = []
        for widget in self.widgetToItem:
            name = widget['text']
            tags = self.canvas.gettags(self.widgetToItem[widget])
            (x1, y1) = self.canvas.coords(self.widgetToItem[widget])
            if (('Switch' in tags) or ('LegacySwitch' in tags) or ('LegacyRouter' in tags)):
                nodeNum = self.switchOpts[name]['nodeNum']
                nodeToSave = {'number': str(nodeNum), 'x': str(x1), 'y': str(y1), 'opts': self.switchOpts[name]}
                switchesToSave.append(nodeToSave)
            elif ('Host' in tags):
                nodeNum = self.hostOpts[name]['nodeNum']
                nodeToSave = {'number': str(nodeNum), 'x': str(x1), 'y': str(y1), 'opts': self.hostOpts[name]}
                hostsToSave.append(nodeToSave)
            elif ('Controller' in tags):
                nodeToSave = {'x': str(x1), 'y': str(y1), 'opts': self.controllers[name]}
                controllersToSave.append(nodeToSave)
            else:
                raise Exception(('Cannot    create    mystery    node:    ' + name))
        savingDictionary['hosts'] = hostsToSave
        savingDictionary['switches'] = switchesToSave
        savingDictionary['controllers'] = controllersToSave
        linksToSave = []
        for link in self.links.values():
            src = link['src']
            dst = link['dest']
            linkopts = link['linkOpts']
            (srcName, dstName) = (src['text'], dst['text'])
            linkToSave = {'src': srcName, 'dest': dstName, 'opts': linkopts}
            if (link['type'] == 'data'):
                linksToSave.append(linkToSave)
        savingDictionary['links'] = linksToSave
        savingDictionary['application'] = self.appPrefs
        try:
            f = open(fileName, 'wb')
            f.write(json.dumps(savingDictionary, sort_keys=True, indent=4, separators=(',', ':    ')))
        except Exception as er:
            warn(er, '\n')
        finally:
            f.close()
