import configparser
import json
import sys

class INIEngine:
    def __init__(self, config_path):
        self.config_path = config_path

    def load(self):
        config = configparser.ConfigParser()
        config.read(self.config_path)
        return {section: dict(config[section].items()) for section in config.sections()}

    def save(self, data):
        config = configparser.ConfigParser()
        for section, values in data.items():
            config.add_section(section)
            for key, value in values.items():
                config.set(section, key, str(value))
        with open(self.config_path, 'w') as configfile:
            config.write(configfile)

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("用法: python ini_engine.py {load|save} <文件路径> [<目标文件路径>]")
        sys.exit(1)

    command = sys.argv[1]
    engine = INIEngine(sys.argv[2])

    if command == "load":
        config_data = engine.load()
        print(json.dumps(config_data, indent=2))
    elif command == "save":
        if len(sys.argv) != 4:
            print("用法: python ini_engine.py save <文件路径> <目标文件路径>")
            sys.exit(1)
        with open(sys.argv[3], 'r') as file:
            data = json.load(file)
        engine.save(data)
    else:
        print("无效的命令")
        sys.exit(1)
