184 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
		
		
			
		
	
	
			184 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
|  | #!/usr/bin/env python | ||
|  | # -*- coding: utf-8 -*- | ||
|  | 
 | ||
|  | """
 | ||
|  | 配置文件创建工具 | ||
|  | 用于测试和创建默认配置文件 | ||
|  | """
 | ||
|  | 
 | ||
|  | import os | ||
|  | import sys | ||
|  | import configparser | ||
|  | from pathlib import Path | ||
|  | 
 | ||
|  | def create_default_config(): | ||
|  |     """创建默认配置文件""" | ||
|  |     # 确定配置文件位置 | ||
|  |     if getattr(sys, 'frozen', False): | ||
|  |         # 打包后的环境 | ||
|  |         app_dir = Path(sys.executable).parent | ||
|  |     else: | ||
|  |         # 开发环境 | ||
|  |         app_dir = Path(__file__).parent.parent.parent | ||
|  |      | ||
|  |     config_file = app_dir / "config.ini" | ||
|  |      | ||
|  |     print(f"创建配置文件: {config_file}") | ||
|  |      | ||
|  |     # 创建配置 | ||
|  |     config = configparser.ConfigParser() | ||
|  |      | ||
|  |     # 数据库配置 | ||
|  |     config['database'] = { | ||
|  |         'username': 'root', | ||
|  |         'password': 'root', | ||
|  |         'host': 'localhost', | ||
|  |         'port': '3306', | ||
|  |         'database': 'vwed_task' | ||
|  |     } | ||
|  |      | ||
|  |     # API配置 | ||
|  |     config['api'] = { | ||
|  |         'tf_api_base_url': 'http://111.231.146.230:4080/jeecg-boot' | ||
|  |     } | ||
|  |      | ||
|  |     # 服务配置 | ||
|  |     config['service'] = { | ||
|  |         'port': '8000', | ||
|  |         'host': '0.0.0.0' | ||
|  |     } | ||
|  |      | ||
|  |     # 写入配置文件 | ||
|  |     try: | ||
|  |         with open(str(config_file), 'w', encoding='utf-8') as f: | ||
|  |             config.write(f) | ||
|  |          | ||
|  |         print("✓ 配置文件创建成功") | ||
|  |          | ||
|  |         # 显示配置内容 | ||
|  |         print("\n配置内容:") | ||
|  |         with open(str(config_file), 'r', encoding='utf-8') as f: | ||
|  |             print(f.read()) | ||
|  |          | ||
|  |         return True | ||
|  |     except Exception as e: | ||
|  |         print(f"✗ 配置文件创建失败: {e}") | ||
|  |         return False | ||
|  | 
 | ||
|  | def read_config(): | ||
|  |     """读取配置文件""" | ||
|  |     if getattr(sys, 'frozen', False): | ||
|  |         app_dir = Path(sys.executable).parent | ||
|  |     else: | ||
|  |         app_dir = Path(__file__).parent.parent.parent | ||
|  |      | ||
|  |     config_file = app_dir / "config.ini" | ||
|  |      | ||
|  |     if not config_file.exists(): | ||
|  |         print(f"配置文件不存在: {config_file}") | ||
|  |         return False | ||
|  |      | ||
|  |     try: | ||
|  |         config = configparser.ConfigParser() | ||
|  |         config.read(str(config_file), encoding='utf-8') | ||
|  |          | ||
|  |         print(f"读取配置文件: {config_file}") | ||
|  |          | ||
|  |         # 显示所有配置 | ||
|  |         for section_name in config.sections(): | ||
|  |             print(f"\n[{section_name}]") | ||
|  |             for key, value in config[section_name].items(): | ||
|  |                 print(f"{key} = {value}") | ||
|  |          | ||
|  |         return True | ||
|  |     except Exception as e: | ||
|  |         print(f"读取配置文件失败: {e}") | ||
|  |         return False | ||
|  | 
 | ||
|  | def test_config_loading(): | ||
|  |     """测试配置加载""" | ||
|  |     print("测试配置加载...") | ||
|  |      | ||
|  |     try: | ||
|  |         from startup_loader import load_startup_config | ||
|  |          | ||
|  |         result = load_startup_config() | ||
|  |         if result: | ||
|  |             print("✓ 配置加载成功") | ||
|  |              | ||
|  |             # 显示环境变量 | ||
|  |             env_vars = ['DB_USER', 'DB_PASSWORD', 'DB_HOST', 'TF_API_BASE_URL'] | ||
|  |             print("\n环境变量:") | ||
|  |             for var in env_vars: | ||
|  |                 value = os.environ.get(var, '未设置') | ||
|  |                 print(f"{var} = {value}") | ||
|  |              | ||
|  |             return True | ||
|  |         else: | ||
|  |             print("✗ 配置加载失败") | ||
|  |             return False | ||
|  |     except ImportError as e: | ||
|  |         print(f"✗ 无法导入配置加载器: {e}") | ||
|  |         return False | ||
|  |     except Exception as e: | ||
|  |         print(f"✗ 配置加载测试异常: {e}") | ||
|  |         return False | ||
|  | 
 | ||
|  | def main(): | ||
|  |     """主函数""" | ||
|  |     print("VWED配置工具测试") | ||
|  |     print("=" * 40) | ||
|  |      | ||
|  |     import argparse | ||
|  |     parser = argparse.ArgumentParser(description="配置文件管理工具") | ||
|  |     parser.add_argument("--create", action="store_true", help="创建默认配置文件") | ||
|  |     parser.add_argument("--read", action="store_true", help="读取配置文件") | ||
|  |     parser.add_argument("--test", action="store_true", help="测试配置加载") | ||
|  |     parser.add_argument("--all", action="store_true", help="执行所有操作") | ||
|  |      | ||
|  |     args = parser.parse_args() | ||
|  |      | ||
|  |     if args.all or len(sys.argv) == 1: | ||
|  |         # 执行所有操作 | ||
|  |         operations = [ | ||
|  |             ("创建配置文件", create_default_config), | ||
|  |             ("读取配置文件", read_config), | ||
|  |             ("测试配置加载", test_config_loading) | ||
|  |         ] | ||
|  |     else: | ||
|  |         operations = [] | ||
|  |         if args.create: | ||
|  |             operations.append(("创建配置文件", create_default_config)) | ||
|  |         if args.read: | ||
|  |             operations.append(("读取配置文件", read_config)) | ||
|  |         if args.test: | ||
|  |             operations.append(("测试配置加载", test_config_loading)) | ||
|  |      | ||
|  |     results = [] | ||
|  |     for op_name, op_func in operations: | ||
|  |         print(f"\n=== {op_name} ===") | ||
|  |         try: | ||
|  |             result = op_func() | ||
|  |             results.append((op_name, result)) | ||
|  |         except Exception as e: | ||
|  |             print(f"操作失败: {e}") | ||
|  |             results.append((op_name, False)) | ||
|  |      | ||
|  |     # 总结 | ||
|  |     if results: | ||
|  |         print("\n" + "=" * 40) | ||
|  |         print("操作结果:") | ||
|  |         all_success = True | ||
|  |         for op_name, result in results: | ||
|  |             status = "✓" if result else "✗" | ||
|  |             print(f"{status} {op_name}") | ||
|  |             if not result: | ||
|  |                 all_success = False | ||
|  |          | ||
|  |         return 0 if all_success else 1 | ||
|  |     else: | ||
|  |         print("没有执行任何操作") | ||
|  |         return 0 | ||
|  | 
 | ||
|  | if __name__ == "__main__": | ||
|  |     exit(main()) |