#!/usr/bin/env python
#======================================================================
#
# Project	 : hpp_IOStressTest
# File 		 : IOST.py
# Date		 : Sep 21, 2016
# Author	 : HuuHoang Nguyen
# Contact	 : hhnguyen@apm.com
#            : hoangnh.hpp@gmail.com
# License    : MIT License
# Copyright  : 2016
# Description: The hpp_IOStressTest is under the MIT License, a copy of license which may be found in LICENSE
#
#======================================================================

import io
import os
import operator
import sys
import base64
import time

# from Libs.IOST_Prepare import *
# from Libs.IOST_Config  import *
# from Libs.IOST_WMain   import *
# from Libs.IOST_Basic   import *

sys.path.append("Libs")
sys.path.append("../Libs")

from Libs            import IOST_Basic
from Libs            import IOST_Config
from Libs            import IOST_WMain
from Libs.IOST_WMain import *
from Libs            import IOST_Prepare

import gtk
import gtk.glade
import gobject
# from Libs  import *
# from Libs   import *
# from Libs   import *

#======================================================================

IOST_Debug_Enable = 0

#======================================================================
# argv_number = len(sys.argv)
# for i in range(0, argv_number):
#     print sys.argv[i]

#======================================================================

IOST_WMAIN                       = "IOST_WMain"
IOST_CHIP                        = "Skylark"
IOST_OBJECT                      = "_Skylark"

IOST_CONFIG_DATA_DEFAULTE_FILE   = "IOST_DataDefault.json"
IOST_CONFIG_OBJS_DEFAULTE_FILE   = "IOST_ObjsDefault.json"
IOST_PROGRAM_PATH=os.path.dirname(os.path.abspath(sys.argv[0]))
if IOST_Debug_Enable:
    print IOST_PROGRAM_PATH

IOST_SSH_BIN     = "ssh"
IOST_TELNET_BIN  = "telnet"
IOST_SHELL_BIN   = os.environ["SHELL"]

IOST_CONGIG_DATA_PATH = IOST_PROGRAM_PATH + "/" + IOST_CHIP + "/" + IOST_CONFIG_DATA_DEFAULTE_FILE
if IOST_Debug_Enable:
    print IOST_CONGIG_DATA_PATH
IOST_CONGIG_OBJS_PATH = IOST_PROGRAM_PATH + "/" + IOST_CHIP + "/" + IOST_CONFIG_OBJS_DEFAULTE_FILE
if IOST_Debug_Enable:
    print IOST_CONGIG_OBJS_PATH

#======================================================================
class IOST(IOST_WMain):
    """
    This is a main class of the program.
    """

    #----------------------------------------------------------------------
    def __init__(self, glade_filename = "", 
                    window_name       = "",
                    object_name       = "",
                    iost_data         = None, 
                    iost_objs         = None):
        "The function is main function to start IOST program"

        IOST_WMain.__init__(self, glade_filename, window_name, object_name, iost_data, iost_objs)

    #----------------------------------------------------------------------
    def IOST_Main(self):
        gtk.main()


#======================================================================
#                            MAIN FUNCTION
#======================================================================
if __name__ == "__main__":
    "The main function of IOST"



    IOST_Config=IOST_Config()

    #-------------------------------------------------------------------------
    IOST_Config.IOST_Data = IOST_Config.ReadFile(file_name=IOST_CONGIG_DATA_PATH)
    

    #-------------------------------------------------------------------------
    IOST_Config.IOST_Objs = IOST_Config.ReadFile(file_name=IOST_CONGIG_OBJS_PATH)
    

    IOST_Config.IOST_Data["GladeFileName"] = IOST_PROGRAM_PATH + "/" + IOST_CHIP+ '/'+ IOST_Config.IOST_Data["GladeFileName"] + '_'+ IOST_Config.IOST_Data["ProjectVersion"] + '.glade'
   
   
    # print IOST_Config.IOST_Data["GladeFileName"]

    # print "=================================================================="
    # pprint (IOST_Config.IOST_Data.keys())
    # print "=================================================================="
    # pprint (IOST_Config.IOST_Objs["IOST_WMain"].keys())
    # print "=================================================================="
    argv_number = len(sys.argv)
    
    if IOST_Debug_Enable:
        print "=================================================================="        
        print "Number of arg have entered is : ", argv_number        
        for i in range(0, argv_number):
            print "========== argv[%s] = : " %(i, sys.argv[i])                
    

    #Add config file to a Files list
    for i in range(1, argv_number):
        # print i
        # IOST_Config.IOST_Files.append(sys.argv[1]+'/'+sys.argv[i])
        if os.path.isfile(sys.argv[i]):
            IOST_Config.AddFileConfig2List(IOST_Config.IOST_Files, sys.argv[i])
        else:
            IOST_Config.AddFileConfig2List(IOST_Config.IOST_Files, IOST_PROGRAM_PATH +'/'+sys.argv[i])


    # Print to debug name of all file config have inputed  
    if IOST_Debug_Enable:
        print "=========================The list config files have entered==========================="
        print "Number of config Files is %s" % (len (IOST_Config.IOST_Files))
        print "Number of config files is: "
        for i in range(0, len (IOST_Config.IOST_Files)):
            pprint (IOST_Config.IOST_Files[i])
    
    #Read file and store in Files Dist type at location (2n+1)
    if argv_number > 1:
        IOST_Config.AddObjConfig2List(IOST_Config.IOST_Files)
    
    if IOST_Debug_Enable:        
        for i in range(0, len (IOST_Config.IOST_Files)):
            print "================================= %s ================================="  %i
            pprint (IOST_Config.IOST_Files[i])   
   
    for i in range(0, len (IOST_Config.IOST_Files), 2):
        IOST_Config.ModifyIOST_Objs(IOST_Config.IOST_Data, IOST_Config.IOST_Files[i+1] )

    if IOST_Debug_Enable: 
        print "IOST_Config.IOST_Data is : "
        pprint (IOST_Config.IOST_Data)
        print "IOST_Config.IOST_Data['I2C0'] is : "
        pprint (IOST_Config.IOST_Data["I2C0"])

    IOST_Config.IOST_Data["IOST_Path"] = IOST_PROGRAM_PATH
    IOST_Config.IOST_Data["IOST_RunPath"] = os.getcwd()
    IOST_Config.IOST_Data["ConfigFile"]["CfgDataPath"] = IOST_CONGIG_DATA_PATH
    IOST_Config.IOST_Data["ConfigFile"]["CfgObjsPath"] = IOST_CONGIG_OBJS_PATH
    

    #-------------------------------------------------------------------------
    IOST_Config.WriteFile(IOST_PROGRAM_PATH+"/Temp_Configs/Config_Data.json", IOST_Config.IOST_Data)
    IOST_Config.WriteFile(IOST_PROGRAM_PATH+"/Temp_Configs/Config_Objects.json", IOST_Config.IOST_Objs)

    # Some debug code here
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    if False:
        for key, value in IOST_Config.IOST_Objs["IOST_WSetupTestcase"].iteritems():
            print key, value
    if False:
        len__temp = len(IOST_Config.IOST_Objs["IOST_WSetupTestcase"])
        print  "=============================================="
        print "Len of IOST_WSetupTestcase object is", len__temp
        print  "=============================================="
        print IOST_Config.IOST_Objs["IOST_WSetupTestcase"].keys()
        
        for i in range(0, len__temp, 2 ):
            print  "=============================================="
            print i
            print  "----------------------------------------------"
            print IOST_Config.IOST_Objs["IOST_WSetupTestcase"].keys()[i]
            print  "----------------------------------------------"
            print IOST_Config.IOST_Objs["IOST_WSetupTestcase"].keys()[i+1]
            print  "----------------------------------------------"
            print IOST_Config.IOST_Objs["IOST_WSetupTestcase"][IOST_Config.IOST_Objs["IOST_WSetupTestcase"].keys()[(i+1)]]
    if False:
        exit(1)
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    #-------------------------------------------------------------------------
    main = IOST(glade_filename=IOST_Config.IOST_Data["GladeFileName"],
                window_name=IOST_WMAIN,
                object_name=IOST_OBJECT,
                iost_data=IOST_Config.IOST_Data, iost_objs=IOST_Config.IOST_Objs)

    main.IOST_Main()

