返回 Social Auto Upload
arguments.py
根目录 / sau_commands / arguments.py
1 """CLI 共用的路径、标签和时间参数规则。"""
2 from __future__ import annotations
3 import argparse
4 import sys
5 from datetime import datetime
6 from pathlib import Path
7 from typing import Iterable
8 from conf import BASE_DIR
9
10 SCHEDULE_FORMAT = "%Y-%m-%d %H:%M"
11
12 def has_interactive_terminal() -> bool:
13 return sys.stdin.isatty() and sys.stdout.isatty()
14
15
16 def resolve_account_file(platform: str, account_name: str) -> Path:
17 account_file = Path(BASE_DIR) / "cookies" / f"{platform}_{account_name}.json"
18 account_file.parent.mkdir(exist_ok=True)
19 return account_file
20
21
22 def parse_tags(raw_tags: str | None) -> list[str]:
23 if not raw_tags:
24 return []
25
26 tags: list[str] = []
27 for item in raw_tags.split(","):
28 cleaned = item.strip().lstrip("#")
29 if cleaned:
30 tags.append(cleaned)
31 return tags
32
33
34 def parse_image_files(raw_files: Iterable[Path]) -> list[Path]:
35 return [Path(file) for file in raw_files]
36
37
38 def parse_schedule(raw_schedule: str | None) -> datetime | int:
39 if not raw_schedule:
40 return 0
41 return datetime.strptime(raw_schedule, SCHEDULE_FORMAT)
42
43
44 def existing_file_path(value: str) -> Path:
45 path = Path(value)
46 if not path.is_file():
47 raise argparse.ArgumentTypeError(f"File not found: {value}")
48 return path
49
50
51 def schedule_value(value: str):
52 try:
53 return parse_schedule(value)
54 except ValueError as exc:
55 raise argparse.ArgumentTypeError(
56 f"Invalid schedule '{value}'. Expected format: {SCHEDULE_FORMAT}"
57 ) from exc
58
59
60 def add_runtime_flags(parser: argparse.ArgumentParser) -> None:
61 parser.add_argument("--debug", action="store_true", help="Enable debug mode")
62 headless_group = parser.add_mutually_exclusive_group()
63 headless_group.add_argument("--headed", dest="headless", action="store_false", help="Run with browser UI")
64 headless_group.add_argument("--headless", dest="headless", action="store_true", help="Run in headless mode")
65 parser.set_defaults(headless=True)
66
66 lines PYTHON