-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathauto_sync.py
More file actions
81 lines (65 loc) · 2.3 KB
/
Copy pathauto_sync.py
File metadata and controls
81 lines (65 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Auto-sync configuration and orchestration."""
import shutil
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Optional
import yaml
CONFIG_DIR = Path.home() / ".config" / "droidctx"
CONFIG_FILE = CONFIG_DIR / "auto-sync.yaml"
LOG_FILE = CONFIG_DIR / "auto-sync.log"
def load_config() -> dict[str, Any]:
"""Load auto-sync config from disk. Returns empty dict if missing."""
if not CONFIG_FILE.exists():
return {}
return yaml.safe_load(CONFIG_FILE.read_text()) or {}
def save_config(config: dict[str, Any]) -> None:
"""Write auto-sync config to disk, creating directory if needed."""
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
CONFIG_FILE.write_text(yaml.dump(config, default_flow_style=False, sort_keys=False))
def resolve_paths(keyfile: Path, output_dir: Optional[Path]) -> tuple[Path, Path]:
"""Return (keyfile, output_dir) as absolute paths.
If output_dir is None, defaults to the keyfile's parent directory.
"""
keyfile = keyfile.resolve()
if output_dir is None:
output_dir = keyfile.parent.resolve()
else:
output_dir = output_dir.resolve()
return keyfile, output_dir
def find_droidctx_binary() -> Optional[str]:
"""Locate the droidctx executable on $PATH."""
return shutil.which("droidctx")
def get_last_run_time() -> Optional[str]:
"""Parse the log file and return the timestamp of the last run, or None."""
if not LOG_FILE.exists():
return None
try:
text = LOG_FILE.read_text().strip()
if not text:
return None
# Return the last non-empty line (most recent log entry)
for line in reversed(text.splitlines()):
line = line.strip()
if line:
return line
return None
except OSError:
return None
def build_config(
*,
keyfile: Path,
output_dir: Path,
interval_minutes: int,
droidctx_bin: str,
) -> dict[str, Any]:
"""Build a config dict ready to be saved."""
return {
"enabled": True,
"interval_minutes": interval_minutes,
"keyfile": str(keyfile),
"output_dir": str(output_dir),
"droidctx_bin": droidctx_bin,
"platform": sys.platform,
"created_at": datetime.now(timezone.utc).isoformat(),
}