from os import walk
import re, sys

class DirectoryReader(object):
    # Definition of static values
    __extensions_video = ['mkv', 'avi', 'wmv', 'mp4']
    __extensions_subtitle = ['srt', 'sub']
    __extensions_audio = ['ac3', 'ogg', 'mp3']
    __valid_languages = ['fre', 'eng', 'ger']
    
    # Definition of local variables
    list_video_files = []
    list_subtitle_files = []
    list_subtitle_languages = []
    list_audio_files = []
    list_audio_languages = []
    list_garbage_files = []
    
    # Constructor
    def __init__(self, path):
        self.path = path
        for (path,dirs,files) in walk(self.path):
            # At this level we do not care about directory walking, should be called by the parent
            for file in files:
                tmp = re.split('[.]',file)
                if len(tmp) > 0:
                    if tmp[len(tmp)-1] in self.__extensions_video:
                        self.list_video_files.append(file)
                    elif tmp[len(tmp)-1] in self.__extensions_subtitle:
                        self.list_subtitle_files.append(file)
                        if len(tmp) > 1 and tmp[len(tmp)-2] in self.__valid_languages:
                            self.list_subtitle_languages.append(tmp[len(tmp)-2])
                        else:
                            self.list_subtitle_languages.append('unk')
                    elif tmp[len(tmp)-1] in self.__extensions_audio:
                        self.list_audio_files.append(file)
                        if len(tmp) > 1 and tmp[len(tmp)-2] in self.__valid_languages:
                            self.list_audio_languages.append(tmp[len(tmp)-2])
                        else:
                            self.list_audio_languages.append('unk')
                    else:
                        self.list_garbage_files.append(file)
        if len(self.list_audio_languages) != len(self.list_audio_files) or len(self.list_subtitle_languages) != len(self.list_subtitle_files):
            print('Something went terribly wrong while reading ' + path + '\nAborting.')
            sys.exit(2) 
                