# -*- coding: iso-8859-8 -*-

#ltrsubs - Converts hebrew subtitles from ANSI to Unicode and provides RTL support.

import os, string, hashlib, base64, re, plistlib, unicodedata, codecs

class LineStatus:
    LINE_NUMBER = 1
    TIME = 2
    SUBTITLES = 3

def reverseGroup(match, groupName):
        line = ''
        for group in match.groups():
            if (group is None):
                continue
            if (group == match.group(groupName)):
                line += group[::-1]
            else:
                line += group
        return line

def writeHebSubs(inputFile, encoding):
    input = os.fdopen(os.open(inputFile, os.O_RDONLY))
    lines = input.readlines()
    state = LineStatus.LINE_NUMBER
    result = ''
    for line in lines:
        if (state == LineStatus.LINE_NUMBER):
            result += line
            state = LineStatus.TIME
            continue
        if (state == LineStatus.TIME):
            result += line
            state = LineStatus.SUBTITLES
            continue
        if (line == '\n'):
            state = LineStatus.LINE_NUMBER
        line = re.sub('(<[a-zA-Z]>)*([\.\,\"\-:\?\'!\(\)a-zA-Z]*)(?P<subs>.*[à-ú]+.*?)([\.\,\"\-:\?\'!\(\)a-zA-Z]*)(</[a-zA-Z]>)*$', lambda match: reverseGroup(match, 'subs'), line)
        line = re.sub(r'([à-ú]+.*)(?P<nums>[0-9]+)', lambda match: reverseGroup(match, 'nums'), line)
        line = re.sub(r'(?P<nums>[0-9]+)(.*[à-ú])+', lambda match: reverseGroup(match, 'nums'), line)
        result += line
    input.close()
    return codecs.BOM_UTF8 + result.decode(encoding).encode('utf-8')
    
def is_encoding(lines, encoding):
    for line in lines:
        try:
            #bytes(line, encoding)
            line.decode(encoding)
        #except UnicodeEncodeError as e:
        except UnicodeDecodeError as e:
            return False
    return True

def searchRegex(lines, regex):
    for line in lines:
        if (regex.match(line)):
            return True
    return False       

def get_encoding(lines, encodings):
    for encoding in encodings:
        if (is_encoding(lines, encoding)):
            return encoding
    return None
    
def get_hebrew_encoding(inputFile):
    hebrewRegex = re.compile('.*[à-ú]{3,}.*', re.DOTALL | re.MULTILINE)
    with os.fdopen(os.open(inputFile, os.O_RDONLY)) as input:
        lines = input.readlines()
        encoding = get_encoding(lines, ['iso-8859-8', 'cp862', 'windows-1255'])
        if (encoding is None):
            return None
        if (False == searchRegex(lines, hebrewRegex)):
            return None
        return encoding
fd2087f60ad2531bf6f473c9922a586877c16757
<mask token>


class LineStatus:
    LINE_NUMBER = 1
    TIME = 2
    SUBTITLES = 3


<mask token>


def get_encoding(lines, encodings):
    for encoding in encodings:
        if is_encoding(lines, encoding):
            return encoding
    return None


<mask token>

<mask token>


class LineStatus:
    LINE_NUMBER = 1
    TIME = 2
    SUBTITLES = 3


def reverseGroup(match, groupName):
    line = ''
    for group in match.groups():
        if group is None:
            continue
        if group == match.group(groupName):
            line += group[::-1]
        else:
            line += group
    return line


def writeHebSubs(inputFile, encoding):
    input = os.fdopen(os.open(inputFile, os.O_RDONLY))
    lines = input.readlines()
    state = LineStatus.LINE_NUMBER
    result = ''
    for line in lines:
        if state == LineStatus.LINE_NUMBER:
            result += line
            state = LineStatus.TIME
            continue
        if state == LineStatus.TIME:
            result += line
            state = LineStatus.SUBTITLES
            continue
        if line == '\n':
            state = LineStatus.LINE_NUMBER
        line = re.sub(
            '(<[a-zA-Z]>)*([\\.\\,"\\-:\\?\'!\\(\\)a-zA-Z]*)(?P<subs>.*[à-ú]+.*?)([\\.\\,"\\-:\\?\'!\\(\\)a-zA-Z]*)(</[a-zA-Z]>)*$'
            , lambda match: reverseGroup(match, 'subs'), line)
        line = re.sub('([à-ú]+.*)(?P<nums>[0-9]+)', lambda match:
            reverseGroup(match, 'nums'), line)
        line = re.sub('(?P<nums>[0-9]+)(.*[à-ú])+', lambda match:
            reverseGroup(match, 'nums'), line)
        result += line
    input.close()
    return codecs.BOM_UTF8 + result.decode(encoding).encode('utf-8')


def is_encoding(lines, encoding):
    for line in lines:
        try:
            line.decode(encoding)
        except UnicodeDecodeError as e:
            return False
    return True


<mask token>


def get_encoding(lines, encodings):
    for encoding in encodings:
        if is_encoding(lines, encoding):
            return encoding
    return None


<mask token>

<mask token>


class LineStatus:
    LINE_NUMBER = 1
    TIME = 2
    SUBTITLES = 3


def reverseGroup(match, groupName):
    line = ''
    for group in match.groups():
        if group is None:
            continue
        if group == match.group(groupName):
            line += group[::-1]
        else:
            line += group
    return line


def writeHebSubs(inputFile, encoding):
    input = os.fdopen(os.open(inputFile, os.O_RDONLY))
    lines = input.readlines()
    state = LineStatus.LINE_NUMBER
    result = ''
    for line in lines:
        if state == LineStatus.LINE_NUMBER:
            result += line
            state = LineStatus.TIME
            continue
        if state == LineStatus.TIME:
            result += line
            state = LineStatus.SUBTITLES
            continue
        if line == '\n':
            state = LineStatus.LINE_NUMBER
        line = re.sub(
            '(<[a-zA-Z]>)*([\\.\\,"\\-:\\?\'!\\(\\)a-zA-Z]*)(?P<subs>.*[à-ú]+.*?)([\\.\\,"\\-:\\?\'!\\(\\)a-zA-Z]*)(</[a-zA-Z]>)*$'
            , lambda match: reverseGroup(match, 'subs'), line)
        line = re.sub('([à-ú]+.*)(?P<nums>[0-9]+)', lambda match:
            reverseGroup(match, 'nums'), line)
        line = re.sub('(?P<nums>[0-9]+)(.*[à-ú])+', lambda match:
            reverseGroup(match, 'nums'), line)
        result += line
    input.close()
    return codecs.BOM_UTF8 + result.decode(encoding).encode('utf-8')


def is_encoding(lines, encoding):
    for line in lines:
        try:
            line.decode(encoding)
        except UnicodeDecodeError as e:
            return False
    return True


def searchRegex(lines, regex):
    for line in lines:
        if regex.match(line):
            return True
    return False


def get_encoding(lines, encodings):
    for encoding in encodings:
        if is_encoding(lines, encoding):
            return encoding
    return None


<mask token>

<mask token>


class LineStatus:
    LINE_NUMBER = 1
    TIME = 2
    SUBTITLES = 3


def reverseGroup(match, groupName):
    line = ''
    for group in match.groups():
        if group is None:
            continue
        if group == match.group(groupName):
            line += group[::-1]
        else:
            line += group
    return line


def writeHebSubs(inputFile, encoding):
    input = os.fdopen(os.open(inputFile, os.O_RDONLY))
    lines = input.readlines()
    state = LineStatus.LINE_NUMBER
    result = ''
    for line in lines:
        if state == LineStatus.LINE_NUMBER:
            result += line
            state = LineStatus.TIME
            continue
        if state == LineStatus.TIME:
            result += line
            state = LineStatus.SUBTITLES
            continue
        if line == '\n':
            state = LineStatus.LINE_NUMBER
        line = re.sub(
            '(<[a-zA-Z]>)*([\\.\\,"\\-:\\?\'!\\(\\)a-zA-Z]*)(?P<subs>.*[à-ú]+.*?)([\\.\\,"\\-:\\?\'!\\(\\)a-zA-Z]*)(</[a-zA-Z]>)*$'
            , lambda match: reverseGroup(match, 'subs'), line)
        line = re.sub('([à-ú]+.*)(?P<nums>[0-9]+)', lambda match:
            reverseGroup(match, 'nums'), line)
        line = re.sub('(?P<nums>[0-9]+)(.*[à-ú])+', lambda match:
            reverseGroup(match, 'nums'), line)
        result += line
    input.close()
    return codecs.BOM_UTF8 + result.decode(encoding).encode('utf-8')


def is_encoding(lines, encoding):
    for line in lines:
        try:
            line.decode(encoding)
        except UnicodeDecodeError as e:
            return False
    return True


def searchRegex(lines, regex):
    for line in lines:
        if regex.match(line):
            return True
    return False


def get_encoding(lines, encodings):
    for encoding in encodings:
        if is_encoding(lines, encoding):
            return encoding
    return None


def get_hebrew_encoding(inputFile):
    hebrewRegex = re.compile('.*[à-ú]{3,}.*', re.DOTALL | re.MULTILINE)
    with os.fdopen(os.open(inputFile, os.O_RDONLY)) as input:
        lines = input.readlines()
        encoding = get_encoding(lines, ['iso-8859-8', 'cp862', 'windows-1255'])
        if encoding is None:
            return None
        if False == searchRegex(lines, hebrewRegex):
            return None
        return encoding

# -*- coding: iso-8859-8 -*-

#ltrsubs - Converts hebrew subtitles from ANSI to Unicode and provides RTL support.

import os, string, hashlib, base64, re, plistlib, unicodedata, codecs

class LineStatus:
    LINE_NUMBER = 1
    TIME = 2
    SUBTITLES = 3

def reverseGroup(match, groupName):
        line = ''
        for group in match.groups():
            if (group is None):
                continue
            if (group == match.group(groupName)):
                line += group[::-1]
            else:
                line += group
        return line

def writeHebSubs(inputFile, encoding):
    input = os.fdopen(os.open(inputFile, os.O_RDONLY))
    lines = input.readlines()
    state = LineStatus.LINE_NUMBER
    result = ''
    for line in lines:
        if (state == LineStatus.LINE_NUMBER):
            result += line
            state = LineStatus.TIME
            continue
        if (state == LineStatus.TIME):
            result += line
            state = LineStatus.SUBTITLES
            continue
        if (line == '\n'):
            state = LineStatus.LINE_NUMBER
        line = re.sub('(<[a-zA-Z]>)*([\.\,\"\-:\?\'!\(\)a-zA-Z]*)(?P<subs>.*[à-ú]+.*?)([\.\,\"\-:\?\'!\(\)a-zA-Z]*)(</[a-zA-Z]>)*$', lambda match: reverseGroup(match, 'subs'), line)
        line = re.sub(r'([à-ú]+.*)(?P<nums>[0-9]+)', lambda match: reverseGroup(match, 'nums'), line)
        line = re.sub(r'(?P<nums>[0-9]+)(.*[à-ú])+', lambda match: reverseGroup(match, 'nums'), line)
        result += line
    input.close()
    return codecs.BOM_UTF8 + result.decode(encoding).encode('utf-8')
    
def is_encoding(lines, encoding):
    for line in lines:
        try:
            #bytes(line, encoding)
            line.decode(encoding)
        #except UnicodeEncodeError as e:
        except UnicodeDecodeError as e:
            return False
    return True

def searchRegex(lines, regex):
    for line in lines:
        if (regex.match(line)):
            return True
    return False       

def get_encoding(lines, encodings):
    for encoding in encodings:
        if (is_encoding(lines, encoding)):
            return encoding
    return None
    
def get_hebrew_encoding(inputFile):
    hebrewRegex = re.compile('.*[à-ú]{3,}.*', re.DOTALL | re.MULTILINE)
    with os.fdopen(os.open(inputFile, os.O_RDONLY)) as input:
        lines = input.readlines()
        encoding = get_encoding(lines, ['iso-8859-8', 'cp862', 'windows-1255'])
        if (encoding is None):
            return None
        if (False == searchRegex(lines, hebrewRegex)):
            return None
        return encoding