def _stringify(buffer):
    from koUnicodeEncoding import autoDetectEncoding
    encoded, encoding, bom = autoDetectEncoding(buffer)
    try:
        text = str(encoded)
    except UnicodeDecodeError:
        log.debug("Couldn't get a str(...) of %d chars, encoding %s",
                  len(encoded), encoding)
        return -1
    return text
Beispiel #2
0
def _stringify(buffer):
    from koUnicodeEncoding import autoDetectEncoding
    encoded, encoding, bom = autoDetectEncoding(buffer)
    try:
        text = str(encoded)
    except UnicodeDecodeError:
        log.debug("Couldn't get a str(...) of %d chars, encoding %s",
                  len(encoded), encoding)
        return -1
    return text
Beispiel #3
0
 def readfile(self, path):
     try:
         _globalPrefSvc = components.classes["@activestate.com/koPrefService;1"].getService(
             components.interfaces.koIPrefService
         )
         prefs = _globalPrefSvc.prefs
         defaultEncoding = prefs.getStringPref("encodingDefault")
         f = open(path, "rb")
         buffer = f.read()
         unicode_buffer, encoding, bom = koUnicodeEncoding.autoDetectEncoding(
             buffer, tryXMLDecl=1, tryHTMLMeta=1, tryModeline=1, wantEncoding=None, defaultEncoding=defaultEncoding
         )
         return unicode_buffer
     except Exception, ex:
         lastErrorSvc.setLastError(ex.errno, ex.strerror)
         raise
def _genPathCompletions(pattern, cwd, dirsOnly=False):
    import sys, glob
    from os.path import isabs, join, isdir, ismount

    RELATIVE, ABSOLUTE, TILDE = range(3)
    if isabs(pattern):
        type = ABSOLUTE
        abspattern = pattern
    elif pattern and pattern[0] == '~':
        type = TILDE
        abspattern = os.path.expanduser(pattern)
        tilde_part = pattern.split(os.sep, 1)[0]
        home_part = os.path.expanduser(tilde_part)
    else:
        type = RELATIVE
        if cwd:
            abspattern = join(cwd, pattern)
        else:
            raise StopIteration
        cwd_with_sep = (cwd[-1] in (os.sep, os.altsep) and cwd or cwd + os.sep)

    try:
        flist = glob.glob(abspattern + "*")
    except UnicodeDecodeError, ex:
        # Python's os.listdir doesn't handle names with high-bit characters,
        # since it doesn't know which encoding to use, and doesn't bother
        # trying to guess.
        dirPart, restPart = os.path.split(abspattern)
        if not dirPart:
            # os.path.split(<no slashes>) => ('', <no slashes>)
            return
        if restPart:
            # os.path.split(<ends-with-slash>) => (x, '')
            restPartStar = restPart + "*"
        names = os.listdir(dirPart)
        flist = []
        for name in names:
            if not restPart or glob.fnmatch.fnmatch(name, restPartStar):
                try:
                    flist.append(os.path.join(dirPart, name))
                except UnicodeDecodeError:
                    from koUnicodeEncoding import autoDetectEncoding
                    flist.append(
                        os.path.join(dirPart,
                                     autoDetectEncoding(name)[0]))
def _genPathCompletions(pattern, cwd, dirsOnly=False):
    import sys, glob
    from os.path import isabs, join, isdir, ismount

    RELATIVE, ABSOLUTE, TILDE = range(3)
    if isabs(pattern):
        type = ABSOLUTE
        abspattern = pattern
    elif pattern and pattern[0] == "~":
        type = TILDE
        abspattern = os.path.expanduser(pattern)
        tilde_part = pattern.split(os.sep, 1)[0]
        home_part = os.path.expanduser(tilde_part)
    else:
        type = RELATIVE
        if cwd:
            abspattern = join(cwd, pattern)
        else:
            raise StopIteration
        cwd_with_sep = cwd[-1] in (os.sep, os.altsep) and cwd or cwd + os.sep

    try:
        flist = glob.glob(abspattern + "*")
    except UnicodeDecodeError, ex:
        # Python's os.listdir doesn't handle names with high-bit characters,
        # since it doesn't know which encoding to use, and doesn't bother
        # trying to guess.
        dirPart, restPart = os.path.split(abspattern)
        if not dirPart:
            # os.path.split(<no slashes>) => ('', <no slashes>)
            return
        if restPart:
            # os.path.split(<ends-with-slash>) => (x, '')
            restPartStar = restPart + "*"
        names = os.listdir(dirPart)
        flist = []
        for name in names:
            if not restPart or glob.fnmatch.fnmatch(name, restPartStar):
                try:
                    flist.append(os.path.join(dirPart, name))
                except UnicodeDecodeError:
                    from koUnicodeEncoding import autoDetectEncoding

                    flist.append(os.path.join(dirPart, autoDetectEncoding(name)[0]))
Beispiel #6
0
 def readfile(self, path):
     try:
         _globalPrefSvc = components.classes["@activestate.com/koPrefService;1"].\
                             getService(components.interfaces.koIPrefService)
         prefs = _globalPrefSvc.prefs
         defaultEncoding = prefs.getStringPref('encodingDefault')
         f = open(path, 'rb')
         buffer = f.read()
         unicode_buffer, encoding, bom = koUnicodeEncoding.autoDetectEncoding(
             buffer,
             tryXMLDecl=1,
             tryHTMLMeta=1,
             tryModeline=1,
             wantEncoding=None,
             defaultEncoding=defaultEncoding)
         return unicode_buffer
     except Exception, ex:
         lastErrorSvc.setLastError(ex.errno, ex.strerror)
         raise
Beispiel #7
0
 def getUnicodeEncodedStringUsingOSDefault(self, s):
     encoding_name = sys.getfilesystemencoding()
     encoding = self.get_encoding_info(encoding_name).python_encoding_name
     unicodebuffer, encoding, bom =\
         koUnicodeEncoding.autoDetectEncoding(s, defaultEncoding=encoding)
     return unicodebuffer, encoding, bom
Beispiel #8
0
 def getUnicodeEncodedString(self, s):
     encoding_name = self._globalPrefs.getStringPref('encodingDefault')
     encoding = self.get_encoding_info(encoding_name).python_encoding_name
     unicodebuffer, encoding, bom =\
         koUnicodeEncoding.autoDetectEncoding(s, defaultEncoding=encoding)
     return unicodebuffer, encoding, bom
 def getUnicodeEncodedStringUsingOSDefault(self, s):
     encoding_name = sys.getfilesystemencoding()
     encoding = self.get_encoding_info(encoding_name).python_encoding_name
     unicodebuffer, encoding, bom =\
         koUnicodeEncoding.autoDetectEncoding(s, defaultEncoding=encoding)
     return unicodebuffer, encoding, bom
 def getUnicodeEncodedString(self, s):
     encoding_name = self._globalPrefs.getStringPref('encodingDefault')
     encoding = self.get_encoding_info(encoding_name).python_encoding_name
     unicodebuffer, encoding, bom =\
         koUnicodeEncoding.autoDetectEncoding(s, defaultEncoding=encoding)
     return unicodebuffer, encoding, bom