pyaisync 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyaisync/__init__.py +3 -0
- pyaisync/cli.py +369 -0
- pyaisync/config.py +97 -0
- pyaisync/errors.py +5 -0
- pyaisync/frontmatter.py +42 -0
- pyaisync/importers/__init__.py +186 -0
- pyaisync/importers/common.py +54 -0
- pyaisync/importers/structured.py +879 -0
- pyaisync/loader.py +313 -0
- pyaisync/manifest.py +98 -0
- pyaisync/model.py +133 -0
- pyaisync/plugin.py +78 -0
- pyaisync/safefs.py +97 -0
- pyaisync/targets/__init__.py +62 -0
- pyaisync/targets/base.py +67 -0
- pyaisync/targets/claude.py +140 -0
- pyaisync/targets/cursor.py +154 -0
- pyaisync/targets/opencode.py +109 -0
- pyaisync/vocab.py +132 -0
- pyaisync/writer.py +356 -0
- pyaisync-0.1.0.dist-info/METADATA +365 -0
- pyaisync-0.1.0.dist-info/RECORD +25 -0
- pyaisync-0.1.0.dist-info/WHEEL +4 -0
- pyaisync-0.1.0.dist-info/entry_points.txt +2 -0
- pyaisync-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"""Import native configuration -> .ai/. Reads native files only; writes only to .ai/."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .. import frontmatter
|
|
9
|
+
from ..config import AI_DIR
|
|
10
|
+
from ..errors import PyAisyncError
|
|
11
|
+
from ..safefs import atomic_write, read_tree, safe_join, tree_modes
|
|
12
|
+
from .common import ImportReport, Item, read_native, slug
|
|
13
|
+
from .structured import agent_items, import_structured
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _rule_item(name: str, meta: dict[str, Any], body: str) -> Item:
|
|
17
|
+
out: dict[str, Any] = {}
|
|
18
|
+
if isinstance(meta.get("description"), str) and meta["description"]:
|
|
19
|
+
out["description"] = meta["description"]
|
|
20
|
+
globs = meta.get("globs")
|
|
21
|
+
if isinstance(globs, str):
|
|
22
|
+
globs = [g.strip() for g in globs.split(",") if g.strip()]
|
|
23
|
+
if isinstance(globs, list) and globs:
|
|
24
|
+
out["globs"] = [str(g) for g in globs]
|
|
25
|
+
if meta.get("always") is True or meta.get("alwaysApply") is True:
|
|
26
|
+
out["always"] = True
|
|
27
|
+
text = frontmatter.dump(out, body.strip("\n"))
|
|
28
|
+
return Item("rules", name, {f"rules/{name}.md": text.encode()})
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _command_item(name: str, meta: dict[str, Any], body: str) -> Item:
|
|
32
|
+
out = (
|
|
33
|
+
{"description": meta["description"]}
|
|
34
|
+
if isinstance(meta.get("description"), str) and meta["description"]
|
|
35
|
+
else {}
|
|
36
|
+
)
|
|
37
|
+
text = frontmatter.dump(out, body.strip("\n"))
|
|
38
|
+
return Item("commands", name, {f"commands/{name}.md": text.encode()})
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _collect_md(directory: Path, suffix: str, kind: str, rep: ImportReport) -> list[Item]:
|
|
42
|
+
items: list[Item] = []
|
|
43
|
+
if not directory.is_dir() or directory.is_symlink():
|
|
44
|
+
return items
|
|
45
|
+
for p in sorted(directory.iterdir()):
|
|
46
|
+
if p.suffix != suffix or p.is_symlink() or not p.is_file():
|
|
47
|
+
continue
|
|
48
|
+
name = slug(p.stem)
|
|
49
|
+
if name is None:
|
|
50
|
+
rep.warnings.append(f"{p.name}: invalid name, not imported")
|
|
51
|
+
continue
|
|
52
|
+
parsed = read_native(p, rep)
|
|
53
|
+
if parsed is None:
|
|
54
|
+
continue
|
|
55
|
+
meta, body = parsed
|
|
56
|
+
items.append(
|
|
57
|
+
_rule_item(name, meta, body) if kind == "rules" else _command_item(name, meta, body)
|
|
58
|
+
)
|
|
59
|
+
return items
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _collect_skills(directory: Path, rep: ImportReport) -> list[Item]:
|
|
63
|
+
items: list[Item] = []
|
|
64
|
+
if not directory.is_dir() or directory.is_symlink():
|
|
65
|
+
return items
|
|
66
|
+
for d in sorted(directory.iterdir()):
|
|
67
|
+
if d.name.startswith(".") or d.is_symlink() or not (d / "SKILL.md").is_file():
|
|
68
|
+
continue
|
|
69
|
+
name = slug(d.name)
|
|
70
|
+
if name is None:
|
|
71
|
+
rep.warnings.append(f"{d.name}: invalid skill name, not imported")
|
|
72
|
+
continue
|
|
73
|
+
try:
|
|
74
|
+
tree = read_tree(d)
|
|
75
|
+
except PyAisyncError as exc:
|
|
76
|
+
rep.warnings.append(str(exc))
|
|
77
|
+
continue
|
|
78
|
+
modes = tree_modes(d)
|
|
79
|
+
items.append(
|
|
80
|
+
Item(
|
|
81
|
+
"skills",
|
|
82
|
+
name,
|
|
83
|
+
{f"skills/{name}/{r}": b for r, b in tree.items()},
|
|
84
|
+
frozenset(f"skills/{name}/{r}" for r in modes),
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
return items
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _root_rule(path: Path, rep: ImportReport) -> list[Item]:
|
|
91
|
+
if not path.is_file() or path.is_symlink():
|
|
92
|
+
return []
|
|
93
|
+
parsed = read_native(path, rep)
|
|
94
|
+
if parsed is None:
|
|
95
|
+
return []
|
|
96
|
+
meta, body = parsed
|
|
97
|
+
meta = {"always": True, "description": meta.get("description", "")}
|
|
98
|
+
return [_rule_item("project", meta, body)]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _discover(root: Path, target: str, rep: ImportReport) -> list[Item]:
|
|
102
|
+
if target == "claude":
|
|
103
|
+
return (
|
|
104
|
+
_root_rule(root / "CLAUDE.md", rep)
|
|
105
|
+
+ _collect_md(root / ".claude/commands", ".md", "commands", rep)
|
|
106
|
+
+ _collect_skills(root / ".claude/skills", rep)
|
|
107
|
+
)
|
|
108
|
+
if target == "cursor":
|
|
109
|
+
return (
|
|
110
|
+
_collect_md(root / ".cursor/rules", ".mdc", "rules", rep)
|
|
111
|
+
+ _collect_md(root / ".cursor/commands", ".md", "commands", rep)
|
|
112
|
+
+ _collect_skills(root / ".cursor/skills", rep)
|
|
113
|
+
+ _collect_skills(root / ".agents/skills", rep)
|
|
114
|
+
)
|
|
115
|
+
if target == "opencode":
|
|
116
|
+
return (
|
|
117
|
+
_root_rule(root / "AGENTS.md", rep)
|
|
118
|
+
+ _collect_md(root / ".opencode/commands", ".md", "commands", rep)
|
|
119
|
+
+ _collect_md(root / ".opencode/command", ".md", "commands", rep)
|
|
120
|
+
+ _collect_skills(root / ".opencode/skills", rep)
|
|
121
|
+
+ _collect_skills(root / ".agents/skills", rep)
|
|
122
|
+
)
|
|
123
|
+
raise PyAisyncError(f"unknown target: {target}")
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _rename(item: Item, new: str) -> Item:
|
|
127
|
+
files = {}
|
|
128
|
+
for rel, data in item.files.items():
|
|
129
|
+
parts = rel.split("/")
|
|
130
|
+
if item.kind == "skills":
|
|
131
|
+
parts[1] = new
|
|
132
|
+
else:
|
|
133
|
+
parts[1] = f"{new}.md"
|
|
134
|
+
files["/".join(parts)] = data
|
|
135
|
+
renamed = {"/".join([*rel.split("/")[:1], new, *rel.split("/")[2:]]) for rel in item.executable}
|
|
136
|
+
return Item(item.kind, new, files, frozenset(renamed))
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def import_target(
|
|
140
|
+
root: Path, target: str, *, on_conflict: str = "skip", write: bool = True
|
|
141
|
+
) -> ImportReport:
|
|
142
|
+
if on_conflict not in ("skip", "rename", "overwrite"):
|
|
143
|
+
raise PyAisyncError("--on-conflict must be skip, rename or overwrite")
|
|
144
|
+
if not (root / AI_DIR / "config.toml").is_file():
|
|
145
|
+
raise PyAisyncError(".ai/config.toml not found: run 'pyaisync init' first")
|
|
146
|
+
rep = ImportReport()
|
|
147
|
+
items = _discover(root, target, rep)
|
|
148
|
+
items += agent_items(root, target, rep)
|
|
149
|
+
seen: set[tuple[str, str]] = set()
|
|
150
|
+
for item in items:
|
|
151
|
+
key = (item.kind, item.name)
|
|
152
|
+
if key in seen: # e.g. a skill duplicated in .cursor/skills and .agents/skills
|
|
153
|
+
continue
|
|
154
|
+
seen.add(key)
|
|
155
|
+
dest_exists = any(safe_join(root, f"{AI_DIR}/{r}").exists() for r in item.files)
|
|
156
|
+
same = dest_exists and all(
|
|
157
|
+
(root / AI_DIR / r).is_file() and (root / AI_DIR / r).read_bytes() == d
|
|
158
|
+
for r, d in item.files.items()
|
|
159
|
+
)
|
|
160
|
+
label = f"{item.kind}/{item.name}"
|
|
161
|
+
if same:
|
|
162
|
+
rep.unchanged.append(label)
|
|
163
|
+
continue
|
|
164
|
+
if dest_exists:
|
|
165
|
+
if on_conflict == "skip":
|
|
166
|
+
rep.skipped.append(
|
|
167
|
+
f"{label}: already exists in .ai/ (use --on-conflict rename|overwrite)"
|
|
168
|
+
)
|
|
169
|
+
continue
|
|
170
|
+
if on_conflict == "rename":
|
|
171
|
+
new = slug(f"{item.name}-{target}")
|
|
172
|
+
if new is None or any(
|
|
173
|
+
safe_join(root, f"{AI_DIR}/{r}").exists() for r in _rename(item, new).files
|
|
174
|
+
):
|
|
175
|
+
rep.skipped.append(f"{label}: could not rename without a collision")
|
|
176
|
+
continue
|
|
177
|
+
item = _rename(item, new)
|
|
178
|
+
label = f"{item.kind}/{item.name}"
|
|
179
|
+
for rel, data in item.files.items():
|
|
180
|
+
if write:
|
|
181
|
+
atomic_write(
|
|
182
|
+
safe_join(root, f"{AI_DIR}/{rel}"), data, executable=rel in item.executable
|
|
183
|
+
)
|
|
184
|
+
rep.written.append(label)
|
|
185
|
+
import_structured(root, target, rep, on_conflict, write)
|
|
186
|
+
return rep
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Shared types and helpers for importers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from .. import frontmatter
|
|
11
|
+
from ..errors import PyAisyncError
|
|
12
|
+
from ..model import NAME_RE
|
|
13
|
+
from ..safefs import read_text
|
|
14
|
+
from ..targets.base import GENERATED_MARK
|
|
15
|
+
|
|
16
|
+
_HEADER_RE = re.compile(r"^<!--\s*generated by pyaisync[^>]*-->\s*\n?", re.MULTILINE)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class ImportReport:
|
|
21
|
+
written: list[str] = field(default_factory=list)
|
|
22
|
+
unchanged: list[str] = field(default_factory=list)
|
|
23
|
+
skipped: list[str] = field(default_factory=list)
|
|
24
|
+
warnings: list[str] = field(default_factory=list)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class Item:
|
|
29
|
+
kind: str # rules | commands | skills | agents
|
|
30
|
+
name: str
|
|
31
|
+
files: dict[str, bytes] # path relative to .ai/ -> content
|
|
32
|
+
executable: frozenset[str] = frozenset() # paths (relative to .ai/) that keep +x
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def slug(raw: str) -> str | None:
|
|
36
|
+
name = re.sub(r"[^a-z0-9._-]+", "-", raw.lower()).strip("-.")
|
|
37
|
+
return name if NAME_RE.fullmatch(name) and ".." not in name else None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def read_native(path: Path, rep: ImportReport) -> tuple[dict[str, Any], str] | None:
|
|
41
|
+
try:
|
|
42
|
+
raw = read_text(path)
|
|
43
|
+
except PyAisyncError as exc:
|
|
44
|
+
rep.warnings.append(str(exc))
|
|
45
|
+
return None
|
|
46
|
+
if GENERATED_MARK in raw:
|
|
47
|
+
rep.skipped.append(f"{path.name}: was already generated by pyaisync (skipped)")
|
|
48
|
+
return None
|
|
49
|
+
try:
|
|
50
|
+
meta, body = frontmatter.parse(raw, str(path))
|
|
51
|
+
except PyAisyncError as exc:
|
|
52
|
+
rep.warnings.append(str(exc))
|
|
53
|
+
return None
|
|
54
|
+
return meta, _HEADER_RE.sub("", body)
|