Revision 928c0761 mininet/util.py

View differences:

mininet/util.py
400 400
        kwargs.update( params )
401 401
        return cls( *args, **kwargs )
402 402
    return customized
403

  
404
def splitArgs( argstr ):
405
    """Split argument string into usable python arguments
406
       argstr: argument string with format fn,arg2,kw1=arg3...
407
       returns: fn, args, kwargs"""
408
    split = argstr.split( ',' )
409
    fn = split[ 0 ]
410
    params = split[ 1: ]
411
    # Convert int and float args; removes the need for function
412
    # to be flexible with input arg formats.
413
    args = [ makeNumeric( s ) for s in params if '=' not in s ]
414
    kwargs = {}
415
    for s in [ p for p in params if '=' in p ]:
416
        key, val = s.split( '=' )
417
        kwargs[ key ] = makeNumeric( val )
418
    return fn, args, kwargs
419

  
420
def customConstructor( constructors, argStr ):
421
    """Return custom constructor based on argStr
422
    
423
    The args and key/val pairs in argsStr will be automatically applied
424
    when the generated constructor is later used.
425
    """
426
    cname, newargs, kwargs = splitArgs( argStr )
427
    constructor = constructors.get( cname, None )
428

  
429
    if not constructor:
430
        raise Exception( "error: %s is unknown - please specify one of %s" %
431
               ( cname, constructors.keys() ) )
432

  
433
    def customized( name, *args, **params ):
434
        "Customized constructor, useful for Node, Link, and other classes"
435
        params.update( kwargs )
436
        if not newargs:
437
            return constructor( name, *args, **params )
438
        if args:
439
            warn( 'warning: %s replacing %s with %s\n' % (
440
                  constructor, args, newargs ) )
441
        return constructor( name, *newargs, **params )
442

  
443
    return customized
444

  
445
def buildTopo( topos, topoStr ):
446
    """Create topology from string with format (object, arg1, arg2,...).
447
    
448
    input topos is a dict of topo names to constructors, possibly w/args.
449
    """
450
    topo, args, kwargs = splitArgs( topoStr )
451
    if topo not in topos:
452
        raise Exception( 'Invalid topo name %s' % topo )
453
    return topos[ topo ]( *args, **kwargs )

Also available in: Unified diff