mirror of
https://github.com/sun-guannan/CapCutAPI.git
synced 2025-11-25 03:15:00 +08:00
113 lines
4.0 KiB
Python
113 lines
4.0 KiB
Python
"""草稿文件夹管理器"""
|
|
|
|
import os
|
|
import shutil
|
|
|
|
from typing import List
|
|
|
|
from .script_file import Script_file
|
|
|
|
class Draft_folder:
|
|
"""管理一个文件夹及其内的一系列草稿"""
|
|
|
|
folder_path: str
|
|
"""根路径"""
|
|
|
|
def __init__(self, folder_path: str):
|
|
"""初始化草稿文件夹管理器
|
|
|
|
Args:
|
|
folder_path (`str`): 包含若干草稿的文件夹, 一般取剪映保存草稿的位置即可
|
|
|
|
Raises:
|
|
`FileNotFoundError`: 路径不存在
|
|
"""
|
|
self.folder_path = folder_path
|
|
|
|
if not os.path.exists(self.folder_path):
|
|
raise FileNotFoundError(f"根文件夹 {self.folder_path} 不存在")
|
|
|
|
def list_drafts(self) -> List[str]:
|
|
"""列出文件夹中所有草稿的名称
|
|
|
|
注意: 本函数只是如实地列出子文件夹的名称, 并不检查它们是否符合草稿的格式
|
|
"""
|
|
return [f for f in os.listdir(self.folder_path) if os.path.isdir(os.path.join(self.folder_path, f))]
|
|
|
|
def remove(self, draft_name: str) -> None:
|
|
"""删除指定名称的草稿
|
|
|
|
Args:
|
|
draft_name (`str`): 草稿名称, 即相应文件夹名称
|
|
|
|
Raises:
|
|
`FileNotFoundError`: 对应的草稿不存在
|
|
"""
|
|
draft_path = os.path.join(self.folder_path, draft_name)
|
|
if not os.path.exists(draft_path):
|
|
raise FileNotFoundError(f"草稿文件夹 {draft_name} 不存在")
|
|
|
|
shutil.rmtree(draft_path)
|
|
|
|
def inspect_material(self, draft_name: str) -> None:
|
|
"""输出指定名称草稿中的贴纸素材元数据
|
|
|
|
Args:
|
|
draft_name (`str`): 草稿名称, 即相应文件夹名称
|
|
|
|
Raises:
|
|
`FileNotFoundError`: 对应的草稿不存在
|
|
"""
|
|
draft_path = os.path.join(self.folder_path, draft_name)
|
|
if not os.path.exists(draft_path):
|
|
raise FileNotFoundError(f"草稿文件夹 {draft_name} 不存在")
|
|
|
|
script_file = self.load_template(draft_name)
|
|
script_file.inspect_material()
|
|
|
|
def load_template(self, draft_name: str) -> Script_file:
|
|
"""在文件夹中打开一个草稿作为模板, 并在其上进行编辑
|
|
|
|
Args:
|
|
draft_name (`str`): 草稿名称, 即相应文件夹名称
|
|
|
|
Returns:
|
|
`Script_file`: 以模板模式打开的草稿对象
|
|
|
|
Raises:
|
|
`FileNotFoundError`: 对应的草稿不存在
|
|
"""
|
|
draft_path = os.path.join(self.folder_path, draft_name)
|
|
if not os.path.exists(draft_path):
|
|
raise FileNotFoundError(f"草稿文件夹 {draft_name} 不存在")
|
|
|
|
return Script_file.load_template(os.path.join(draft_path, "draft_info.json"))
|
|
|
|
def duplicate_as_template(self, template_name: str, new_draft_name: str, allow_replace: bool = False) -> Script_file:
|
|
"""复制一份给定的草稿, 并在复制出的新草稿上进行编辑
|
|
|
|
Args:
|
|
template_name (`str`): 原草稿名称
|
|
new_draft_name (`str`): 新草稿名称
|
|
allow_replace (`bool`, optional): 是否允许覆盖与`new_draft_name`重名的草稿. 默认为否.
|
|
|
|
Returns:
|
|
`Script_file`: 以模板模式打开的**复制后的**草稿对象
|
|
|
|
Raises:
|
|
`FileNotFoundError`: 原始草稿不存在
|
|
`FileExistsError`: 已存在与`new_draft_name`重名的草稿, 但不允许覆盖.
|
|
"""
|
|
template_path = os.path.join(self.folder_path, template_name)
|
|
new_draft_path = os.path.join(self.folder_path, new_draft_name)
|
|
if not os.path.exists(template_path):
|
|
raise FileNotFoundError(f"模板草稿 {template_name} 不存在")
|
|
if os.path.exists(new_draft_path) and not allow_replace:
|
|
raise FileExistsError(f"新草稿 {new_draft_name} 已存在且不允许覆盖")
|
|
|
|
# 复制草稿文件夹
|
|
shutil.copytree(template_path, new_draft_path, dirs_exist_ok=allow_replace)
|
|
|
|
# 打开草稿
|
|
return self.load_template(new_draft_name)
|