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,127 @@
1
+ #!/usr/bin/env python3
2
+ # ///////////////////////////////////////////////////////////////
3
+ # PATH RESOLVER UTILS - Path & Module Path Utilities
4
+ # Project: works-on-my-machine
5
+ # ///////////////////////////////////////////////////////////////
6
+
7
+ """
8
+ Path and module path utilities for Works On My Machine.
9
+
10
+ This module centralizes path resolution logic that was previously in
11
+ `womm/shared/path_resolver.py` and `womm/shared/imports.py` (path part).
12
+ All helpers are stateless and safe to use from anywhere.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ # ///////////////////////////////////////////////////////////////
18
+ # IMPORTS
19
+ # ///////////////////////////////////////////////////////////////
20
+ # Standard library imports
21
+ import logging
22
+ from pathlib import Path
23
+
24
+ # ///////////////////////////////////////////////////////////////
25
+ # LOGGER SETUP
26
+ # ///////////////////////////////////////////////////////////////
27
+
28
+ logger = logging.getLogger(__name__)
29
+
30
+ # ///////////////////////////////////////////////////////////////
31
+ # CONSTANTS
32
+ # ///////////////////////////////////////////////////////////////
33
+
34
+ BIN_PREFIX = "bin/"
35
+ BIN_PREFIX_LENGTH = len(BIN_PREFIX)
36
+ ASSETS_PREFIX = "assets/"
37
+ ASSETS_PREFIX_LENGTH = len(ASSETS_PREFIX)
38
+
39
+
40
+ # ///////////////////////////////////////////////////////////////
41
+ # PROJECT & SHARED PATH HELPERS
42
+ # ///////////////////////////////////////////////////////////////
43
+
44
+
45
+ def get_shared_module_path() -> Path:
46
+ """Return the path to the `womm/shared` directory.
47
+
48
+ Returns:
49
+ Path: Path to the shared module directory.
50
+ """
51
+ return Path(__file__).parent.parent.parent / "shared"
52
+
53
+
54
+ def get_bin_module_path() -> Path:
55
+ """Return the path to the `womm/bin` directory.
56
+
57
+ Returns:
58
+ Path: Path to the bin module directory.
59
+ """
60
+ return Path(__file__).parent.parent / "bin"
61
+
62
+
63
+ def get_assets_module_path() -> Path:
64
+ """Return the path to the `womm/assets` directory.
65
+
66
+ Returns:
67
+ Path: Path to the assets module directory.
68
+ """
69
+ return Path(__file__).parent.parent.parent / "assets"
70
+
71
+
72
+ # ///////////////////////////////////////////////////////////////
73
+ # SCRIPT PATH HELPERS
74
+ # ///////////////////////////////////////////////////////////////
75
+
76
+
77
+ def resolve_script_path(relative_path: str) -> Path:
78
+ """Resolve a script path relative to assets, bin, or the working directory.
79
+
80
+ The path is resolved based on its prefix:
81
+ - ``\"bin/\"``: resolved against `womm/bin/`
82
+ - ``\"assets/\"``: resolved against `womm/assets/`
83
+ - Otherwise: resolved against the current working directory
84
+
85
+ Args:
86
+ relative_path: Relative path to resolve.
87
+
88
+ Returns:
89
+ Path: Resolved absolute path.
90
+ """
91
+ if relative_path.startswith(BIN_PREFIX):
92
+ bin_path = get_bin_module_path()
93
+ return bin_path / relative_path[BIN_PREFIX_LENGTH:]
94
+ if relative_path.startswith(ASSETS_PREFIX):
95
+ assets_path = get_assets_module_path()
96
+ return assets_path / relative_path[ASSETS_PREFIX_LENGTH:]
97
+ return Path(relative_path).resolve()
98
+
99
+
100
+ def validate_script_exists(script_path: Path) -> bool:
101
+ """Return True if a script file exists and is a regular file.
102
+
103
+ Args:
104
+ script_path: Path to the script file.
105
+
106
+ Returns:
107
+ bool: True if the path exists and is a file, False otherwise.
108
+ """
109
+ return script_path.exists() and script_path.is_file()
110
+
111
+
112
+ # ///////////////////////////////////////////////////////////////
113
+ # ENVIRONMENT DETECTION
114
+ # ///////////////////////////////////////////////////////////////
115
+
116
+
117
+ # ///////////////////////////////////////////////////////////////
118
+ # PUBLIC API
119
+ # ///////////////////////////////////////////////////////////////
120
+
121
+ __all__ = [
122
+ "get_assets_module_path",
123
+ "get_bin_module_path",
124
+ "get_shared_module_path",
125
+ "resolve_script_path",
126
+ "validate_script_exists",
127
+ ]
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/env python3
2
+ # ///////////////////////////////////////////////////////////////
3
+ # SAFE FILESYSTEM UTILITIES - Protected filesystem operations
4
+ # Project: works-on-my-machine
5
+ # ///////////////////////////////////////////////////////////////
6
+
7
+ """
8
+ Safe filesystem utilities for Works On My Machine.
9
+
10
+ Provides guarded wrappers around destructive filesystem operations
11
+ (e.g. shutil.rmtree) to prevent accidental deletion of critical
12
+ system paths or paths outside an expected boundary.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ # ///////////////////////////////////////////////////////////////
18
+ # IMPORTS
19
+ # ///////////////////////////////////////////////////////////////
20
+ # Standard library imports
21
+ import logging
22
+ import platform
23
+ import shutil
24
+ from pathlib import Path
25
+
26
+ # ///////////////////////////////////////////////////////////////
27
+ # LOGGER
28
+ # ///////////////////////////////////////////////////////////////
29
+ # Low-level fs utility: use stdlib logging to avoid depending on the ui layer
30
+ # (importing ui.common here would create a utils <-> ui circular import).
31
+ logger = logging.getLogger(__name__)
32
+
33
+ # ///////////////////////////////////////////////////////////////
34
+ # CRITICAL PATHS - Never allow deletion
35
+ # ///////////////////////////////////////////////////////////////
36
+
37
+ _CRITICAL_PATHS_UNIX: set[str] = {
38
+ "/",
39
+ "/bin",
40
+ "/boot",
41
+ "/dev",
42
+ "/etc",
43
+ "/home",
44
+ "/lib",
45
+ "/lib64",
46
+ "/opt",
47
+ "/proc",
48
+ "/root",
49
+ "/sbin",
50
+ "/sys",
51
+ # Protected path in delete blocklist, not a temp-file usage.
52
+ "/tmp", # noqa: S108 # nosec B108 - blocklist entry, never a temp path we write to
53
+ "/usr",
54
+ "/var",
55
+ }
56
+
57
+ _CRITICAL_PATHS_WINDOWS: set[str] = {
58
+ "C:\\",
59
+ "C:\\Windows",
60
+ "C:\\Windows\\System32",
61
+ "C:\\Program Files",
62
+ "C:\\Program Files (x86)",
63
+ "C:\\Users",
64
+ "C:\\ProgramData",
65
+ }
66
+
67
+
68
+ def _get_critical_paths() -> set[Path]:
69
+ """Build the set of critical paths for the current platform."""
70
+ raw: set[str] = set()
71
+ if platform.system() == "Windows":
72
+ raw = _CRITICAL_PATHS_WINDOWS
73
+ else:
74
+ raw = _CRITICAL_PATHS_UNIX
75
+ # Always protect home directory root
76
+ raw.add(str(Path.home()))
77
+ return {Path(p).resolve() for p in raw}
78
+
79
+
80
+ # ///////////////////////////////////////////////////////////////
81
+ # PUBLIC API
82
+ # ///////////////////////////////////////////////////////////////
83
+
84
+
85
+ def safe_rmtree(path: Path, *, allowed_parent: Path) -> None:
86
+ """Remove a directory tree with safety guards.
87
+
88
+ Performs two safety checks before delegating to ``shutil.rmtree``:
89
+
90
+ 1. **Boundary check** -- *path* must resolve to a location **inside**
91
+ *allowed_parent*. This prevents path-traversal attacks where a
92
+ crafted symbolic link or ``..`` component could escape the
93
+ expected directory.
94
+
95
+ 2. **Critical-path check** -- *path* must not resolve to a known
96
+ system-critical directory (``/``, ``C:\\Windows``, user home, etc.).
97
+
98
+ Args:
99
+ path: Directory to remove. Resolved before any check.
100
+ allowed_parent: The directory that must be an ancestor of *path*.
101
+ Resolved before comparison.
102
+
103
+ Raises:
104
+ ValueError: If *path* falls outside *allowed_parent* or resolves
105
+ to a critical system directory.
106
+ FileNotFoundError: If *path* does not exist (no silent no-op).
107
+ PermissionError: Propagated from ``shutil.rmtree``.
108
+ OSError: Propagated from ``shutil.rmtree``.
109
+ """
110
+ resolved = path.resolve()
111
+ parent_resolved = allowed_parent.resolve()
112
+
113
+ # --- guard: path must exist ----------------------------------
114
+ if not resolved.exists():
115
+ raise FileNotFoundError(f"Directory does not exist: {resolved}")
116
+
117
+ # --- guard: must be a directory ------------------------------
118
+ if not resolved.is_dir():
119
+ raise NotADirectoryError(f"Path is not a directory: {resolved}")
120
+
121
+ # --- guard: boundary check -----------------------------------
122
+ if not resolved.is_relative_to(parent_resolved):
123
+ raise ValueError(
124
+ f"Refusing to delete {resolved}: outside allowed boundary {parent_resolved}"
125
+ )
126
+
127
+ # --- guard: critical path check ------------------------------
128
+ critical = _get_critical_paths()
129
+ if resolved in critical:
130
+ raise ValueError(f"Refusing to delete critical system path: {resolved}")
131
+
132
+ logger.debug(
133
+ "safe_rmtree: removing %s (allowed parent: %s)", resolved, parent_resolved
134
+ )
135
+ shutil.rmtree(resolved)
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env python3
2
+ # ///////////////////////////////////////////////////////////////
3
+ # UTILS CONTEXT - Context Utilities
4
+ # Project: works-on-my-machine
5
+ # ///////////////////////////////////////////////////////////////
6
+
7
+ """
8
+ Pure utility functions for context menu operations.
9
+
10
+ This package contains stateless utility functions for context menu
11
+ validation and registry operations.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ # ///////////////////////////////////////////////////////////////
17
+ # IMPORTS
18
+ # ///////////////////////////////////////////////////////////////
19
+ # Local imports
20
+ from .core_utils import (
21
+ build_command_with_parameter,
22
+ generate_registry_key_name,
23
+ get_available_file_types,
24
+ get_context_type_help,
25
+ get_file_type_help,
26
+ get_registry_entry_info,
27
+ sanitize_label,
28
+ sanitize_registry_key,
29
+ validate_backup_data,
30
+ validate_icon_path,
31
+ validate_label,
32
+ validate_registry_key,
33
+ )
34
+ from .icon_utils import ContextIconResolver
35
+
36
+ # ///////////////////////////////////////////////////////////////
37
+ # PUBLIC API
38
+ # ///////////////////////////////////////////////////////////////
39
+
40
+ __all__ = [
41
+ "ContextIconResolver",
42
+ "build_command_with_parameter",
43
+ "generate_registry_key_name",
44
+ "get_available_file_types",
45
+ "get_context_type_help",
46
+ "get_file_type_help",
47
+ "get_registry_entry_info",
48
+ "sanitize_label",
49
+ "sanitize_registry_key",
50
+ "validate_backup_data",
51
+ "validate_icon_path",
52
+ "validate_label",
53
+ "validate_registry_key",
54
+ ]
@@ -0,0 +1,345 @@
1
+ #!/usr/bin/env python3
2
+ # ///////////////////////////////////////////////////////////////
3
+ # CONTEXT UTILS - Consolidated Context Menu Utilities
4
+ # Project: works-on-my-machine
5
+ # ///////////////////////////////////////////////////////////////
6
+
7
+ """
8
+ Consolidated context menu utilities.
9
+
10
+ Merged from:
11
+ - context_command_utils.py
12
+ - context_help_utils.py
13
+ - context_registry_utils.py
14
+ - context_validation_utils.py
15
+
16
+ Provides utilities for context menu operations including:
17
+ - Command building
18
+ - Registry key management
19
+ - Help text generation
20
+ - Input validation
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ # ///////////////////////////////////////////////////////////////
26
+ # IMPORTS
27
+ # ///////////////////////////////////////////////////////////////
28
+ # Standard library imports
29
+ import logging
30
+ from pathlib import Path
31
+
32
+ # Local imports
33
+ from ...shared.configs.context import ContextConfig
34
+
35
+ # ///////////////////////////////////////////////////////////////
36
+ # LOGGER SETUP
37
+ # ///////////////////////////////////////////////////////////////
38
+
39
+ logger = logging.getLogger(__name__)
40
+
41
+ # ///////////////////////////////////////////////////////////////
42
+ # COMMAND BUILDING FUNCTIONS
43
+ # ///////////////////////////////////////////////////////////////
44
+
45
+
46
+ def build_command_with_parameter(base_command: str, parameter: str) -> str:
47
+ """
48
+ Build a command with a parameter, handling various quote formats.
49
+
50
+ Args:
51
+ base_command: Base command without parameters
52
+ parameter: Parameter to append (e.g., "%V", "%1")
53
+
54
+ Returns:
55
+ Complete command with parameter
56
+
57
+ Raises:
58
+ ValueError: If parameters are invalid
59
+ """
60
+ if not base_command:
61
+ raise ValueError("Base command cannot be empty")
62
+
63
+ # Handle quoted commands
64
+ if base_command.startswith('"') and base_command.endswith('"'):
65
+ # Insert parameter before closing quote
66
+ return f'{base_command[:-1]} {parameter}"'
67
+ else:
68
+ # Append parameter directly
69
+ return f"{base_command} {parameter}"
70
+
71
+
72
+ # ///////////////////////////////////////////////////////////////
73
+ # REGISTRY KEY GENERATION FUNCTIONS
74
+ # ///////////////////////////////////////////////////////////////
75
+
76
+
77
+ def generate_registry_key_name(file_path: str) -> str:
78
+ """
79
+ Generate registry key name from file path.
80
+
81
+ Args:
82
+ file_path: Path to the file
83
+
84
+ Returns:
85
+ Generated registry key name (filename without extension)
86
+
87
+ Raises:
88
+ ValueError: If file_path is invalid
89
+ """
90
+ if not file_path:
91
+ raise ValueError("File path cannot be empty")
92
+
93
+ path = Path(file_path)
94
+ key_name = path.stem # Filename without extension
95
+
96
+ if not key_name:
97
+ raise ValueError(
98
+ f"Could not generate valid key name from file path: {file_path}"
99
+ )
100
+
101
+ return key_name
102
+
103
+
104
+ def sanitize_registry_key(key: str) -> str:
105
+ """
106
+ Sanitize registry key by removing invalid characters.
107
+
108
+ Args:
109
+ key: Registry key to sanitize
110
+
111
+ Returns:
112
+ Sanitized registry key
113
+
114
+ Raises:
115
+ ValueError: If key is invalid
116
+ """
117
+ if not key:
118
+ raise ValueError("Registry key cannot be empty")
119
+
120
+ # Remove invalid characters
121
+ invalid_chars = ["\\", "/", ":", "*", "?", '"', "<", ">", "|"]
122
+ sanitized = key
123
+ for char in invalid_chars:
124
+ sanitized = sanitized.replace(char, "_")
125
+
126
+ return sanitized
127
+
128
+
129
+ def sanitize_label(label: str) -> str:
130
+ """
131
+ Sanitize label by removing/replacing invalid characters.
132
+
133
+ Args:
134
+ label: Label to sanitize
135
+
136
+ Returns:
137
+ Sanitized label
138
+
139
+ Raises:
140
+ ValueError: If label is invalid
141
+ """
142
+ if not label:
143
+ raise ValueError("Label cannot be empty")
144
+
145
+ # Remove control characters but keep spaces and common punctuation
146
+ sanitized = "".join(c for c in label if c.isprintable() or c.isspace())
147
+
148
+ return sanitized.strip()
149
+
150
+
151
+ def get_context_type_help() -> str:
152
+ """
153
+ Get help text for context types.
154
+
155
+ Returns:
156
+ Help text string describing available context types
157
+ """
158
+ help_text = "Available context types:\n"
159
+ help_text += "• --root: Root directories (drives)\n"
160
+ help_text += "• --file: Single file selection\n"
161
+ help_text += "• --files: Multiple file selection\n"
162
+ help_text += "• --background: Background context (empty space)\n"
163
+ help_text += "• Default: Both directory and background contexts\n"
164
+ return help_text
165
+
166
+
167
+ def get_file_type_help() -> str:
168
+ """
169
+ Get help text for file types.
170
+
171
+ Returns:
172
+ Help text string describing available file types
173
+ """
174
+ help_text = "Available file types:\n"
175
+ for file_type, extensions in ContextConfig.FILE_TYPE_EXTENSIONS.items():
176
+ ext_list = ", ".join(sorted(extensions))
177
+ help_text += f"• {file_type}: {ext_list}\n"
178
+ help_text += "• Custom extensions: Use --extensions .ext1 .ext2\n"
179
+ return help_text
180
+
181
+
182
+ def get_available_file_types() -> dict[str, set[str]]:
183
+ """
184
+ Get all available file types and their extensions.
185
+
186
+ Returns:
187
+ Dictionary of file types and their extensions
188
+ """
189
+ return ContextConfig.FILE_TYPE_EXTENSIONS.copy()
190
+
191
+
192
+ def get_registry_entry_info(
193
+ base_path: str, key_name: str
194
+ ) -> dict[str, str | None] | None:
195
+ """
196
+ Get information about a specific registry entry.
197
+
198
+ Args:
199
+ base_path: Base registry path
200
+ key_name: Registry key name
201
+
202
+ Returns:
203
+ Entry information dictionary or None if failed
204
+
205
+ Raises:
206
+ OSError: If the registry key cannot be opened
207
+ """
208
+ import winreg
209
+
210
+ full_path = f"{base_path}\\{key_name}"
211
+ with winreg.OpenKey(winreg.HKEY_CURRENT_USER, full_path) as key:
212
+ info: dict[str, str | None] = {
213
+ "key_name": key_name,
214
+ "registry_path": full_path,
215
+ }
216
+
217
+ # Get MUIVerb (display name)
218
+ try:
219
+ mui_verb, _ = winreg.QueryValueEx(key, "MUIVerb")
220
+ info["display_name"] = mui_verb
221
+ except OSError:
222
+ info["display_name"] = key_name
223
+
224
+ # Get icon
225
+ try:
226
+ icon, _ = winreg.QueryValueEx(key, "Icon")
227
+ info["icon"] = icon
228
+ except OSError:
229
+ info["icon"] = None
230
+
231
+ # Get command
232
+ try:
233
+ with winreg.OpenKey(key, "command") as cmd_key:
234
+ command, _ = winreg.QueryValueEx(cmd_key, "")
235
+ info["command"] = command
236
+ except OSError:
237
+ info["command"] = None
238
+
239
+ return info
240
+
241
+
242
+ # ///////////////////////////////////////////////////////////////
243
+ # HELP TEXT FUNCTIONS
244
+ # ///////////////////////////////////////////////////////////////
245
+
246
+
247
+ # ///////////////////////////////////////////////////////////////
248
+ # VALIDATION FUNCTIONS
249
+ # ///////////////////////////////////////////////////////////////
250
+
251
+
252
+ def validate_label(label: str) -> None:
253
+ """
254
+ Validate label for context menu entry.
255
+
256
+ Args:
257
+ label: Label to validate
258
+
259
+ Raises:
260
+ ValueError: If label is invalid
261
+ """
262
+ if not label:
263
+ raise ValueError("Label cannot be empty")
264
+
265
+ if len(label) > 100:
266
+ raise ValueError(f"Label is too long (max 100 characters, got {len(label)})")
267
+
268
+ # Check for at least one valid character
269
+ if not any(c.isalnum() for c in label):
270
+ raise ValueError("Label must contain at least one alphanumeric character")
271
+
272
+
273
+ def validate_registry_key(key: str) -> None:
274
+ """
275
+ Validate registry key for context menu entry.
276
+
277
+ Args:
278
+ key: Registry key to validate
279
+
280
+ Raises:
281
+ ValueError: If key is invalid
282
+ """
283
+ if not key:
284
+ raise ValueError("Registry key cannot be empty")
285
+
286
+ # Check for invalid characters
287
+ invalid_chars = ["\\", "/", ":", "*", "?", '"', "<", ">", "|"]
288
+ for char in invalid_chars:
289
+ if char in key:
290
+ raise ValueError(f"Registry key contains invalid character: {char}")
291
+
292
+ if len(key) > 255:
293
+ raise ValueError(
294
+ f"Registry key is too long (max 255 characters, got {len(key)})"
295
+ )
296
+
297
+
298
+ def validate_icon_path(icon_path: str) -> None:
299
+ """
300
+ Validate icon path for context menu entry.
301
+
302
+ Args:
303
+ icon_path: Icon path to validate
304
+
305
+ Raises:
306
+ ValueError: If icon path is invalid
307
+ """
308
+ if not icon_path or icon_path == "auto":
309
+ return # "auto" is valid
310
+
311
+ path = Path(icon_path)
312
+ if not path.exists():
313
+ raise ValueError(f"Icon file does not exist: {icon_path}")
314
+
315
+ # Check file extension
316
+ valid_extensions = {".ico", ".png", ".bmp", ".jpg", ".exe", ".dll"}
317
+ if path.suffix.lower() not in valid_extensions:
318
+ raise ValueError(f"Unsupported icon format: {path.suffix}")
319
+
320
+
321
+ def validate_backup_data(data: dict) -> None:
322
+ """
323
+ Validate backup data structure.
324
+
325
+ Args:
326
+ data: Backup data to validate
327
+
328
+ Raises:
329
+ ValueError: If backup data is invalid
330
+ """
331
+ if not isinstance(data, dict):
332
+ raise ValueError(f"Backup data must be a dictionary, got {type(data).__name__}")
333
+
334
+ required_keys = {"version", "timestamp", "entries", "metadata"}
335
+ missing_keys = required_keys - set(data.keys())
336
+
337
+ if missing_keys:
338
+ raise ValueError(
339
+ f"Backup data missing required keys: {', '.join(missing_keys)}"
340
+ )
341
+
342
+ if not isinstance(data["entries"], (dict, list)):
343
+ raise ValueError(
344
+ f"Entries must be dict or list, got {type(data['entries']).__name__}"
345
+ )