Spaces:
Sleeping

MediaMaster-v2 / tools /base_tool.py
KeyBow's picture
Upload base_tool.py
fa9ab61 verified
Raw History Blame Contribute Delete
1.63 kB
"""
base_tool.py
共通ロジック:タスク状態管理・ZIP化・パス管理
"""
import os
import zipfile
from typing import Dict, TypedDict
# --- タスク状態の型定義 ---
class TaskState(TypedDict, total=False):
status: str # "processing" | "completed" | "failed"
progress: int # 0〜100
last_log: str # ユーザー向けの進捗メッセージ
error: str # エラー時のメッセージ
completed_at: float # 完了時刻(time.time())
# --- パス設定 ---
BASE_TEMP_DIR = "/tmp/media_master"
os.makedirs(BASE_TEMP_DIR, exist_ok=True)
# --- タスク状態管理(メモリ上) ---
# app.py からインポートして使うことで、全モジュールで同一インスタンスを共有する
tasks: Dict[str, TaskState] = {}
# --- パスヘルパー ---
def get_session_paths(task_id: str) -> tuple[str, str, str]:
"""(session_path, input_path, output_path) を返す"""
session_path = os.path.join(BASE_TEMP_DIR, task_id)
input_path = os.path.join(session_path, "input")
output_path = os.path.join(session_path, "output")
os.makedirs(output_path, exist_ok=True)
return session_path, input_path, output_path
# --- ZIP化 ---
def zip_output(session_path: str, output_path: str) -> str:
"""output_path 内のファイルをまとめて result.zip を作成し、そのパスを返す"""
final_zip = os.path.join(session_path, "result.zip")
with zipfile.ZipFile(final_zip, "w") as z:
for f in os.listdir(output_path):
z.write(os.path.join(output_path, f), f)
return final_zip