示例#1
0
def detectEncoding(path):
    """Attempt automatic detection of the given file's encoding.
    
    Returns:
        Either the encoding as a string (suitable for passing to editFile or 
        codecs.open), or None if the automatic detection failed.
    """
    from edu.wisc.ssec.mcidasv.util import DetectCharset
    return str(DetectCharset.detect(expandpath(path)))
示例#2
0
def editFile(path, encoding=None, cleanup=False):
    """Import file contents into the Jython Shell input field.
    
    Args:
        path: Required string value that represents a path to a file. The string
              is validated with expandpath, so paths like "~/test.py" will work.
              
        encoding: Optional string value that defaults to None. If given a value,
                  no attempt will be made to check the encoding of the given
                  file.
              
        cleanup: Optional boolean value that defaults to False. If set to True,
                 calls to removeAllData() and removeAllLayers() are added to the 
                 beginning of the Jython Shell input text field.
    """
    from edu.wisc.ssec.mcidasv.util import DetectCharset
    import codecs
    
    filepath = expandpath(path)
    encoding = encoding or DetectCharset.detect(filepath)
    
    # detection may have failed
    if not encoding:
        raise RuntimeError('Could not detect encoding of "%s". Please verify the file\'s encoding and then run "editFile(\'%s\', encoding=\'ENCODING_HERE\', cleanup=%s)".' % (path, path, cleanup))
        
    fp = codecs.open(filepath, 'r', encoding=encoding)
    try:
        shell = getStaticMcv().getJythonManager().getShell()
        lines = u''
        if cleanup:
            lines += u'# removeAllData and removeAllLayers were added because editFile was called with "cleanup" set to True.\nremoveAllData()\nremoveAllLayers()\n\n'
        once = False
        for line in fp:
            lines += line
        shell.setMultilineText(lines)
    except UnicodeDecodeError:
        detected = DetectCharset.detect(filepath)
        raise RuntimeError('Could not decode contents of "%s" using "%s"! You may want to retry by running "editFile(\'%s\', encoding=\'%s\', cleanup=%s)".' % (path, encoding, path, detected, cleanup))
    finally:
        fp.close()