#!/usr/bin/env python3 """Настроенные режимы Focus в macOS.""" from __future__ import annotations import argparse import json import subprocess from pathlib import Path def read_json(path: Path) -> dict: if not path.exists(): return {} result = subprocess.run( ["plutil", "-convert", "json", "-o", "-", str(path)], capture_output=True, text=True, check=False, ) if result.returncode != 0: return {} try: data = json.loads(result.stdout) except json.JSONDecodeError: return {} return data if isinstance(data, dict) else {} def collect_names(node, names: set[str]) -> None: if isinstance(node, dict): for key, value in node.items(): if key == "name" and isinstance(value, str) and value.strip(): names.add(value.strip()) else: collect_names(value, names) elif isinstance(node, list): for item in node: collect_names(item, names) def count_assertions(node) -> int: if isinstance(node, dict): if isinstance(node.get("data"), list): return len(node["data"]) return sum(count_assertions(value) for value in node.values()) if isinstance(node, list): return sum(count_assertions(item) for item in node) return 0 def main() -> int: parser = argparse.ArgumentParser(description="Показать режимы фокуса") parser.add_argument("--json", type=Path) args = parser.parse_args() base = Path.home() / "Library" / "DoNotDisturb" / "DB" modes_data = read_json(base / "ModeConfigurations.json") assertions_data = read_json(base / "Assertions.json") names: set[str] = set() collect_names(modes_data, names) active = count_assertions(assertions_data) report = { "directory": str(base), "modes": sorted(names), "active_assertions": active, "mode_file": (base / "ModeConfigurations.json").exists(), "assertions_file": (base / "Assertions.json").exists(), } if args.json: args.json.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print(f"Каталог Focus: {base}") if not report["mode_file"] and not report["assertions_file"]: print("Файлы Focus не найдены. Возможно, режимы ещё не настраивались.") return 0 print(f"Настроенных режимов: {len(report['modes'])}.") for name in report["modes"]: print(f" {name}") print(f"Активных утверждений: {active}.") print("Скрипт не меняет режимы и не выключает уведомления.") return 0 if __name__ == "__main__": raise SystemExit(main())