womm-cli 4.0.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.
Files changed (218) hide show
  1. womm/__init__.py +53 -0
  2. womm/__main__.py +48 -0
  3. womm/_version.py +10 -0
  4. womm/assets/copier/meta/copier.yml +15 -0
  5. womm/assets/copier/meta/template/README.md.jinja +32 -0
  6. womm/assets/copier/meta/template/copier.yml.jinja +13 -0
  7. womm/assets/copier/meta/template/template/.gitkeep +0 -0
  8. womm/assets/copier/official/javascript/copier.yml +104 -0
  9. womm/assets/copier/official/javascript/template/.gitignore.jinja +35 -0
  10. womm/assets/copier/official/javascript/template/.nvmrc.jinja +1 -0
  11. womm/assets/copier/official/javascript/template/.prettierignore.jinja +10 -0
  12. womm/assets/copier/official/javascript/template/README.md.jinja +81 -0
  13. womm/assets/copier/official/javascript/template/biome.json.jinja +54 -0
  14. womm/assets/copier/official/javascript/template/eslint.config.mjs.jinja +51 -0
  15. womm/assets/copier/official/javascript/template/index.html.jinja +12 -0
  16. womm/assets/copier/official/javascript/template/package.json.jinja +88 -0
  17. womm/assets/copier/official/javascript/template/prettier.config.mjs.jinja +8 -0
  18. womm/assets/copier/official/javascript/template/src/App.jsx.jinja +10 -0
  19. womm/assets/copier/official/javascript/template/src/App.tsx.jinja +10 -0
  20. womm/assets/copier/official/javascript/template/src/App.vue.jinja +18 -0
  21. womm/assets/copier/official/javascript/template/src/index.jsx.jinja +10 -0
  22. womm/assets/copier/official/javascript/template/src/index.tsx.jinja +11 -0
  23. womm/assets/copier/official/javascript/template/src/main.js.jinja +21 -0
  24. womm/assets/copier/official/javascript/template/src/main.ts.jinja +21 -0
  25. womm/assets/copier/official/javascript/template/tsconfig.json.jinja +35 -0
  26. womm/assets/copier/official/javascript/template/vite.config.mjs.jinja +10 -0
  27. womm/assets/copier/official/javascript/template/{% if with_ci %}.github{% endif %}/workflows/ci.yml.jinja +63 -0
  28. womm/assets/copier/official/javascript/template/{% if with_tests %}tests{% endif %}/smoke.test.js.jinja +12 -0
  29. womm/assets/copier/official/javascript/template/{% if with_tests %}tests{% endif %}/smoke.test.ts.jinja +12 -0
  30. womm/assets/copier/official/javascript/template/{% if with_vscode %}.vscode{% endif %}/extensions.json.jinja +4 -0
  31. womm/assets/copier/official/javascript/template/{% if with_vscode %}.vscode{% endif %}/settings.json.jinja +27 -0
  32. womm/assets/copier/official/python/copier.yml +107 -0
  33. womm/assets/copier/official/python/template/.gitignore.jinja +40 -0
  34. womm/assets/copier/official/python/template/.pre-commit-config.yaml.jinja +19 -0
  35. womm/assets/copier/official/python/template/.python-version.jinja +1 -0
  36. womm/assets/copier/official/python/template/README.md.jinja +83 -0
  37. womm/assets/copier/official/python/template/pyproject.toml.jinja +114 -0
  38. womm/assets/copier/official/python/template/{% if framework == 'django' %}manage.py{% endif %}.jinja +23 -0
  39. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/__init__.py.jinja +3 -0
  40. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/asgi.py.jinja +9 -0
  41. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/__init__.py.jinja +0 -0
  42. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/apps.py.jinja +10 -0
  43. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/models.py.jinja +1 -0
  44. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/views.py.jinja +8 -0
  45. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/py.typed +0 -0
  46. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/settings.py.jinja +81 -0
  47. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/urls.py.jinja +11 -0
  48. womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/wsgi.py.jinja +9 -0
  49. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/__init__.py.jinja +3 -0
  50. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/asgi.py.jinja +9 -0
  51. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/__init__.py.jinja +0 -0
  52. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/apps.py.jinja +10 -0
  53. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/models.py.jinja +1 -0
  54. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/views.py.jinja +8 -0
  55. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/py.typed +0 -0
  56. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/settings.py.jinja +81 -0
  57. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/urls.py.jinja +11 -0
  58. womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/wsgi.py.jinja +9 -0
  59. womm/assets/copier/official/python/template/{% if with_ci %}.github{% endif %}/workflows/ci.yml.jinja +78 -0
  60. womm/assets/copier/official/python/template/{% if with_tests %}tests{% endif %}/test_smoke.py.jinja +16 -0
  61. womm/assets/copier/official/python/template/{% if with_vscode %}.vscode{% endif %}/extensions.json.jinja +8 -0
  62. womm/assets/copier/official/python/template/{% if with_vscode %}.vscode{% endif %}/settings.json.jinja +28 -0
  63. womm/bin/refresh_env.cmd +130 -0
  64. womm/cli.py +111 -0
  65. womm/commands/__init__.py +30 -0
  66. womm/commands/core/__init__.py +29 -0
  67. womm/commands/core/doctor.py +52 -0
  68. womm/commands/core/path.py +337 -0
  69. womm/commands/project/__init__.py +31 -0
  70. womm/commands/project/create.py +128 -0
  71. womm/commands/project/setup.py +438 -0
  72. womm/commands/project/template.py +160 -0
  73. womm/commands/system/__init__.py +31 -0
  74. womm/commands/system/context/__init__.py +82 -0
  75. womm/commands/system/context/backup.py +344 -0
  76. womm/commands/system/context/inventory.py +111 -0
  77. womm/commands/system/context/registration.py +290 -0
  78. womm/commands/system/deps.py +165 -0
  79. womm/commands/system/system.py +130 -0
  80. womm/exceptions/__init__.py +69 -0
  81. womm/exceptions/common/__init__.py +55 -0
  82. womm/exceptions/common/command_service.py +91 -0
  83. womm/exceptions/common/file_service.py +60 -0
  84. womm/exceptions/common/security_service.py +44 -0
  85. womm/exceptions/common/validation_service.py +70 -0
  86. womm/exceptions/context/__init__.py +27 -0
  87. womm/exceptions/context/context_service.py +55 -0
  88. womm/exceptions/project/__init__.py +29 -0
  89. womm/exceptions/project/project_service.py +59 -0
  90. womm/exceptions/system/__init__.py +25 -0
  91. womm/exceptions/system/system_service.py +53 -0
  92. womm/interfaces/__init__.py +71 -0
  93. womm/interfaces/context/__init__.py +34 -0
  94. womm/interfaces/context/menu_interface.py +686 -0
  95. womm/interfaces/context/registry_interface.py +562 -0
  96. womm/interfaces/context/script_detector_interface.py +208 -0
  97. womm/interfaces/context/script_registrar.py +264 -0
  98. womm/interfaces/core/__init__.py +26 -0
  99. womm/interfaces/core/doctor_interface.py +102 -0
  100. womm/interfaces/dependencies/__init__.py +28 -0
  101. womm/interfaces/dependencies/deps_interface.py +160 -0
  102. womm/interfaces/project/__init__.py +36 -0
  103. womm/interfaces/project/create_interface.py +162 -0
  104. womm/interfaces/project/detection_interface.py +181 -0
  105. womm/interfaces/project/manager_interface.py +99 -0
  106. womm/interfaces/project/setup_interface.py +477 -0
  107. womm/interfaces/project/template_store_interface.py +196 -0
  108. womm/interfaces/system/__init__.py +32 -0
  109. womm/interfaces/system/detector_interface.py +84 -0
  110. womm/interfaces/system/environment_interface.py +110 -0
  111. womm/interfaces/system/path_interface.py +570 -0
  112. womm/services/__init__.py +70 -0
  113. womm/services/common/__init__.py +33 -0
  114. womm/services/common/base_validation_service.py +450 -0
  115. womm/services/common/command_runner_service.py +647 -0
  116. womm/services/common/file_scanner_service.py +483 -0
  117. womm/services/common/security_validator_service.py +348 -0
  118. womm/services/context/__init__.py +37 -0
  119. womm/services/context/backup_entries.py +148 -0
  120. womm/services/context/parameters.py +515 -0
  121. womm/services/context/registry_paths.py +56 -0
  122. womm/services/context/registry_service.py +573 -0
  123. womm/services/context/validation_service.py +691 -0
  124. womm/services/dependencies/__init__.py +29 -0
  125. womm/services/dependencies/probe.py +102 -0
  126. womm/services/project/__init__.py +48 -0
  127. womm/services/project/copier_project_creation_service.py +111 -0
  128. womm/services/project/destination_guard.py +96 -0
  129. womm/services/project/detection_service.py +354 -0
  130. womm/services/project/env_utils.py +333 -0
  131. womm/services/project/javascript_environment_service.py +348 -0
  132. womm/services/project/python_environment_service.py +245 -0
  133. womm/services/project/setup_profile_resolver.py +146 -0
  134. womm/services/project/template_store_service.py +344 -0
  135. womm/services/project/validation_service.py +418 -0
  136. womm/services/system/__init__.py +32 -0
  137. womm/services/system/detector_service.py +704 -0
  138. womm/services/system/environment_service.py +494 -0
  139. womm/services/system/path_service.py +642 -0
  140. womm/shared/__init__.py +30 -0
  141. womm/shared/configs/__init__.py +51 -0
  142. womm/shared/configs/context/__init__.py +43 -0
  143. womm/shared/configs/context/context_config.py +125 -0
  144. womm/shared/configs/context/context_file_types_config.py +159 -0
  145. womm/shared/configs/context/context_limits_config.py +187 -0
  146. womm/shared/configs/context/context_paths_config.py +115 -0
  147. womm/shared/configs/context/context_types_config.py +65 -0
  148. womm/shared/configs/context/script_config.py +219 -0
  149. womm/shared/configs/dependencies/__init__.py +32 -0
  150. womm/shared/configs/dependencies/runtime_config.py +46 -0
  151. womm/shared/configs/dependencies/runtime_package_manager_config.py +48 -0
  152. womm/shared/configs/logging_config.py +89 -0
  153. womm/shared/configs/project/__init__.py +33 -0
  154. womm/shared/configs/project/project_config.py +109 -0
  155. womm/shared/configs/project/project_structure_config.py +88 -0
  156. womm/shared/configs/project/python_project_config.py +165 -0
  157. womm/shared/configs/security/__init__.py +26 -0
  158. womm/shared/configs/security/security_patterns_config.py +242 -0
  159. womm/shared/configs/system/__init__.py +28 -0
  160. womm/shared/configs/system/system_detector_config.py +210 -0
  161. womm/shared/configs/system/system_environment_config.py +101 -0
  162. womm/shared/models/__init__.py +13 -0
  163. womm/shared/models/template_source.py +65 -0
  164. womm/shared/paths.py +192 -0
  165. womm/shared/results/__init__.py +138 -0
  166. womm/shared/results/base.py +85 -0
  167. womm/shared/results/command_results.py +61 -0
  168. womm/shared/results/context_results.py +345 -0
  169. womm/shared/results/dependency_results.py +110 -0
  170. womm/shared/results/doctor_results.py +41 -0
  171. womm/shared/results/file_results.py +97 -0
  172. womm/shared/results/project_results.py +163 -0
  173. womm/shared/results/security_results.py +128 -0
  174. womm/shared/results/system_results.py +268 -0
  175. womm/shared/results/template_results.py +43 -0
  176. womm/shared/runtime.py +36 -0
  177. womm/ui/__init__.py +31 -0
  178. womm/ui/common/__init__.py +39 -0
  179. womm/ui/common/interactive_menu.py +246 -0
  180. womm/ui/common/logging.py +184 -0
  181. womm/ui/common/prompts.py +93 -0
  182. womm/ui/context/__init__.py +31 -0
  183. womm/ui/context/display.py +485 -0
  184. womm/ui/context/menu_display.py +189 -0
  185. womm/ui/context/wizard.py +557 -0
  186. womm/ui/project/__init__.py +42 -0
  187. womm/ui/project/create/create_completion_summaries.py +55 -0
  188. womm/ui/project/project_wizard.py +174 -0
  189. womm/ui/project/setup/setup_completion_summaries.py +111 -0
  190. womm/ui/project/templates/template_store_display.py +128 -0
  191. womm/ui/system/__init__.py +60 -0
  192. womm/ui/system/display.py +531 -0
  193. womm/ui/system/path_display.py +387 -0
  194. womm/utils/__init__.py +23 -0
  195. womm/utils/common/__init__.py +49 -0
  196. womm/utils/common/file_scanner_utils.py +110 -0
  197. womm/utils/common/path_resolver_utils.py +127 -0
  198. womm/utils/common/safe_fs_utils.py +135 -0
  199. womm/utils/context/__init__.py +54 -0
  200. womm/utils/context/core_utils.py +345 -0
  201. womm/utils/context/icon_utils.py +259 -0
  202. womm/utils/project/__init__.py +106 -0
  203. womm/utils/project/asset_utils.py +154 -0
  204. womm/utils/project/core_utils.py +391 -0
  205. womm/utils/project/file_utils.py +494 -0
  206. womm/utils/project/platform_utils.py +170 -0
  207. womm/utils/project/validation_utils.py +273 -0
  208. womm/utils/security/__init__.py +46 -0
  209. womm/utils/security/security_validation_utils.py +258 -0
  210. womm/utils/system/__init__.py +69 -0
  211. womm/utils/system/detector_utils.py +299 -0
  212. womm/utils/system/environment_utils.py +172 -0
  213. womm/utils/system/path_utils.py +101 -0
  214. womm_cli-4.0.0.dist-info/METADATA +203 -0
  215. womm_cli-4.0.0.dist-info/RECORD +218 -0
  216. womm_cli-4.0.0.dist-info/WHEEL +4 -0
  217. womm_cli-4.0.0.dist-info/entry_points.txt +2 -0
  218. womm_cli-4.0.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env python3
2
+ # ///////////////////////////////////////////////////////////////
3
+ # SETUP PROFILE RESOLVER - Generic project-type resolution for setup
4
+ # Project: works-on-my-machine
5
+ # ///////////////////////////////////////////////////////////////
6
+
7
+ """Resolve a project-type identifier to a setup profile.
8
+
9
+ Replaces the historical fixed ``SUPPORTED_PROJECT_TYPES`` list. An
10
+ identifier is looked up in the template catalog first (``_womm.setup``
11
+ metadata); ``react``/``vue`` are not yet separate catalog entries so they
12
+ fall back to a small static alias table.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ # ///////////////////////////////////////////////////////////////
18
+ # IMPORTS
19
+ # ///////////////////////////////////////////////////////////////
20
+ from collections.abc import Mapping
21
+ from dataclasses import dataclass
22
+ from pathlib import Path
23
+
24
+ from .template_store_service import TemplateStoreService
25
+
26
+ # ///////////////////////////////////////////////////////////////
27
+ # MODELS
28
+ # ///////////////////////////////////////////////////////////////
29
+
30
+
31
+ @dataclass(frozen=True)
32
+ class SetupProfile:
33
+ """What `womm setup` needs to know about a project type.
34
+
35
+ Attributes:
36
+ language: Language family selecting the environment service
37
+ ("python" or "javascript").
38
+ template_source: Copier template used to render the VS Code settings.
39
+ answers: Template answers needed to render the settings.
40
+ """
41
+
42
+ language: str
43
+ template_source: Path
44
+ answers: Mapping[str, object]
45
+
46
+
47
+ # ///////////////////////////////////////////////////////////////
48
+ # CONSTANTS
49
+ # ///////////////////////////////////////////////////////////////
50
+
51
+ # Identifiers not yet backed by a separate catalog entry. Only reachable
52
+ # via explicit --type or the interactive wizard: auto-detection never
53
+ # returns them (see ProjectDetectionService.detect_project_type).
54
+ _LEGACY_SETUP_ALIASES: dict[str, tuple[str, str]] = {
55
+ "react": ("react", "React.js project (javascript template variant)"),
56
+ "vue": ("vue", "Vue.js project (javascript template variant)"),
57
+ }
58
+
59
+ # ///////////////////////////////////////////////////////////////
60
+ # PUBLIC FUNCTIONS
61
+ # ///////////////////////////////////////////////////////////////
62
+
63
+
64
+ def resolve_setup_profile(
65
+ identifier: str, store: TemplateStoreService | None = None
66
+ ) -> SetupProfile | None:
67
+ """Resolve an identifier to a setup profile.
68
+
69
+ Args:
70
+ identifier: Catalog id (e.g. "python") or legacy alias
71
+ (e.g. "react").
72
+ store: Template store to query. Defaults to a fresh
73
+ ``TemplateStoreService``.
74
+
75
+ Returns:
76
+ SetupProfile | None: The resolved profile, or ``None`` when the
77
+ identifier is neither a catalog entry with setup metadata nor a
78
+ known legacy alias.
79
+ """
80
+ store = store or TemplateStoreService()
81
+ entries = store.list_templates()
82
+ for entry in entries:
83
+ if entry.id == identifier and entry.setup_language:
84
+ return SetupProfile(
85
+ language=entry.setup_language,
86
+ template_source=entry.source,
87
+ answers=_setup_answers(),
88
+ )
89
+
90
+ alias = _LEGACY_SETUP_ALIASES.get(identifier)
91
+ if alias is None:
92
+ return None
93
+ javascript_template = next(
94
+ (entry for entry in entries if entry.id == "javascript"), None
95
+ )
96
+ if javascript_template is None:
97
+ return None
98
+ return SetupProfile(
99
+ language="javascript",
100
+ template_source=javascript_template.source,
101
+ answers={**_setup_answers(), "framework": alias[0]},
102
+ )
103
+
104
+
105
+ def _setup_answers() -> dict[str, object]:
106
+ """Supply non-interactive answers needed to render setup configuration."""
107
+ return {
108
+ "description": "Project configured by WOMM",
109
+ "with_vscode": True,
110
+ }
111
+
112
+
113
+ def list_setup_identifiers(
114
+ store: TemplateStoreService | None = None,
115
+ ) -> list[tuple[str, str]]:
116
+ """List every identifier `womm setup` accepts, with a description.
117
+
118
+ Args:
119
+ store: Template store to query. Defaults to a fresh
120
+ ``TemplateStoreService``.
121
+
122
+ Returns:
123
+ list[tuple[str, str]]: ``(identifier, description)`` pairs, catalog
124
+ entries first (sorted by id), then legacy aliases.
125
+ """
126
+ store = store or TemplateStoreService()
127
+ catalog = sorted(
128
+ (
129
+ (entry.id, entry.description)
130
+ for entry in store.list_templates()
131
+ if entry.setup_language
132
+ ),
133
+ key=lambda pair: pair[0],
134
+ )
135
+ aliases = [
136
+ (identifier, description)
137
+ for identifier, (_framework, description) in _LEGACY_SETUP_ALIASES.items()
138
+ ]
139
+ return catalog + aliases
140
+
141
+
142
+ # ///////////////////////////////////////////////////////////////
143
+ # PUBLIC API
144
+ # ///////////////////////////////////////////////////////////////
145
+
146
+ __all__ = ["SetupProfile", "list_setup_identifiers", "resolve_setup_profile"]
@@ -0,0 +1,344 @@
1
+ #!/usr/bin/env python3
2
+ # ///////////////////////////////////////////////////////////////
3
+ # TEMPLATE STORE SERVICE - Copier template catalog
4
+ # Project: works-on-my-machine
5
+ # ///////////////////////////////////////////////////////////////
6
+
7
+ """Catalog of Copier templates known to WOMM.
8
+
9
+ Official templates ship inside the wheel and are discovered at read time;
10
+ they are never written to the user catalog. ``catalog.json`` holds only
11
+ user-registered templates.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ # ///////////////////////////////////////////////////////////////
17
+ # IMPORTS
18
+ # ///////////////////////////////////////////////////////////////
19
+ import json
20
+ import os
21
+ import tempfile
22
+ from pathlib import Path
23
+ from typing import Any
24
+
25
+ import yaml
26
+
27
+ from womm.exceptions.project import ProjectServiceError
28
+ from womm.shared.models.template_source import TemplateEntry, TemplateOrigin
29
+ from womm.shared.paths import (
30
+ packaged_copier_official,
31
+ template_catalog_file,
32
+ user_templates_dir,
33
+ )
34
+
35
+ # ///////////////////////////////////////////////////////////////
36
+ # CONSTANTS
37
+ # ///////////////////////////////////////////////////////////////
38
+
39
+ # Manifest a template root must expose to be considered valid.
40
+ TEMPLATE_MANIFEST = "copier.yml"
41
+
42
+ # Key carrying WOMM metadata inside a template manifest.
43
+ WOMM_METADATA_KEY = "_womm"
44
+
45
+ # Prefixes that mark a source as remote. Remote sources are out of scope
46
+ # until an explicit trust model exists.
47
+ REMOTE_PREFIXES = (
48
+ "http://",
49
+ "https://",
50
+ "http:/",
51
+ "https:/",
52
+ "http:\\",
53
+ "https:\\",
54
+ "git@",
55
+ "git+",
56
+ "gh:",
57
+ "gl:",
58
+ )
59
+
60
+ # ///////////////////////////////////////////////////////////////
61
+ # SERVICE
62
+ # ///////////////////////////////////////////////////////////////
63
+
64
+
65
+ class TemplateStoreService:
66
+ """Read and write the template catalog."""
67
+
68
+ # ///////////////////////////////////////////////////////////
69
+ # PUBLIC METHODS
70
+ # ///////////////////////////////////////////////////////////
71
+
72
+ def list_templates(self) -> list[TemplateEntry]:
73
+ """List every known template, official first.
74
+
75
+ Returns:
76
+ list[TemplateEntry]: Entries sorted by qualified identifier.
77
+
78
+ Raises:
79
+ ProjectServiceError: If the user catalog cannot be read.
80
+ """
81
+ entries = self._discover_official() + self._load_user_entries()
82
+ return sorted(entries, key=lambda entry: entry.qualified_id)
83
+
84
+ def resolve(self, identifier: str) -> TemplateEntry:
85
+ """Resolve an identifier to a single template.
86
+
87
+ A qualified identifier (``official/python``) selects one origin. A
88
+ short identifier resolves across origins and fails when ambiguous.
89
+
90
+ Args:
91
+ identifier: Qualified or short template identifier.
92
+
93
+ Returns:
94
+ TemplateEntry: The matching entry.
95
+
96
+ Raises:
97
+ ProjectServiceError: If unknown or ambiguous.
98
+ """
99
+ entries = self.list_templates()
100
+
101
+ qualified = [e for e in entries if e.qualified_id == identifier]
102
+ if qualified:
103
+ return qualified[0]
104
+
105
+ matches = [e for e in entries if e.id == identifier]
106
+ if len(matches) == 1:
107
+ return matches[0]
108
+ if len(matches) > 1:
109
+ names = ", ".join(e.qualified_id for e in matches)
110
+ raise ProjectServiceError(
111
+ operation="resolve_template",
112
+ reason=f"Template identifier is ambiguous: {identifier}",
113
+ details=f"Candidates: {names}. Use a qualified identifier.",
114
+ )
115
+ raise ProjectServiceError(
116
+ operation="resolve_template",
117
+ reason=f"Unknown template: {identifier}",
118
+ )
119
+
120
+ def add(self, identifier: str, source: Path) -> TemplateEntry:
121
+ """Register a local template under the user namespace.
122
+
123
+ Args:
124
+ identifier: Short identifier, without any separator.
125
+ source: Directory holding ``copier.yml``.
126
+
127
+ Returns:
128
+ TemplateEntry: The registered entry.
129
+
130
+ Raises:
131
+ ProjectServiceError: If the identifier or source is refused.
132
+ """
133
+ self._validate_identifier(identifier, operation="add_template")
134
+ self._validate_source(source, operation="add_template")
135
+
136
+ existing = self._load_user_entries()
137
+ if any(entry.id == identifier for entry in existing):
138
+ raise ProjectServiceError(
139
+ operation="add_template",
140
+ reason=f"Template already registered: {identifier}",
141
+ )
142
+
143
+ entry = self._build_user_entry(identifier, source)
144
+ self._save_user_entries([*existing, entry])
145
+ return entry
146
+
147
+ def remove(self, identifier: str) -> TemplateEntry:
148
+ """Unregister a user template.
149
+
150
+ Args:
151
+ identifier: Qualified or short identifier.
152
+
153
+ Returns:
154
+ TemplateEntry: The removed entry.
155
+
156
+ Raises:
157
+ ProjectServiceError: If unknown or not user-owned.
158
+ """
159
+ entry = self.resolve(identifier)
160
+ if entry.origin is not TemplateOrigin.USER:
161
+ raise ProjectServiceError(
162
+ operation="remove_template",
163
+ reason=f"Official templates cannot be removed: {identifier}",
164
+ )
165
+ remaining = [item for item in self._load_user_entries() if item.id != entry.id]
166
+ self._save_user_entries(remaining)
167
+ return entry
168
+
169
+ def update(self, identifier: str, source: Path) -> TemplateEntry:
170
+ """Point an existing user template at a new source.
171
+
172
+ Args:
173
+ identifier: Qualified or short identifier.
174
+ source: New directory holding ``copier.yml``.
175
+
176
+ Returns:
177
+ TemplateEntry: The updated entry.
178
+
179
+ Raises:
180
+ ProjectServiceError: If unknown, not user-owned, or source invalid.
181
+ """
182
+ entry = self.resolve(identifier)
183
+ if entry.origin is not TemplateOrigin.USER:
184
+ raise ProjectServiceError(
185
+ operation="update_template",
186
+ reason=f"Official templates cannot be updated: {identifier}",
187
+ )
188
+ self._validate_source(source, operation="update_template")
189
+ updated = self._build_user_entry(entry.id, source)
190
+ remaining = [item for item in self._load_user_entries() if item.id != entry.id]
191
+ self._save_user_entries([*remaining, updated])
192
+ return updated
193
+
194
+ # ///////////////////////////////////////////////////////////
195
+ # PRIVATE METHODS - DISCOVERY
196
+ # ///////////////////////////////////////////////////////////
197
+
198
+ def _discover_official(self) -> list[TemplateEntry]:
199
+ """Enumerate shipped templates, without persisting anything."""
200
+ root = Path(str(packaged_copier_official()))
201
+ if not root.is_dir():
202
+ return []
203
+ entries: list[TemplateEntry] = []
204
+ for candidate in sorted(root.iterdir()):
205
+ manifest = candidate / TEMPLATE_MANIFEST
206
+ if not manifest.is_file():
207
+ continue
208
+ metadata = self._read_metadata(manifest)
209
+ setup = metadata.get("setup", {})
210
+ setup = setup if isinstance(setup, dict) else {}
211
+ entries.append(
212
+ TemplateEntry(
213
+ id=candidate.name,
214
+ origin=TemplateOrigin.OFFICIAL,
215
+ source=candidate,
216
+ version=str(metadata.get("version", "")),
217
+ description=str(metadata.get("description", "")),
218
+ setup_language=str(setup.get("language", "")),
219
+ )
220
+ )
221
+ return entries
222
+
223
+ def _read_metadata(self, manifest: Path) -> dict[str, Any]:
224
+ """Read the ``_womm`` block of a template manifest, tolerantly."""
225
+ try:
226
+ payload = yaml.safe_load(manifest.read_text(encoding="utf-8")) or {}
227
+ except (OSError, yaml.YAMLError):
228
+ return {}
229
+ block = payload.get(WOMM_METADATA_KEY, {})
230
+ return block if isinstance(block, dict) else {}
231
+
232
+ # ///////////////////////////////////////////////////////////
233
+ # PRIVATE METHODS - CATALOG IO
234
+ # ///////////////////////////////////////////////////////////
235
+
236
+ def _load_user_entries(self) -> list[TemplateEntry]:
237
+ """Read the user catalog, treating absence as empty."""
238
+ catalog = template_catalog_file()
239
+ if not catalog.is_file():
240
+ return []
241
+ try:
242
+ payload = json.loads(catalog.read_text(encoding="utf-8"))
243
+ items = payload["templates"]
244
+ return [
245
+ TemplateEntry(
246
+ id=item["id"],
247
+ origin=TemplateOrigin.USER,
248
+ source=Path(item["source"]),
249
+ version=item.get("version", ""),
250
+ description=item.get("description", ""),
251
+ )
252
+ for item in items
253
+ ]
254
+ except (OSError, json.JSONDecodeError, KeyError, TypeError) as exc:
255
+ raise ProjectServiceError(
256
+ operation="load_catalog",
257
+ reason=f"Template catalog is unreadable: {catalog}",
258
+ details=str(exc),
259
+ ) from exc
260
+
261
+ def _save_user_entries(self, entries: list[TemplateEntry]) -> None:
262
+ """Write the user catalog atomically."""
263
+ catalog = template_catalog_file()
264
+ user_templates_dir()
265
+ payload = {
266
+ "templates": [
267
+ {
268
+ "id": entry.id,
269
+ "source": str(entry.source),
270
+ "version": entry.version,
271
+ "description": entry.description,
272
+ }
273
+ for entry in sorted(entries, key=lambda item: item.id)
274
+ ]
275
+ }
276
+ handle, temp_name = tempfile.mkstemp(dir=str(catalog.parent), suffix=".tmp")
277
+ try:
278
+ with os.fdopen(handle, "w", encoding="utf-8") as stream:
279
+ json.dump(payload, stream, indent=2)
280
+ os.replace(temp_name, catalog)
281
+ except OSError as exc:
282
+ Path(temp_name).unlink(missing_ok=True)
283
+ raise ProjectServiceError(
284
+ operation="save_catalog",
285
+ reason=f"Template catalog could not be written: {catalog}",
286
+ details=str(exc),
287
+ ) from exc
288
+
289
+ # ///////////////////////////////////////////////////////////
290
+ # PRIVATE METHODS - VALIDATION
291
+ # ///////////////////////////////////////////////////////////
292
+
293
+ def _validate_identifier(self, identifier: str, *, operation: str) -> None:
294
+ """Reject namespaced, empty or path-like identifiers."""
295
+ if not identifier or identifier != identifier.strip():
296
+ raise ProjectServiceError(
297
+ operation=operation,
298
+ reason="Template identifier must not be empty",
299
+ )
300
+ if "/" in identifier or "\\" in identifier or identifier.startswith("."):
301
+ raise ProjectServiceError(
302
+ operation=operation,
303
+ reason=f"Template identifier must be a plain name: {identifier}",
304
+ details="The official/ namespace is reserved and separators "
305
+ "are not allowed.",
306
+ )
307
+
308
+ def _validate_source(self, source: Path, *, operation: str) -> None:
309
+ """Reject remote sources and sources without a manifest."""
310
+ raw = str(source)
311
+ if raw.startswith(REMOTE_PREFIXES):
312
+ raise ProjectServiceError(
313
+ operation=operation,
314
+ reason=f"Remote template sources are not supported yet: {raw}",
315
+ )
316
+ resolved = source.expanduser().resolve()
317
+ if not (resolved / TEMPLATE_MANIFEST).is_file():
318
+ raise ProjectServiceError(
319
+ operation=operation,
320
+ reason=f"Not a Copier template: {source}",
321
+ details=f"Expected a {TEMPLATE_MANIFEST} file at the root.",
322
+ )
323
+
324
+ def _build_user_entry(self, identifier: str, source: Path) -> TemplateEntry:
325
+ """Build a user entry, reading metadata from the manifest."""
326
+ resolved = source.expanduser().resolve()
327
+ metadata = self._read_metadata(resolved / TEMPLATE_MANIFEST)
328
+ setup = metadata.get("setup", {})
329
+ setup = setup if isinstance(setup, dict) else {}
330
+ return TemplateEntry(
331
+ id=identifier,
332
+ origin=TemplateOrigin.USER,
333
+ source=resolved,
334
+ version=str(metadata.get("version", "")),
335
+ description=str(metadata.get("description", "")),
336
+ setup_language=str(setup.get("language", "")),
337
+ )
338
+
339
+
340
+ # ///////////////////////////////////////////////////////////////
341
+ # PUBLIC API
342
+ # ///////////////////////////////////////////////////////////////
343
+
344
+ __all__ = ["TemplateStoreService"]