Skip to content

Commit ae74205

Browse files
seiya-kojiclaude
andcommitted
refactor(_utils): add dump_frontmatter helper
Centralize skill/command frontmatter YAML serialization into a single _utils.dump_frontmatter helper so no call site can drop allow_unicode or diverge on formatting. Route the 7 existing sites through it and drop a now-unused local yaml import. Switch the extension test fixtures to yaml.safe_dump for parity with the production safe-dump/safe-load codepaths. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 99649c3 commit ae74205

5 files changed

Lines changed: 27 additions & 26 deletions

File tree

‎src/specify_cli/_utils.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import stat
99
import subprocess
1010
import tempfile
11+
import yaml
1112
from pathlib import Path
1213
from typing import Any
1314
from ._console import console
@@ -16,6 +17,16 @@
1617
CLAUDE_NPM_LOCAL_PATH = Path.home() / ".claude" / "local" / "node_modules" / ".bin" / "claude"
1718

1819

20+
def dump_frontmatter(data: dict[str, Any]) -> str:
21+
"""Serialize skill/command frontmatter to a YAML string.
22+
23+
Centralizes the dump options used for SKILL.md frontmatter: ``allow_unicode``
24+
preserves Unicode descriptions and ``sort_keys=False`` keeps key order, so no
25+
call site can silently drop either.
26+
"""
27+
return yaml.safe_dump(data, sort_keys=False, allow_unicode=True).strip()
28+
29+
1930
def run_command(cmd: list[str], check_return: bool = True, capture: bool = False, shell: bool = False) -> str | None:
2031
"""Run a shell command and optionally capture output."""
2132
try:

‎src/specify_cli/extensions.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from .catalogs import CatalogEntry as BaseCatalogEntry, CatalogStackBase
2929
from ._init_options import is_ai_skills_enabled
30+
from ._utils import dump_frontmatter
3031

3132
_FALLBACK_CORE_COMMAND_NAMES = frozenset({
3233
"analyze",
@@ -969,7 +970,6 @@ def _register_extension_skills(
969970
from . import load_init_options
970971
from .agents import CommandRegistrar
971972
from .integrations import get_integration
972-
import yaml
973973

974974
written: List[str] = []
975975
opts = load_init_options(self.project_root)
@@ -1053,9 +1053,7 @@ def _register_extension_skills(
10531053
description,
10541054
f"extension:{manifest.id}",
10551055
)
1056-
frontmatter_text = yaml.safe_dump(
1057-
frontmatter_data, sort_keys=False, allow_unicode=True
1058-
).strip()
1056+
frontmatter_text = dump_frontmatter(frontmatter_data)
10591057

10601058
# Derive a human-friendly title from the command name
10611059
short_name = cmd_name

‎src/specify_cli/integrations/claude/__init__.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
from pathlib import Path
66
from typing import Any
77

8-
import yaml
9-
108
from ..base import SkillsIntegration
119
from ..manifest import IntegrationManifest
10+
from ..._utils import dump_frontmatter
1211

1312
# Mapping of command template stem → argument-hint text shown inline
1413
# when a user invokes the slash command in Claude Code.
@@ -103,9 +102,7 @@ def _render_skill(self, template_name: str, frontmatter: dict[str, Any], body: s
103102
skill_frontmatter = self._build_skill_fm(
104103
skill_name, description, f"templates/commands/{template_name}.md"
105104
)
106-
frontmatter_text = yaml.safe_dump(
107-
skill_frontmatter, sort_keys=False, allow_unicode=True
108-
).strip()
105+
frontmatter_text = dump_frontmatter(skill_frontmatter)
109106
return f"---\n{frontmatter_text}\n---\n\n{body.strip()}\n"
110107

111108
def _build_skill_fm(self, name: str, description: str, source: str) -> dict:

‎src/specify_cli/presets.py‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .extensions import REINSTALL_COMMAND, ExtensionRegistry, normalize_priority
3131
from .integrations.base import IntegrationBase
3232
from ._init_options import is_ai_skills_enabled
33+
from ._utils import dump_frontmatter
3334

3435

3536
def _substitute_core_template(
@@ -1068,7 +1069,7 @@ def _reconcile_skills(self, command_names: List[str]) -> None:
10681069
skill_name, desc,
10691070
f"override:{cmd_name}",
10701071
)
1071-
fm_text = yaml.safe_dump(fm_data, sort_keys=False, allow_unicode=True).strip()
1072+
fm_text = dump_frontmatter(fm_data)
10721073
skill_title = self._skill_title_from_command(cmd_name)
10731074
skill_content = (
10741075
f"---\n{fm_text}\n---\n\n"
@@ -1345,9 +1346,7 @@ def _register_skills(
13451346
enhanced_desc,
13461347
f"preset:{manifest.id}",
13471348
)
1348-
frontmatter_text = yaml.safe_dump(
1349-
frontmatter_data, sort_keys=False, allow_unicode=True
1350-
).strip()
1349+
frontmatter_text = dump_frontmatter(frontmatter_data)
13511350
skill_content = (
13521351
f"---\n"
13531352
f"{frontmatter_text}\n"
@@ -1443,9 +1442,7 @@ def _unregister_skills(self, skill_names: List[str], preset_dir: Path) -> None:
14431442
enhanced_desc,
14441443
f"templates/commands/{short_name}.md",
14451444
)
1446-
frontmatter_text = yaml.safe_dump(
1447-
frontmatter_data, sort_keys=False, allow_unicode=True
1448-
).strip()
1445+
frontmatter_text = dump_frontmatter(frontmatter_data)
14491446
skill_title = self._skill_title_from_command(short_name)
14501447
skill_content = (
14511448
f"---\n"
@@ -1482,9 +1479,7 @@ def _unregister_skills(self, skill_names: List[str], preset_dir: Path) -> None:
14821479
frontmatter.get("description", f"Extension command: {command_name}"),
14831480
extension_restore["source"],
14841481
)
1485-
frontmatter_text = yaml.safe_dump(
1486-
frontmatter_data, sort_keys=False, allow_unicode=True
1487-
).strip()
1482+
frontmatter_text = dump_frontmatter(frontmatter_data)
14881483
skill_content = (
14891484
f"---\n"
14901485
f"{frontmatter_text}\n"
@@ -3282,7 +3277,7 @@ def _parse_fm_yaml(fm_block: str) -> dict:
32823277
if top_fm:
32833278
top_frontmatter_text = (
32843279
"---\n"
3285-
+ yaml.safe_dump(top_fm, sort_keys=False, allow_unicode=True).strip()
3280+
+ dump_frontmatter(top_fm)
32863281
+ "\n---"
32873282
)
32883283
else:

‎tests/test_extension_skills.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _create_extension_dir(temp_dir: Path, ext_id: str = "test-ext") -> Path:
9090
}
9191

9292
with open(ext_dir / "extension.yml", "w") as f:
93-
yaml.dump(manifest_data, f)
93+
yaml.safe_dump(manifest_data, f)
9494

9595
commands_dir = ext_dir / "commands"
9696
commands_dir.mkdir()
@@ -146,7 +146,7 @@ def _create_unicode_extension_dir(temp_dir: Path, ext_id: str = "uni-ext") -> Pa
146146
}
147147

148148
with open(ext_dir / "extension.yml", "w", encoding="utf-8") as f:
149-
yaml.dump(manifest_data, f, allow_unicode=True)
149+
yaml.safe_dump(manifest_data, f, allow_unicode=True)
150150

151151
commands_dir = ext_dir / "commands"
152152
commands_dir.mkdir()
@@ -619,7 +619,7 @@ def test_skill_registration_resolves_script_placeholders(self, project_dir, temp
619619
},
620620
}
621621
with open(ext_dir / "extension.yml", "w") as f:
622-
yaml.dump(manifest_data, f)
622+
yaml.safe_dump(manifest_data, f)
623623

624624
(ext_dir / "commands").mkdir()
625625
(ext_dir / "commands" / "plan.md").write_text(
@@ -674,7 +674,7 @@ def test_missing_command_file_skipped(self, skills_project, temp_dir):
674674
},
675675
}
676676
with open(ext_dir / "extension.yml", "w") as f:
677-
yaml.dump(manifest_data, f)
677+
yaml.safe_dump(manifest_data, f)
678678

679679
(ext_dir / "commands").mkdir()
680680
(ext_dir / "commands" / "exists.md").write_text(
@@ -1230,7 +1230,7 @@ def test_command_without_frontmatter(self, skills_project, temp_dir):
12301230
},
12311231
}
12321232
with open(ext_dir / "extension.yml", "w") as f:
1233-
yaml.dump(manifest_data, f)
1233+
yaml.safe_dump(manifest_data, f)
12341234

12351235
(ext_dir / "commands").mkdir()
12361236
(ext_dir / "commands" / "plain.md").write_text(
@@ -1317,7 +1317,7 @@ def test_malformed_frontmatter_handled(self, skills_project, temp_dir):
13171317
},
13181318
}
13191319
with open(ext_dir / "extension.yml", "w") as f:
1320-
yaml.dump(manifest_data, f)
1320+
yaml.safe_dump(manifest_data, f)
13211321

13221322
(ext_dir / "commands").mkdir()
13231323
# Malformed YAML: invalid key-value syntax

0 commit comments

Comments
 (0)