Esempio n. 1
0
def call_template(frag, params):
    if not 'template' in params:
        raise Exception(x_not_found('field', 'template', params))
    
    template_name = params['template']
    if template_name is None:
        return
    del params['template']
    
    if not template_name in AllTemplates:
        raise Exception(x_not_found('template', template_name, AllTemplates))
    
    t = AllTemplates[template_name]
    function = t.function
    necessary = t.necessary
    optional = t.optional
    
    for nec in necessary: 
        if not nec in params:
            msg = 'Required template param %r not found ' % nec
            msg += 'while calling %r with %r' % (template_name, params)
            raise Exception(msg)
        
    final = {}
    for p in params:
        if not p in (necessary + optional):
            msg = 'Extra param %r for template %r' % (p, template_name) 
            logger.warning(msg)
        else:
            final[p] = params[p]
    
    final['frag'] = frag
    try:
        return function(**final)
    except:
        logger.error('Error while considering cell %r' % params)
        logger.error('Template: %r' % str(t))  # @UndefinedVariable
        logger.error('Error while calling function %r with params %r' %  # @UndefinedVariable
                     (function, final))
        raise
def load_agent_state(data_central, id_agent, id_robot,
                     reset_state=False,
                     raise_if_no_state=False):
    ''' 
        Load the agent, loading the agent state from the state_db directory.
        If the state is not available, then it initializes anew. 
        
        Returns a tuple (agent, state).
    '''
    logger.info('Loading state %s/%s reset=%s ' % (id_agent,
                                                   id_robot, reset_state))
    config = data_central.get_bo_config()
    agent = config.agents.instance(id_agent)  # @UndefinedVariable

    index = data_central.get_log_index()
    has_log = index.has_streams_for_robot(id_robot)
    has_instance = id_robot in config.robots 
    
    if has_log:
        boot_spec = index.get_robot_spec(id_robot)
    elif has_instance:
        robot = config.robots.instance(id_robot)
        boot_spec = robot.get_spec()
    else:
        msg = 'Cannot load agent state for %r ' % id_agent
        msg += 'because I cannot find any streams for robot %r ' % id_robot
        msg += 'and I need them to find the BootSpec. '
        msg += 'Also I could not instance the robot.'
        msg += x_not_found('robot', id_robot, index.list_robots())
        raise UserError(msg)
        
    agent.init(boot_spec)

    return load_agent_state_core(data_central, id_agent, agent, id_robot,
                                 reset_state=reset_state,
                                 raise_if_no_state=raise_if_no_state)