Read metadata from a delimited text file.

def readstoredmetadata(fname, skiprows=0, linenumber=None, comments='#', 
                       metametadata=None, verbosity=DEFAULT_VERBOSITY):
    """
    Read metadata from a delimited text file.
    
    """
    if is_string_like(fname):
        fh = file(fname, 'rU')
    elif hasattr(fname, 'readline'):
        fh = fname
    else:
        raise ValueError('fname must be a string or file handle')
       
    if not metametadata:
        for _ind in range(skiprows):
            fh.readline()

        phlines = []
        if linenumber is None:
            if comments:
                for line in fh:
                    if not line.startswith(comments):
                        if len(phlines) == 0:
                            phlines = [line]
                        break
                    else:
                        phlines.append(line)
                if len(phlines) == 1:
                    if verbosity >= 10:
                        print '''Looking for metametadata on line 0 
                                 (no comment lines present).'''
                else:
                    if verbosity >= 9:
                        print '''Searching for metametadata lines up to 
                                 and including line %d where comments 
                                 end.''' % (len(phlines) - 1)
            else:
                phlines = [fh.readline()]
                if verbosity >=9:
                    print '''No comments found, looking for metametadata on 
                             line 0.'''
        else:
            for _ind in range(linenumber):
                fh.readline()
            phlines = [fh.readline()]
                               
        metametadata = None
        for (ln, metametaline) in enumerate(phlines):
            s = re.compile(r'metametadata[\s]*=[\s]*{').search(metametaline)
            if s:
                l = s.start()
                if len(uniqify(metametaline[:l])) <= 1:
                    metametaline = metametaline[l:].rstrip()
                    try:
                        X = compiler.parse(metametaline)
                    except SyntaxError:
                        pass
                    else:
                        if IsMetaMetaDict(X):
                            exec metametaline
                            if verbosity > 6:
                                print 'Found valid metametadata at line', ln, 'in file.  Metametadata is:', metametadata
                            break
   
    if metametadata:
        
        metadata = {}
        metadata['metametadata'] = metametadata
        Tval = max([v if isinstance(v,int) else max(v) 
                    for v in metametadata.values()])
        fh = file(fname, 'rU')
        data = [fh.readline() for _ind in range(Tval + 1 + skiprows)][skiprows:]
 
        if (max([v if isinstance(v,int) else max(v) 
                for v in  metametadata.values()]) < len(data)):
            for n in metametadata.keys():
                [s, e] = [metametadata[n], metametadata[n]+1] \
                          if isinstance(metametadata[n],int) \
                          else [metametadata[n][0],metametadata[n][1]]
                metadata[n] = ''.join(data[s:e]).strip('\n')
            processmetadata(metadata, comments=comments, verbosity=verbosity)
        
            return metadata