Revision 9281719d

View differences:

mininet/cli.py
43 43

  
44 44
    def __init__( self, mininet, stdin=sys.stdin, script=None ):
45 45
        self.mn = mininet
46
        # CLI locals for py commands
47
        self.locals = { 'net': mininet }
46 48
        # Attempt to handle input
47 49
        self.stdin = stdin
48 50
        self.inPoller = poll()
......
71 73
        "Don't repeat last command when you hit return."
72 74
        pass
73 75

  
74
    def locals( self ):
76
    def getLocals( self ):
75 77
        "Local variable bindings for py command"
76
        locals = { 'net': self.mn }
77
        locals.update( self.mn )
78
        return locals
78
        self.locals.update( self.mn )
79
        return self.locals
79 80

  
80 81
    # Disable pylint "Unused argument: 'arg's'" messages, as well as
81 82
    # "method could be a function" warning, since each CLI function
......
109 110

  
110 111
    def do_nodes( self, _line ):
111 112
        "List all nodes."
112
        #                                                 self.mn.values()
113
        nodes = ' '.join( [ node.name for node in sorted( self.mn ) ] )
113
        nodes = ' '.join( sorted( self.mn ) )
114 114
        output( 'available nodes are: \n%s\n' % nodes )
115 115

  
116 116
    def do_net( self, _line ):
117 117
        "List network connections."
118
        #                    self.mn.values()
119
        dumpNodeConnections( self.mn )
118
        dumpNodeConnections( self.mn.values() )
120 119

  
121 120
    def do_sh( self, line ):
122 121
        "Run an external shell command"
......
129 128
        """Evaluate a Python expression.
130 129
           Node names may be used, e.g.: py h1.cmd('ls')"""
131 130
        try:
132
            result = eval( line, globals(), self.locals() )
131
            result = eval( line, globals(), self.getLocals() )
133 132
            if not result:
134 133
                return
135 134
            elif isinstance( result, str ):
......
146 145
        """Execute a Python statement.
147 146
            Node names may be used, e.g.: px print h1.cmd('ls')"""
148 147
        try:
149
            exec( line, globals(), self.locals() )
148
            exec( line, globals(), self.getLocals() )
150 149
        except Exception, e:
151 150
            output( str( e ) + '\n' )
152 151

  
......
177 176
            hosts = []
178 177
            err = False
179 178
            for arg in args:
180
                #             self.mn.keys()
181 179
                if arg not in self.mn:
182 180
                    err = True
183 181
                    error( "node '%s' not in network\n" % arg )
......
211 209

  
212 210
    def do_intfs( self, _line ):
213 211
        "List interfaces."
214
        #           self.mn.values()
215
        for node in self.mn:
212
        for node in self.mn.values():
216 213
            output( '%s: %s\n' %
217 214
                    ( node.name, ','.join( node.intfNames() ) ) )
218 215

  
219 216
    def do_dump( self, _line ):
220 217
        "Dump node info."
221
        #           self.mn.values()
222
        for node in self.mn:
218
        for node in self.mn.values():
223 219
            output( '%s\n' % repr( node ) )
224 220

  
225 221
    def do_link( self, line ):
......
239 235
            error( 'usage: %s node1 node2 ...\n' % term )
240 236
        else:
241 237
            for arg in args:
242
                #             self.mn.keys()
243 238
                if arg not in self.mn:
244 239
                    error( "node '%s' not in network\n" % arg )
245 240
                else:
mininet/net.py
237 237
    def __iter__( self ):
238 238
        "return iterator over nodes"
239 239
        #or dow we want to iterate of the keys i.e. node.name like a dict
240
        return chain( self.hosts, self.switches, self.controllers )
240
        for node in chain( self.hosts, self.switches, self.controllers ):
241
            yield node.name
241 242

  
242 243
    def __len__( self ):
244
        "returns number of nodes in net"
243 245
        return len( self.hosts ) + len( self.switches ) + len( self.controllers )
244 246

  
245 247
    def __contains__( self, item ):
248
        "returns True if net contains named node"
246 249
        return item in self.keys()
247 250

  
248 251
    def keys( self ):
249
        return [ node.name for node in self.__iter__() ]
252
        "return a list of all node names or net's keys"
253
        return list( self.__iter__() )
250 254

  
251 255
    def values( self ):
252
        return list( self.__iter__() )
256
        "return a list of all nodes or net's values"
257
        return [ self[name] for name in self.__iter__() ]
253 258

  
254 259
    def items( self ):
260
        "return (key,value) tuple list for every node in net"
255 261
        return zip( self.keys(), self.values() )
256 262

  
257 263
    def addLink( self, node1, node2, port1=None, port2=None,

Also available in: Unified diff