Esempio n. 1
0
def set_config_from_strings(name, args):
    ''' Sets config from an array of arguments '''
    if not name in config_switches:
        raise UserError("I don't know config switch '%s'" % name)
    
    switch = config_switches[name]
    try:
        value = interpret_strings_like(args, switch.default_value)
    except ValueError as e:
        raise UserError(e)
    
    # TODO: broadcast change?
    compmake_config.__dict__[name] = value
Esempio n. 2
0
def interpret_commands(commands):
    if isinstance(commands, str):
        commands = commands.split()
    
    ui_commands = get_commands() 
    
    command_name = commands[0]
    # Check if this is an alias
    if command_name in alias2name:
        command_name = alias2name[command_name]
    if not command_name in ui_commands.keys():
        raise UserError("Unknown command %r (try 'help'). " % command_name)
        
    # XXX: use more elegant method
    cmd = ui_commands[command_name]
    function = cmd.function 
    function_args = \
        function.func_code.co_varnames[:function.func_code.co_argcount]
    
    args = commands[1:]
    
    # look for  key=value pairs 
    other = []
    kwargs = {}
    argspec = inspect.getargspec(function)

    if argspec.defaults:
        num_args_with_default = len(argspec.defaults)
    else:
        num_args_with_default = 0
    num_args = len(argspec.args)
    num_args_without_default = num_args - num_args_with_default

    args_without_default = argspec.args[0:num_args_without_default]

    for a in args:
        if a.find('=') > 0:
            k, v = a.split('=')
        
            if not k in argspec.args:
                msg = ("You passed the argument %r for command %r, but the only "
                       "available arguments are %s." % (k, cmd.name, function_args))
                raise UserError(msg)
            # look if we have a default value
            index = argspec.args.index(k)
            if index < num_args_without_default:
                # no default, pass as string
                kwargs[k] = v
            else:
                default_value = \
                    argspec.defaults[index - num_args_without_default]
                try:
                    kwargs[k] = interpret_strings_like(v, default_value)
                except ValueError:
                    msg = 'Could not parse %s=%s as %s.' % (k, v, type(default_value))
                    raise UserError(msg)
                
                #print "%s :  %s (%s)" % (k, kwargs[k], type(kwargs[k]))
                
        else:
            other.append(a)
    args = other
    
    if 'args' in function_args:
        kwargs['args'] = args

    if 'non_empty_job_list' in function_args:
        if not args:
            msg = ("The command %r requires a non empty list of jobs as "
                   "argument." % command_name) 
            raise UserError(msg)

            
        job_list = parse_job_list(args) 
        
        # TODO: check non empty
        
        kwargs['non_empty_job_list'] = job_list
        
    if 'job_list' in function_args:
        kwargs['job_list'] = parse_job_list(args)
         
    for x in args_without_default:
        if not x in kwargs:
            raise UserError('Required argument %r not given.' % x)
    
    return function(**kwargs)