[M] refactor: move encryptConfig to package module

Move encryptConfig.py from root to src/config_man/encrypt_config.py,
update imports to use package path, and add console script entry point.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-20 16:48:15 +08:00
parent 292154621c
commit db20b739cd
2 changed files with 1 additions and 3 deletions

109
src/config_man/encrypt_config.py Executable file
View File

@@ -0,0 +1,109 @@
#!/usr/bin/env python3
"""
Config Encryptor Tool
读取原始的配置文件JSON格式生成加密后的配置文件。
"""
import json
import os
import sys
from pathlib import Path
from rich.console import Console
from rich.panel import Panel
from rich.syntax import Syntax
from config_man.utils.crypto import encrypt
# Initialize rich console
console = Console()
def load_raw_config(file_path):
"""Load a raw configuration file"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
config_data = json.load(f)
if not config_data:
console.print(f"[red]错误: 文件 {file_path} 为空或格式无效[/red]")
return None
return config_data
except FileNotFoundError:
console.print(f"[red]错误: 文件 {file_path} 不存在[/red]")
return None
except json.JSONDecodeError as e:
console.print(f"[red]错误: 文件 {file_path} JSON 格式无效: {str(e)}[/red]")
return None
except Exception as e:
console.print(f"[red]错误: 无法读取文件 {file_path}: {str(e)}[/red]")
return None
def save_encrypted_config(config_data, file_path):
"""Encrypt and save a configuration file"""
try:
# Convert config data to JSON string
config_json = json.dumps(config_data, ensure_ascii=False, indent=2)
# Encrypt the content
encrypted_content = encrypt(config_json)
# Save to file
with open(file_path, 'w', encoding='utf-8') as f:
f.write(encrypted_content)
console.print(f"[green]✅ 加密配置文件已保存到: {file_path}[/green]")
return True
except Exception as e:
console.print(f"[red]错误: 无法保存文件 {file_path}: {str(e)}[/red]")
return False
def main():
console.print(Panel("🔐 Config Encryptor Tool", expand=False, border_style="bold blue"))
# Get input file path
while True:
input_file = input("请输入要读取的原始配置文件路径 (如: config.json): ").strip()
if input_file and os.path.exists(input_file):
break
console.print("[red]❌ 文件不存在,请重新输入[/red]")
# Load the raw config file
console.print(f"[blue]正在读取配置文件: {input_file}[/blue]")
config_data = load_raw_config(input_file)
if config_data is None:
return 1
console.print("[green]✅ 配置文件读取成功[/green]")
# Display the config content
console.print(Panel("原始配置内容", title="📄 原始配置", border_style="cyan"))
console.print(Syntax(json.dumps(config_data, ensure_ascii=False, indent=2), "json", theme="monokai"))
# Get output file path
default_output = f"encrypted_{os.path.basename(input_file)}"
output_file = input(f"\n请输入输出文件路径 (默认: {default_output}): ").strip()
if not output_file:
output_file = default_output
# Save the encrypted config
console.print(f"[blue]正在加密并保存配置文件: {output_file}[/blue]")
if save_encrypted_config(config_data, output_file):
console.print(Panel("🎉 配置文件加密完成!", expand=False, border_style="bold green"))
return 0
else:
return 1
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
print("\n\n👋 已取消操作")
sys.exit(0)
except Exception as e:
print(f"\n❌ 程序执行出错: {str(e)}")
sys.exit(1)