#!/usr/bin/python3
import configparser
import yaml
import sys, os

def convert_one_file(file):
    # 读取.ini或.conf配置文件
    config = configparser.ConfigParser()
    config.read(file)

    # 将配置文件数据转换为字典
    data = {'app':{'version':{'children':{}}}}
    version = data['app']['version']['children']
    for section in config.sections():
        version[section] = {}
        for option in config.options(section):
            value = config.get(section, option)
            if 'keys' not in version[section]:
                version[section]['keys'] = {}
            version[section]['keys'][option] = {}
            version[section]['keys'][option]["default"] = value
            version[section]['keys'][option]["_type"] = 's'

    if not os.path.exists('yaml'):
        os.makedirs('yaml')
    # 将字典转换为YAML格式并写入文件
    with open(f'/home/kylin/yaml/{file.rsplit("/",maxsplit=1)[1].rsplit(".", maxsplit = 1)[0]}.yaml', 'w') as yaml_file:
        yaml.dump(data, yaml_file, allow_unicode = True)

if __name__ == "__main__":
    args = sys.argv[1:]
    for arg in args:
        if os.path.isdir(arg):
            for root, dirs, files in os.walk(arg):
                for file in files:
                    if file.endswith('.conf') or file.endswith('.ini'):
                        convert_one_file(file)
        else:
            if arg.endswith('.conf') or arg.endswith('.ini'):
                convert_one_file(arg)
            else:
                print(f'argument{arg} is not a conf file')