"""
@autor:Yue
@date::14:04
@file:dealScene.py
@project:appQutoInfo

"""

import  os,copy,re
from blueprints.tools.dealCasefile import readCaseFile
from blueprints.settings import config,qa3Config
from blueprints.tools.rwKeyData import rwKeyData

def dealScene(config,cases,houzhui="json",nostatus=["notfinfish","skip","noexec","notfinish","notexec"]):

    '''

    :param config:
    :param cases: list类型场景，eg：[{"a":["1.json","2.json"]}]
    :return:
    '''
    rw=rwKeyData()
    execjson=eval(rw.readKeyData('channnel','execjsonversion'))


    caserootpath= config.casedir#"/Users/dm/Desktop/python_script/autoInterface/testCase"
    #caserootpath=config.casedir
    #dealcase=[]

    for index,pp in enumerate(cases):
        dealcase = []
        #print(pp,"****",pp.keys())

        for cn in pp.keys():

            allowexec=execjson.get(cn)

            #print(allowexec,type(allowexec))

            #print(cn,os.listdir(caserootpath),type(cn))

            if cn in os.listdir(caserootpath) :

                casepath=os.path.join(caserootpath,cn)


                if len(pp[cn])>0:

                    for c in pp[cn]:

                        #for case in pp[p]:

                        if c.split(".")[-1] ==houzhui:
                            case = os.path.join(casepath, c)
                            os.system('sed -ie \'s/ ":/":/g\' {0}'.format(case))
                            if os.path.exists(case+"e"):
                                os.system('rm -rf {0}'.format(case+"e"))
                            #print(case)
                            f=readCaseFile(case)

                            if f.get("status") not in nostatus:
                                #print(f.get('execjsonversion'))
                                if allowexec:
                                    #print(str(allowexec).split(','),f.get('execjsonversion'))
                                    if f.get('execjsonversion') and  str(allowexec) in f.get('execjsonversion').split(','):
                                        dealcase.append(case)
                                    #print(dealcase)
                                else:
                                    dealcase.append(case)


                        else:
                            raise Exception("{0}的后缀不是json或者{0} not in {1}".format(c,casepath))

                    cases[index]={cn:dealcase}

                # else:
                #     #dealcase=[os.path.join(casepath,cc) for cc in os.listdir(casepath) if cc.split(".")[-1] ==houzhui ]
                #
                #     cases[index] = {cn: [os.path.join(casepath,cc) for cc in os.listdir(casepath) if cc.split(".")[-1] ==houzhui
                #                          and readCaseFile(os.path.join(casepath,cc)).get("status") not in nostatus ]} #if cc.split["."] =="json"

            else:
                raise Exception("{0} not in {1}".format(cn, os.listdir(caserootpath)))
    #print("cases===",cases)
    t=copy.deepcopy(cases)
    for case in cases:

        for k,v in case.items():
            #print(k,"==",v,index)

            if len(v)<=0:
                t.remove(case)
            else:
                continue

            #print("cases=======",cases)

    return t





if __name__ == '__main__':
    cases= [{'hetong': []}, {'passportapi': ['347.json','323.json']}]
    #cases=[{'hetong': []}, {'ces': []}, {'passportapi': ['359.json', '358.json', '323.json', '357.json', '361.json', '360.json']}]
    cases=[{'payment':['565.json',
 '566.json',
 '607.json',
 '608.json',
 '609.json',
 '610.json',
 '611.json',
 '652.json',
 '688.json',
 '716.json',
 '743.json',
 '744.json',
 '762.json',
 '763.json',
 '764.json']}]
    acases=dealScene(qa3Config,cases)

    # caserootpath = "/Users/dm/Desktop/python_script/autoInterface/testCase/passportapi"
    # temp=[]
    # for i in os.listdir(caserootpath):
    #     temp.append(int(i.split('.')[0]))
    #
    #
    #
    print("==",acases)








