Ejemplo n.º 1
0
def fetch(doc):
    """Fetch document (if necessary).

    Parameters
    ----------
    doc : {dict, string}
        A dictionary representing a handle returned by es_document, or a plain
        string. A Unicode string (Python 2 unicode, Python 3 str) will be
        returned as-is. A byte string (Python 2 str, Python 3 bytes) will be
        run through chardet to guess the encoding, then decoded with
        errors="replace".

    Returns
    -------
    content : string
        Document contents.
    """
    if isinstance(doc, dict) and set(doc.keys()) == set(_ES_DOC_FIELDS):
        idx, typ, id, field = [doc[k] for k in _ES_DOC_FIELDS]
        return _es().get_source(index=idx, doc_type=typ, id=id)[field]
    elif isinstance(doc, unicode):
        return doc
    elif isinstance(doc, str):
        enc = chardetect(doc)['encoding']
        return doc.decode(enc, errors="replace")
    else:
        raise TypeError("fetch expected es_document or string, got %s" %
                        type(doc))
Ejemplo n.º 2
0
def fetch(doc):
    """Fetch document (if necessary).

    Parameters
    ----------
    doc : {dict, string}
        A dictionary representing a handle returned by es_document, or a plain
        string. A Unicode string (Python 2 unicode, Python 3 str) will be
        returned as-is. A byte string (Python 2 str, Python 3 bytes) will be
        run through chardet to guess the encoding, then decoded with
        errors="replace".

    Returns
    -------
    content : string
        Document contents.
    """
    if isinstance(doc, dict) and set(doc.keys()) == set(_ES_DOC_FIELDS):
        idx, typ, id, field = [doc[k] for k in _ES_DOC_FIELDS]
        return _es().get_source(index=idx, doc_type=typ, id=id)[field]
    elif isinstance(doc, unicode):
        return doc
    elif isinstance(doc, str):
        enc = chardetect(doc)['encoding']
        return doc.decode(enc, errors="replace")
    else:
        raise TypeError("fetch expected es_document or string, got %s"
                        % type(doc))
Ejemplo n.º 3
0
 def _readline_decoder(self , bytes_):
     global _ENCODING
     try:
         return bytes_.decode(_ENCODING).replace('\r','\n')
     except:
         _encoding = chardetect(bytes_)['encoding']
         _ENCODING = _encoding
         return bytes_.decode(_encoding).replace('\r','\n')
Ejemplo n.º 4
0
 def _unicode_noerrs(self, string):
     if not string:
         return u""
     if type(string) == type(unicode()):
         return string
     try:
         return unicode(string, encoding=chardetect(string)["encoding"])
     except:
         raise UnicodeWarning("EXCEPTION IN FileChooserThumbView._unicode_noerrs skipped.\nThis means that file list might not contain all the files that are really present in the directory.\nThis was the exception:")
         traceback.print_exc()
         return u""
Ejemplo n.º 5
0
def is_text_file(string):
    
    if ('defkeymap.map' in string):
        pass
    path = ut.join_path(c['sourcedir'], string)
    inpt = open(path, "rb")
    fsiz = ut.file_size(path)
    abuf = inpt.read(min(1024, fsiz))
    inpt.close()
    ret  = chardetect(abuf)
    return (isinstance(ret, dict) and ('encoding' in ret) and (ret['encoding'] is not None))
Ejemplo n.º 6
0
def is_text_file(string):

    if ('defkeymap.map' in string):
        pass
    path = ut.join_path(c['sourcedir'], string)
    inpt = open(path, "rb")
    fsiz = ut.file_size(path)
    abuf = inpt.read(min(1024, fsiz))
    inpt.close()
    ret = chardetect(abuf)
    return (isinstance(ret, dict) and ('encoding' in ret)
            and (ret['encoding'] is not None))
Ejemplo n.º 7
0
 def _unicode_noerrs(self, string):
     if not string:
         return u""
     if type(string) == type(unicode()):
         return string
     try:
         return unicode(string, encoding=chardetect(string)["encoding"])
     except:
         raise UnicodeWarning(
             "EXCEPTION IN FileChooserThumbView._unicode_noerrs skipped.\nThis means that file list might not contain all the files that are really present in the directory.\nThis was the exception:"
         )
         traceback.print_exc()
         return u""
Ejemplo n.º 8
0
 def _unicode_noerrs(self, string):
     if not string:
         return u""
     return unicode(string, encoding=chardetect(string)["encoding"])