-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdap_paths.py
More file actions
129 lines (101 loc) · 3.6 KB
/
dap_paths.py
File metadata and controls
129 lines (101 loc) · 3.6 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from __future__ import annotations
from pathlib import Path
_HOME_MIGRATED = False
_PROJECT_MIGRATED: set[str] = set()
def _try_rename(src: Path, dst: Path) -> bool:
try:
if dst.exists():
return False
try:
dst.parent.mkdir(parents=True, exist_ok=True)
except Exception:
pass
src.rename(dst)
return True
except Exception:
return False
def _maybe_migrate_home() -> None:
global _HOME_MIGRATED
if _HOME_MIGRATED:
return
_HOME_MIGRATED = True
legacy = legacy_home_state_dir()
preferred = home_state_dir()
if legacy.exists() and not preferred.exists():
_try_rename(legacy, preferred)
def _maybe_migrate_project(project_root: Path) -> None:
try:
key = str(Path(project_root).resolve())
except Exception:
key = str(project_root)
if key in _PROJECT_MIGRATED:
return
_PROJECT_MIGRATED.add(key)
root = Path(project_root)
legacy_dir = legacy_project_state_dir(root)
preferred_dir = project_state_dir(root)
if legacy_dir.exists() and not preferred_dir.exists():
_try_rename(legacy_dir, preferred_dir)
mappings = [
(".aider.conf.yml", "DAP.conf.yml"),
(".aiderignore", "DAPignore"),
(".aider.input.history", "DAP.input.history"),
(".aider.chat.history.md", "DAP.chat.history.md"),
(".aider.model.settings.yml", "DAP.model.settings.yml"),
(".aider.model.metadata.json", "DAP.model.metadata.json"),
]
for legacy_name, preferred_name in mappings:
src = root / legacy_name
dst = root / preferred_name
if src.exists() and not dst.exists():
_try_rename(src, dst)
try:
for p in root.iterdir():
if p.is_dir() and p.name.startswith(".aider.tags.cache.v"):
dst = root / ("DAP.tags.cache.v" + p.name.split(".aider.tags.cache.v", 1)[-1])
_try_rename(p, dst)
except Exception:
return
APP_STATE_DIRNAME = "DAP"
LEGACY_STATE_DIRNAME = ".aider"
APP_PREFIX = "DAP"
LEGACY_PREFIX = ".aider"
def home_state_dir() -> Path:
return Path.home() / APP_STATE_DIRNAME
def legacy_home_state_dir() -> Path:
return Path.home() / LEGACY_STATE_DIRNAME
def project_state_dir(project_root: Path) -> Path:
return Path(project_root) / APP_STATE_DIRNAME
def legacy_project_state_dir(project_root: Path) -> Path:
return Path(project_root) / LEGACY_STATE_DIRNAME
def resolve_home_path(*parts: str) -> Path:
_maybe_migrate_home()
preferred = home_state_dir().joinpath(*parts)
legacy = legacy_home_state_dir().joinpath(*parts)
if preferred.exists():
return preferred
if legacy.exists():
return legacy
return preferred
def resolve_project_path(project_root: Path, *parts: str) -> Path:
_maybe_migrate_project(project_root)
preferred = project_state_dir(project_root).joinpath(*parts)
legacy = legacy_project_state_dir(project_root).joinpath(*parts)
if preferred.exists():
return preferred
if legacy.exists():
return legacy
return preferred
def resolve_project_file(project_root: Path, preferred_name: str, legacy_name: str) -> Path:
_maybe_migrate_project(project_root)
preferred = Path(project_root) / preferred_name
legacy = Path(project_root) / legacy_name
if preferred.exists():
return preferred
if legacy.exists():
return legacy
return preferred
def app_ignore_names() -> set[str]:
return {APP_STATE_DIRNAME, LEGACY_STATE_DIRNAME}
def app_prefixes() -> tuple[str, str]:
return (APP_PREFIX, LEGACY_PREFIX)