refactor: 将核心代码迁移到 scripts 目录
- 创建 scripts/ 目录作为核心代码根目录 - 移动 core/, readers/, utils/ 到 scripts/ 下 - 移动 config.py, lyxy_document_reader.py 到 scripts/ - 移动 encoding_detection.py 到 scripts/utils/ - 更新 pyproject.toml 中的入口点路径和 pytest 配置 - 更新所有内部导入语句为 scripts.* 模块 - 更新 README.md 目录结构说明 - 更新 openspec/config.yaml 添加目录结构说明 - 删除无用的 main.py 此变更使项目结构更清晰,便于区分核心代码与测试、文档等支撑文件。
This commit is contained in:
21
scripts/utils/__init__.py
Normal file
21
scripts/utils/__init__.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""Utils module for lyxy-document."""
|
||||
|
||||
from .file_detection import (
|
||||
is_valid_docx,
|
||||
is_valid_pptx,
|
||||
is_valid_xlsx,
|
||||
is_valid_pdf,
|
||||
is_html_file,
|
||||
is_url,
|
||||
detect_file_type,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"is_valid_docx",
|
||||
"is_valid_pptx",
|
||||
"is_valid_xlsx",
|
||||
"is_valid_pdf",
|
||||
"is_html_file",
|
||||
"is_url",
|
||||
"detect_file_type",
|
||||
]
|
||||
62
scripts/utils/encoding_detection.py
Normal file
62
scripts/utils/encoding_detection.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""文件编码自动检测模块。"""
|
||||
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from scripts.config import Config
|
||||
|
||||
|
||||
def detect_encoding(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""
|
||||
检测文件编码。
|
||||
|
||||
Args:
|
||||
file_path: 文件路径
|
||||
|
||||
Returns:
|
||||
(encoding, error): 成功时返回 (编码名称, None),失败时返回 (None, 错误信息)
|
||||
"""
|
||||
try:
|
||||
import chardet
|
||||
except ImportError:
|
||||
return None, "chardet 库未安装"
|
||||
|
||||
try:
|
||||
with open(file_path, 'rb') as f:
|
||||
raw_data = f.read()
|
||||
result = chardet.detect(raw_data)
|
||||
return result['encoding'], None
|
||||
except Exception as e:
|
||||
return None, f"编码检测失败: {str(e)}"
|
||||
|
||||
|
||||
def read_text_file(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""
|
||||
读取文本文件,自动检测编码。
|
||||
|
||||
首先使用 chardet 检测编码,如果失败则尝试配置的回退编码列表。
|
||||
|
||||
Args:
|
||||
file_path: 文件路径
|
||||
|
||||
Returns:
|
||||
(content, error): 成功时返回 (文件内容, None),失败时返回 (None, 错误信息)
|
||||
"""
|
||||
# 尝试使用 chardet 检测编码
|
||||
encoding, error = detect_encoding(file_path)
|
||||
|
||||
if error:
|
||||
# chardet 失败,使用回退编码列表
|
||||
for enc in Config.FALLBACK_ENCODINGS:
|
||||
try:
|
||||
with open(file_path, 'r', encoding=enc) as f:
|
||||
return f.read(), None
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
return None, "无法识别文件编码"
|
||||
|
||||
# 使用检测到的编码读取文件
|
||||
try:
|
||||
with open(file_path, 'r', encoding=encoding) as f:
|
||||
return f.read(), None
|
||||
except Exception as e:
|
||||
return None, f"读取文件失败: {str(e)}"
|
||||
73
scripts/utils/file_detection.py
Normal file
73
scripts/utils/file_detection.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""文件类型检测模块,用于验证和检测输入文件类型。"""
|
||||
|
||||
import os
|
||||
import zipfile
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
def _is_valid_ooxml(file_path: str, required_files: List[str]) -> bool:
|
||||
"""验证 OOXML 格式文件(DOCX/PPTX/XLSX)"""
|
||||
try:
|
||||
with zipfile.ZipFile(file_path, "r") as zip_file:
|
||||
names = set(zip_file.namelist())
|
||||
return all(r in names for r in required_files)
|
||||
except (zipfile.BadZipFile, zipfile.LargeZipFile):
|
||||
return False
|
||||
|
||||
|
||||
_DOCX_REQUIRED = ["[Content_Types].xml", "_rels/.rels", "word/document.xml"]
|
||||
_PPTX_REQUIRED = ["[Content_Types].xml", "_rels/.rels", "ppt/presentation.xml"]
|
||||
_XLSX_REQUIRED = ["[Content_Types].xml", "_rels/.rels", "xl/workbook.xml"]
|
||||
|
||||
|
||||
def is_valid_docx(file_path: str) -> bool:
|
||||
"""验证文件是否为有效的 DOCX 格式"""
|
||||
return _is_valid_ooxml(file_path, _DOCX_REQUIRED)
|
||||
|
||||
|
||||
def is_valid_pptx(file_path: str) -> bool:
|
||||
"""验证文件是否为有效的 PPTX 格式"""
|
||||
return _is_valid_ooxml(file_path, _PPTX_REQUIRED)
|
||||
|
||||
|
||||
def is_valid_xlsx(file_path: str) -> bool:
|
||||
"""验证文件是否为有效的 XLSX 格式"""
|
||||
return _is_valid_ooxml(file_path, _XLSX_REQUIRED)
|
||||
|
||||
|
||||
def is_valid_pdf(file_path: str) -> bool:
|
||||
"""验证文件是否为有效的 PDF 格式"""
|
||||
try:
|
||||
with open(file_path, "rb") as f:
|
||||
header = f.read(4)
|
||||
return header == b"%PDF"
|
||||
except (IOError, OSError):
|
||||
return False
|
||||
|
||||
|
||||
def is_html_file(file_path: str) -> bool:
|
||||
"""判断文件是否为 HTML 文件(仅检查扩展名)"""
|
||||
ext = file_path.lower()
|
||||
return ext.endswith(".html") or ext.endswith(".htm")
|
||||
|
||||
|
||||
def is_url(input_str: str) -> bool:
|
||||
"""判断输入是否为 URL"""
|
||||
return input_str.startswith("http://") or input_str.startswith("https://")
|
||||
|
||||
|
||||
_FILE_TYPE_VALIDATORS = {
|
||||
".docx": is_valid_docx,
|
||||
".pptx": is_valid_pptx,
|
||||
".xlsx": is_valid_xlsx,
|
||||
".pdf": is_valid_pdf,
|
||||
}
|
||||
|
||||
|
||||
def detect_file_type(file_path: str) -> Optional[str]:
|
||||
"""检测文件类型,返回 'docx'、'pptx'、'xlsx' 或 'pdf'"""
|
||||
ext = os.path.splitext(file_path)[1].lower()
|
||||
validator = _FILE_TYPE_VALIDATORS.get(ext)
|
||||
if validator and validator(file_path):
|
||||
return ext.lstrip(".")
|
||||
return None
|
||||
Reference in New Issue
Block a user