import os
import subprocess
import re

class Logs:
    """
    Module `Logs`
    Module which shows log file presents in system.
    `self.path` System path in which all log files have been generated by the system.
    """
    def __init__(self):
        self.path="/var/log/"
    def display_all_log_files(self):
        """
        Lists all files which present in '/var/log' directory
        """
        filesDic={}
        filesList=[]
        os.chdir(self.path)
        commands=os.popen("ls").read()
        split=commands.split("\n")
        if "" in split:
            split.remove("")
        filesDic["list"]=split
        filesList.append(filesDic.copy())
        return filesList        
        
    def display_specific_log_files(self,file_name=None):
        """
        To open any specific log file
        """
        spfileDic={}
        spfileList=[]
        logDic={}
        logList=[]
        zipString=""
        user_path=file_name
        if user_path.endswith(".gz"):
            directory=user_path.split(".")
            directory.remove("gz")    
            for i in directory:
                zipString=zipString+i
            os.system("sudo gunzip {}".format(user_path))
            if os.path.isdir(zipString):         
                os.chdir("{}{}".format(self.path,zipString))
                commands=os.popen("sudo ls").read()
                split=commands.split("\n")
                if "" in split:
                    split.remove("")
                spfileDic["list"]=split
                spfileList.append(spfileDic.copy())
                return spfileList

            else:
                commands=os.popen("sudo ls").read()
                split=commands.split("\n")
                if "" in split:
                    split.remove("")
                spfileDic["list"]=split
                spfileList.append(spfileDic.copy())
                return spfileList
       
        elif os.path.isdir(user_path):
            os.chdir("{}{}".format(self.path,user_path))
            commands=os.popen("sudo ls").read()
            split=commands.split("\n")
            if "" in split:
                split.remove("")
            spfileDic["list"]=split
            spfileList.append(spfileDic.copy())
            return spfileList
           
            os.chdir(self.path+user_path)
            logfile_name=file_name
            logs=os.popen("cat "+logfile_name).read()
            split=logs.split("\n")
            if "" in split:
                split.remove("")
            logDic["logs"]=split
            logList.append(logDic.copy())
            return logList
        else:
            logs=os.popen("cat "+user_path).read()
            split=logs.split("\n")
            if "" in split:
                split.remove("")
            logDic["logs"]=split
            logList.append(logDic.copy())
            return logList  
