Пример #1
0
def load_entries_from_file(filename):
    ''' 
        It is assumed that the file contains a list of dictionaries.
        
        Each dictionary MUST have the field 'id' and it must be unique.
 
        yields (filename, counter), entry 
    
        *Special fields*
        
        If a field name starts with 'file:', it is processed to resolve
        the relative filename.
        
        For example, if we find: ..
        
            {'file:data': 'data.pickle'}, 
        
        we add: .. 
        
            {'data': '/path/data.pickle'}
    '''
    if not os.path.exists(filename):
        msg = 'File %r does not exist.' % friendly_path(filename)
        raise SemanticMistake(msg)

    name2where = {}
    for where, x in enumerate_entries_from_file(filename):
        try:
            if not ID_FIELD in x:
                msg = 'Entry does not have the %r field' % ID_FIELD
                raise SemanticMistake(msg)

            name = x[ID_FIELD]

            if not is_pattern(name):
                # Otherwise it will complain of missing env variables
                x = substitute_special(x, dirname=os.path.dirname(where[0]))

            # Warn about this?

                
            check_valid_id_or_pattern(name)

            if name in name2where and name2where[name] != where:
                msg = ('Entry %r already know from:\n %s (entry #%d)' % 
                        (name,
                         friendly_path(name2where[name][0]),
                         name2where[name][1]))
                raise SemanticMistake(msg)

            name2where[name] = where

            yield where, x

        except ConfToolsException as e:
            msg = ('Error while loading entry #%d '
                   ' from file\n   %r\n%s\nError: %s' % 
                   (where[1] + 1, friendly_path(where[0]), pformat(x), e))
            logger.error(msg)  # XXX
            raise
Пример #2
0
 def __call__(self, entry):
     try:
         instance = instantiate_spec(entry['code'])
         if self.check_function is not None:
             self.check_function(instance)
         return instance
     except:
         if ConfToolsGlobal.log_instance_error:
             logger.error('Error while trying to instantiate entry:\n%s'
                      % (pformat(entry)))
         raise
Пример #3
0
def load_entries_from_dir(dirname, pattern):
    """ calls load_entries_from_file for each file in dirname respecting
        the pattern. Environment is not expanded. """
    # logger.debug('Loading %r from %r' % (dirname, pattern))
    try:
        filenames = list(locate_files(dirname, pattern))
        for filename in filenames:
            for x in load_entries_from_file(filename):
                yield x
    except:
        logger.error('Error while loading dir %r' % friendly_path(dirname))
        raise
Пример #4
0
def instance_generic_code_desc(entry, expected_class=None):
    try:
        instance = instantiate_spec(entry['code'])
    except:
        if ConfToolsGlobal.log_instance_error:
            msg = 'Error while trying to instantiate entry:\n' + pformat(entry)
            logger.error(msg)
        raise

    if expected_class is not None:
        check_type(entry, expected_class, instance)

    return instance