- 添加.gitignore和.flaskenv环境配置文件 - 创建Flask应用基础架构,包括models、services、utils模块 - 配置数据库模型User和Setting,集成SQLAlchemy和Alembic迁移 - 添加前端Vue项目结构,包含Element Plus组件库 - 配置前后端API路由和蓝prints模块 - 实现书籍下载服务BookService功能模块 - 添加代码规范配置.editorconfig、.oxfmtrc.json、.oxlintrc.json - 配置VSCode推荐插件和前端构建工具链 - 实现文件服务FileService和相关业务逻辑
47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
import os
|
|
from typing import Optional, Callable, List
|
|
|
|
|
|
class FolderService:
|
|
def __init__(
|
|
self,
|
|
paths: List[str] | str,
|
|
ignore_dirs: Optional[set] = None,
|
|
folder_callback: Optional[Callable] = None,
|
|
file_callback: Optional[Callable] = None,
|
|
empty_folder_callback: Optional[Callable] = None,
|
|
max_depth: Optional[int] = None
|
|
):
|
|
self.paths: List[str] = paths if type(paths) is not str else [paths]
|
|
self.ignore_dirs = ignore_dirs or set()
|
|
self.folder_callback = folder_callback
|
|
self.file_callback = file_callback
|
|
self.empty_folder_callback = empty_folder_callback
|
|
self.max_depth = max_depth
|
|
|
|
def _traverse_folder(self, path: Optional[str] = None, depth: Optional[int] = 0) -> None:
|
|
"""处理目录"""
|
|
for entry in os.scandir(path):
|
|
if entry.name in self.ignore_dirs:
|
|
continue
|
|
if entry.is_dir():
|
|
# 判断要加载的文件夹深度
|
|
if self.max_depth is not None and depth >= self.max_depth:
|
|
return
|
|
# 循环加载
|
|
self._traverse_folder(entry.path, depth + 1)
|
|
# 判断是空文件夹
|
|
if not os.listdir(entry.path) and self.empty_folder_callback:
|
|
self.empty_folder_callback(entry)
|
|
# 文件夹处理的回调
|
|
if self.folder_callback:
|
|
self.folder_callback(entry)
|
|
elif entry.is_file():
|
|
if self.file_callback:
|
|
self.file_callback(entry)
|
|
|
|
def process_folder(self) -> None:
|
|
for path in self.paths:
|
|
if os.path.exists(path):
|
|
self._traverse_folder(path)
|