import time

def checkConfig(config):
    '''
    检查配置
    '''
    print('check config ...')
    if type(config) == dict:
        return checkChannelConfig(config)
    elif type(config) == list:
        for itemConfig in config:
            if not checkChannelConfig(itemConfig):
                return False
    return True

def checkChannelConfig(config):
    if 'subChannel' not in config:
        print('subChannel not exists in config')
        return False

    # 默认值
    if 'changeIcon' not in config:
        config['changeIcon'] = False
    if 'switchIcon' not in config:
        config['switchIcon'] = False
    if 'addLauncher' not in config:
        config['addLauncher'] = False
    if 'splitDex' not in config:
        config['splitDex'] = True
    if 'clearCache' not in config:
        config['clearCache'] = True
    if 'aapt2disable' not in config:
        config['aapt2disable'] = False
    if 'v2disable' not in config:
        config['v2disable'] = False
    if 'outName' not in config:
        config['outName'] = config['name']
        
    return True

def replaceArgs(config):
    '''
    替换占位符
    '''
    if type(config) == dict:
        replaceItemArgs(config)
    elif type(config) == list:
        # 遍历数组
        for arg in config:
            replaceItemArgs(arg)

def replaceItemArgs(config):
    '''
    替换占位符
    '''
    # 遍历字典
    for arg in config:
        if type(config[arg]) == dict:
            replaceArgs(config[arg])
        elif type(config[arg]) == str:
            replaceString(config, arg)

def replaceString(config, arg):
    '''
    替换占位符
    '''
    content = config[arg]
    if '${DATE}' in content:
        content = content.replace('${DATE}', getDate())

    config[arg] = content

def getDate():
    '''
    获取日期
    '''
    return time.strftime("%Y%m%d", time.localtime()) 