Пример #1
0
def detectEncoding(path, lang, probeSize=32 * 1024):
    dlang, denc = locale.getdefaultlocale()
    if not lang:
        lang2 = dlang.split('_', 1)[0]
        lang = languages2to3.get(lang2)

    encs = ['UTF-8'] + languages.get(lang, (None, []))[1]
    if denc not in encs:
        encs.append(denc)

    try:
        for enc in encs:
            with open(path, 'r', encoding=enc) as fp:
                try:
                    fp.read(32 * 1024)
                    logger.info('detected encoding %s for file "%s"', enc,
                                path)
                    return enc
                except UnicodeError:
                    pass
    except FileNotFoundError:
        raise Error('File not found').add('path', path)

    logger.info('couldn\'t detect encoding for file "%s", tried %s', path,
                encs)
Пример #2
0
    def initLangs(self):
        from subsync.data.languages import languages, languages2to3
        from subsync.translations import listLanguages as langs

        self.Append(_('default'), None)
        self.addSortedLangs(
            {languages.get(languages2to3.get(x), [x])[0]: x
             for x in langs()})
Пример #3
0
    def __init__(self, *args, **kwargs):
        from subsync.data.languages import languages, languages2to3
        from subsync.translations import listLanguages as langs

        super().__init__(*args, **kwargs)
        self.Append(_('default'), None)
        self.addSortedLangs(
            {languages.get(languages2to3.get(x), [x])[0]: x
             for x in langs()})
Пример #4
0
def getLangFromPath(path):
    ''' Returns two- or three-letters language code from filename in form
    name.code.extension, e.g. subtitles.eng.srt or subtitles-fr.srt
    '''

    name = path.rsplit('.', 1)[0]
    size = 0

    for c in reversed(name):
        if c.isalpha():
            size += 1
        else:
            break

    if size == 2:
        return languages2to3.get(name[-2:].lower())
    elif size == 3:
        return name[-3:].lower()