refactor: remove metadata generation and add configurable output directory
- Remove vba_metadata.json generation logic to simplify extraction process - Add VBA_OUTPUT_DIR configuration to allow custom output path - Update output directory priority: parameter > env var > source file directory - Clean up unused imports and remove _save_metadata method Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,10 +6,9 @@ VBA代码提取工具
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Tuple, Optional
|
||||
from typing import Tuple, Dict
|
||||
|
||||
# 加载 .env 配置文件
|
||||
try:
|
||||
@@ -22,6 +21,8 @@ except ImportError:
|
||||
# ==================== 配置区域 ====================
|
||||
# 从 .env 文件读取配置,如果未设置则使用 None(交互模式)
|
||||
TARGET_XLSM_FILE = os.getenv("TARGET_XLSM_FILE", "").strip() or None
|
||||
# VBA代码输出目录(如果未设置,则使用源文件同目录下的VBA文件夹)
|
||||
VBA_OUTPUT_DIR = os.getenv("VBA_OUTPUT_DIR", "").strip() or None
|
||||
# =================================================
|
||||
|
||||
# VBA项目相关常量
|
||||
@@ -29,32 +30,32 @@ STANDARD_MODULE_DIR = "Modules"
|
||||
CLASS_MODULE_DIR = "ClassModules"
|
||||
DOCUMENT_MODULE_DIR = "DocumentModules"
|
||||
FORMS_DIR = "Forms"
|
||||
METADATA_FILE = "vba_metadata.json"
|
||||
|
||||
|
||||
class VBAExtractor:
|
||||
"""VBA代码提取器"""
|
||||
|
||||
def __init__(self, xlsm_path: str, output_dir: str = None, use_same_dir: bool = True):
|
||||
def __init__(self, xlsm_path: str, output_dir: str = None):
|
||||
"""
|
||||
初始化VBA提取器
|
||||
|
||||
Args:
|
||||
xlsm_path: xlsm文件路径
|
||||
output_dir: 输出目录(当use_same_dir=False时有效)
|
||||
use_same_dir: 是否使用目标文件同目录下的VBA文件夹,默认True
|
||||
output_dir: 输出目录(如果未指定,则使用VBA_OUTPUT_DIR配置或源文件同目录)
|
||||
"""
|
||||
self.xlsm_path = Path(xlsm_path)
|
||||
|
||||
if use_same_dir:
|
||||
# 确定输出目录的优先级:
|
||||
# 1. 参数指定的 output_dir
|
||||
# 2. 环境变量配置的 VBA_OUTPUT_DIR
|
||||
# 3. 默认:源文件同目录下的VBA文件夹
|
||||
if output_dir is not None:
|
||||
self.output_dir = Path(output_dir)
|
||||
elif VBA_OUTPUT_DIR is not None:
|
||||
self.output_dir = Path(VBA_OUTPUT_DIR)
|
||||
else:
|
||||
# 使用目标文件同目录下的VBA文件夹
|
||||
self.output_dir = self.xlsm_path.parent / "VBA"
|
||||
elif output_dir is None:
|
||||
# 使用脚本所在目录(项目根目录)下的VBA文件夹
|
||||
script_dir = Path(__file__).parent
|
||||
self.output_dir = script_dir / "VBA"
|
||||
else:
|
||||
self.output_dir = Path(output_dir)
|
||||
|
||||
# 创建输出目录结构
|
||||
self.modules_dir = self.output_dir / STANDARD_MODULE_DIR
|
||||
@@ -67,12 +68,6 @@ class VBAExtractor:
|
||||
self.document_modules_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.forms_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 存储模块元数据
|
||||
self.metadata = {
|
||||
"source_file": str(self.xlsm_path),
|
||||
"modules": {}
|
||||
}
|
||||
|
||||
def parse_attributes(self, code: str) -> Tuple[Dict[str, str], str]:
|
||||
"""
|
||||
解析VBA代码中的Attribute信息
|
||||
@@ -145,14 +140,10 @@ class VBAExtractor:
|
||||
|
||||
vba_parser.close()
|
||||
|
||||
# 保存元数据文件
|
||||
self._save_metadata()
|
||||
|
||||
print(f"\n提取完成!")
|
||||
print(f"- 标准模块: {self.modules_dir}")
|
||||
print(f"- 类模块: {self.class_modules_dir}")
|
||||
print(f"- 文档模块: {self.document_modules_dir}")
|
||||
print(f"- 元数据: {self.output_dir / METADATA_FILE}")
|
||||
return True
|
||||
else:
|
||||
print("未在文件中发现VBA代码")
|
||||
@@ -235,14 +226,10 @@ class VBAExtractor:
|
||||
workbook.Close(False)
|
||||
excel.Quit()
|
||||
|
||||
# 保存元数据文件
|
||||
self._save_metadata()
|
||||
|
||||
print(f"\n提取完成!")
|
||||
print(f"- 标准模块: {self.modules_dir}")
|
||||
print(f"- 类模块: {self.class_modules_dir}")
|
||||
print(f"- 文档模块: {self.document_modules_dir}")
|
||||
print(f"- 元数据: {self.output_dir / METADATA_FILE}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
@@ -338,22 +325,8 @@ class VBAExtractor:
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(clean_code)
|
||||
|
||||
# 保存元数据
|
||||
self.metadata["modules"][clean_name] = {
|
||||
"name": module_name,
|
||||
"type": module_type,
|
||||
"attributes": attributes,
|
||||
"file": str(file_path.relative_to(self.output_dir))
|
||||
}
|
||||
|
||||
print(f" [OK] 已保存: {clean_name} ({module_type})")
|
||||
|
||||
def _save_metadata(self):
|
||||
"""保存元数据到JSON文件"""
|
||||
metadata_path = self.output_dir / METADATA_FILE
|
||||
with open(metadata_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.metadata, f, indent=2, ensure_ascii=False)
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
@@ -417,7 +390,8 @@ def main():
|
||||
print(f"选择文件: {xlsm_file.name}")
|
||||
print()
|
||||
|
||||
# 创建提取器(使用默认参数,输出到目标文件同目录下的VBA文件夹)
|
||||
# 创建提取器
|
||||
# 输出目录优先级: 1. .env中的VBA_OUTPUT_DIR配置 2. 源文件同目录下的VBA文件夹
|
||||
extractor = VBAExtractor(str(xlsm_file))
|
||||
|
||||
# 显示输出目录信息
|
||||
|
||||
Reference in New Issue
Block a user