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,483 @@
1
+ #!/usr/bin/env python3
2
+ # ///////////////////////////////////////////////////////////////
3
+ # FILE SCANNER SERVICE - File Scanning Service
4
+ # Project: works-on-my-machine
5
+ # ///////////////////////////////////////////////////////////////
6
+
7
+ """
8
+ File Scanner Service - Singleton service for finding and filtering Python files.
9
+
10
+ Handles file discovery, security pattern filtering, and directory scanning.
11
+ Provides comprehensive file scanning capabilities with security validation
12
+ and pattern-based filtering for development tools.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ # ///////////////////////////////////////////////////////////////
18
+ # IMPORTS
19
+ # ///////////////////////////////////////////////////////////////
20
+ # Standard library imports
21
+ import logging
22
+ import time
23
+ from pathlib import Path
24
+ from threading import Lock
25
+ from typing import ClassVar
26
+
27
+ # Local imports
28
+ from ...exceptions.common import (
29
+ FileServiceError,
30
+ SecurityServiceError,
31
+ )
32
+ from ...shared.configs.security import FileScannerConfig
33
+ from ...shared.results import FileScanResult, FileSearchResult
34
+ from ...utils.common import (
35
+ contains_security_sensitive_pattern,
36
+ is_python_file,
37
+ should_exclude_path,
38
+ )
39
+ from .security_validator_service import SecurityValidatorService
40
+
41
+ # ///////////////////////////////////////////////////////////////
42
+ # FILE SCANNER SERVICE CLASS
43
+ # ///////////////////////////////////////////////////////////////
44
+
45
+
46
+ class FileScannerService:
47
+ """Singleton service for file discovery and filtering operations."""
48
+
49
+ _instance: ClassVar[FileScannerService | None] = None
50
+ _initialized: ClassVar[bool] = False
51
+ _lock: ClassVar[Lock] = Lock()
52
+
53
+ def __new__(cls) -> FileScannerService:
54
+ """Create or return the singleton instance.
55
+
56
+ Returns:
57
+ FileScannerService: The singleton instance
58
+ """
59
+ with cls._lock:
60
+ if cls._instance is None:
61
+ cls._instance = super().__new__(cls)
62
+ return cls._instance
63
+
64
+ def __init__(self) -> None:
65
+ """Initialize file scanner service (only once)."""
66
+ if FileScannerService._initialized:
67
+ return
68
+
69
+ self.logger = logging.getLogger(__name__)
70
+ self.security_validator = SecurityValidatorService()
71
+ FileScannerService._initialized = True
72
+
73
+ # ///////////////////////////////////////////////////////////////
74
+ # PUBLIC METHODS
75
+ # ///////////////////////////////////////////////////////////////
76
+
77
+ def find_python_files(
78
+ self, target_path: Path, recursive: bool = True
79
+ ) -> FileSearchResult:
80
+ """Find Python files in the given path.
81
+
82
+ Args:
83
+ target_path: Path to search (file or directory)
84
+ recursive: Whether to search recursively in subdirectories
85
+
86
+ Returns:
87
+ FileSearchResult: Result with list of Python file paths
88
+
89
+ Raises:
90
+ FileServiceError: If input validation fails
91
+ FileServiceError: If target path does not exist
92
+ FileServiceError: If directory access fails
93
+ FileServiceError: If security filtering fails
94
+ """
95
+ start_time = time.time()
96
+ try:
97
+ # Input validation
98
+ self._validate_target_path(target_path)
99
+
100
+ python_files = []
101
+
102
+ if target_path.is_file():
103
+ if is_python_file(target_path):
104
+ python_files.append(target_path)
105
+ elif target_path.is_dir():
106
+ python_files.extend(self._scan_directory(target_path, recursive))
107
+ else:
108
+ raise FileServiceError(
109
+ message="Path is neither a file nor a directory",
110
+ operation="find_python_files",
111
+ target_path=str(target_path),
112
+ details=(
113
+ f"Path type: {target_path.stat().st_mode if target_path.exists() else 'unknown'}"
114
+ ),
115
+ )
116
+
117
+ # Filter out files that match security patterns
118
+ filtered_files = self._filter_secure_files(python_files)
119
+
120
+ search_time = time.time() - start_time
121
+ self.logger.debug(
122
+ f"Found {len(filtered_files)} Python files in {target_path}"
123
+ )
124
+ return FileSearchResult(
125
+ success=True,
126
+ message=f"Found {len(filtered_files)} Python files",
127
+ target_path=target_path,
128
+ files_found=filtered_files,
129
+ recursive=recursive,
130
+ search_time=search_time,
131
+ )
132
+
133
+ except FileServiceError as e:
134
+ search_time = time.time() - start_time
135
+ # Re-raise specialized exceptions as-is but return result
136
+ return FileSearchResult(
137
+ success=False,
138
+ error=str(e),
139
+ target_path=target_path,
140
+ files_found=[],
141
+ recursive=recursive,
142
+ search_time=search_time,
143
+ )
144
+ except (OSError, TypeError, ValueError) as e:
145
+ search_time = time.time() - start_time
146
+ # Wrap unexpected external exceptions
147
+ return FileSearchResult(
148
+ success=False,
149
+ error=f"Unexpected error during file scanning: {e}",
150
+ target_path=target_path,
151
+ files_found=[],
152
+ recursive=recursive,
153
+ search_time=search_time,
154
+ )
155
+
156
+ def get_project_python_files(self, project_root: Path) -> FileSearchResult:
157
+ """Get all Python files in a project, excluding common non-source directories.
158
+
159
+ Args:
160
+ project_root: Root directory of the project
161
+
162
+ Returns:
163
+ FileSearchResult: Result with list of Python source files
164
+
165
+ Raises:
166
+ FileServiceError: If input validation fails
167
+ FileServiceError: If project root is invalid
168
+ FileServiceError: If directory access fails
169
+ FileServiceError: If security filtering fails
170
+ """
171
+ start_time = time.time()
172
+ try:
173
+ # Input validation
174
+ self._validate_project_root(project_root)
175
+
176
+ python_files = []
177
+
178
+ # Walk through all subdirectories, filtering by Python extensions
179
+ # at the glob level for performance (avoids enumerating non-Python files)
180
+ try:
181
+ for ext in FileScannerConfig.PYTHON_EXTENSIONS:
182
+ for item in project_root.rglob(f"*{ext}"):
183
+ if should_exclude_path(item):
184
+ continue
185
+
186
+ if item.is_file():
187
+ python_files.append(item)
188
+
189
+ except (PermissionError, OSError) as e:
190
+ search_time = time.time() - start_time
191
+ return FileSearchResult(
192
+ success=False,
193
+ error=f"Permission or OS error: {e}",
194
+ target_path=project_root,
195
+ files_found=[],
196
+ recursive=True,
197
+ search_time=search_time,
198
+ )
199
+
200
+ # Filter security patterns
201
+ filtered_files = self._filter_secure_files(python_files)
202
+
203
+ search_time = time.time() - start_time
204
+ self.logger.debug(
205
+ f"Found {len(filtered_files)} Python files in project {project_root.name}"
206
+ )
207
+ return FileSearchResult(
208
+ success=True,
209
+ message=f"Found {len(filtered_files)} Python files in project",
210
+ target_path=project_root,
211
+ files_found=filtered_files,
212
+ recursive=True,
213
+ search_time=search_time,
214
+ )
215
+
216
+ except FileServiceError as e:
217
+ search_time = time.time() - start_time
218
+ return FileSearchResult(
219
+ success=False,
220
+ error=str(e),
221
+ target_path=project_root,
222
+ files_found=[],
223
+ recursive=True,
224
+ search_time=search_time,
225
+ )
226
+ except (OSError, TypeError, ValueError) as e:
227
+ search_time = time.time() - start_time
228
+ return FileSearchResult(
229
+ success=False,
230
+ error=f"Unexpected error during project file scanning: {e}",
231
+ target_path=project_root,
232
+ files_found=[],
233
+ recursive=True,
234
+ search_time=search_time,
235
+ )
236
+
237
+ def get_scan_summary(
238
+ self, target_path: Path | list[Path] | None = None
239
+ ) -> FileScanResult:
240
+ """Get a summary of the scanning operation.
241
+
242
+ Args:
243
+ target_path: Path that was scanned, or list of already-scanned files.
244
+ If None, returns a generic summary.
245
+
246
+ Returns:
247
+ FileScanResult: Summary information with scan results
248
+
249
+ Raises:
250
+ FileServiceError: If scanning fails
251
+ """
252
+ start_time = time.time()
253
+ try:
254
+ if isinstance(target_path, list):
255
+ # Already have files, just create summary
256
+ python_files = target_path
257
+ actual_path = python_files[0].parent if python_files else None
258
+ elif target_path is not None:
259
+ # Need to scan
260
+ search_result = self.find_python_files(target_path)
261
+ if search_result.success and isinstance(
262
+ search_result.files_found, list
263
+ ):
264
+ python_files = search_result.files_found
265
+ else:
266
+ python_files = []
267
+ actual_path = target_path
268
+ else:
269
+ # No path provided, return empty summary
270
+ python_files = []
271
+ actual_path = None
272
+
273
+ scan_time = time.time() - start_time
274
+
275
+ return FileScanResult(
276
+ success=True,
277
+ message=f"Successfully scanned {len(python_files)} Python files",
278
+ target_path=actual_path,
279
+ total_files=len(python_files),
280
+ files_found=python_files,
281
+ excluded_dirs=list(FileScannerConfig.EXCLUDED_DIRS),
282
+ file_extensions=list(FileScannerConfig.PYTHON_EXTENSIONS),
283
+ scan_time=scan_time,
284
+ )
285
+ except (OSError, TypeError, ValueError) as e:
286
+ scan_time = time.time() - start_time
287
+ return FileScanResult(
288
+ success=False,
289
+ error=f"Scan failed: {e}",
290
+ target_path=target_path if isinstance(target_path, Path) else None,
291
+ total_files=0,
292
+ files_found=[],
293
+ excluded_dirs=list(FileScannerConfig.EXCLUDED_DIRS),
294
+ file_extensions=list(FileScannerConfig.PYTHON_EXTENSIONS),
295
+ scan_time=scan_time,
296
+ )
297
+
298
+ # ///////////////////////////////////////////////////////////////
299
+ # PRIVATE METHODS
300
+ # ///////////////////////////////////////////////////////////////
301
+
302
+ def _scan_directory(self, directory: Path, recursive: bool) -> list[Path]:
303
+ """Scan a directory for Python files.
304
+
305
+ Args:
306
+ directory: Directory to scan
307
+ recursive: Whether to scan recursively
308
+
309
+ Returns:
310
+ List[Path]: List of Python files found
311
+
312
+ Raises:
313
+ FileServiceError: If directory does not exist
314
+ FileServiceError: If directory access fails
315
+ """
316
+ try:
317
+ if not directory.exists():
318
+ raise FileServiceError(
319
+ message="Directory does not exist",
320
+ operation="scan_directory",
321
+ target_path=str(directory),
322
+ details=f"Directory {directory} was not found",
323
+ )
324
+
325
+ python_files = []
326
+
327
+ if recursive:
328
+ # Recursive scan - filter by Python extensions at glob level
329
+ try:
330
+ for ext in FileScannerConfig.PYTHON_EXTENSIONS:
331
+ for item in directory.rglob(f"*{ext}"):
332
+ if should_exclude_path(item):
333
+ continue
334
+
335
+ if item.is_file():
336
+ python_files.append(item)
337
+ except (PermissionError, OSError) as e:
338
+ raise FileServiceError(
339
+ message=f"Permission or OS error during recursive scan: {e}",
340
+ operation="recursive_scan",
341
+ target_path=str(directory),
342
+ details=f"Failed to recursively scan directory {directory}",
343
+ ) from e
344
+ else:
345
+ # Non-recursive scan
346
+ try:
347
+ for item in directory.iterdir():
348
+ if should_exclude_path(item):
349
+ continue
350
+
351
+ if item.is_file() and is_python_file(item):
352
+ python_files.append(item)
353
+ except (PermissionError, OSError) as e:
354
+ raise FileServiceError(
355
+ message=f"Permission or OS error during directory scan: {e}",
356
+ operation="directory_scan",
357
+ target_path=str(directory),
358
+ details=f"Failed to scan directory {directory}",
359
+ ) from e
360
+
361
+ return python_files
362
+
363
+ except FileServiceError:
364
+ # Re-raise specialized exceptions as-is
365
+ raise
366
+ except (OSError, TypeError, ValueError) as e:
367
+ # Wrap unexpected external exceptions
368
+ raise FileServiceError(
369
+ message=f"Unexpected error during directory scanning: {e}",
370
+ operation="scan_directory",
371
+ target_path=str(directory),
372
+ details=f"Exception type: {type(e).__name__}",
373
+ ) from e
374
+
375
+ def _validate_target_path(self, target_path: Path) -> None:
376
+ """Validate target path for file scanning.
377
+
378
+ Args:
379
+ target_path: Path to validate
380
+
381
+ Raises:
382
+ FileServiceError: If target path is invalid
383
+ FileServiceError: If target path does not exist
384
+ """
385
+ if not target_path:
386
+ raise FileServiceError(
387
+ message="Target path cannot be None",
388
+ operation="validate_target_path",
389
+ target_path="",
390
+ details="Input validation failed for file scanning operation",
391
+ )
392
+
393
+ if not target_path.exists():
394
+ raise FileServiceError(
395
+ message="Path does not exist",
396
+ operation="validate_target_path",
397
+ target_path=str(target_path),
398
+ details=f"Target path {target_path} was not found",
399
+ )
400
+
401
+ def _validate_project_root(self, project_root: Path) -> None:
402
+ """Validate project root for file scanning.
403
+
404
+ Args:
405
+ project_root: Project root to validate
406
+
407
+ Raises:
408
+ FileServiceError: If project root is invalid
409
+ FileServiceError: If project root does not exist or is not a directory
410
+ """
411
+ if not project_root:
412
+ raise FileServiceError(
413
+ message="Project root cannot be None",
414
+ operation="validate_project_root",
415
+ target_path="",
416
+ details="Input validation failed for project file scanning",
417
+ )
418
+
419
+ if not project_root.exists() or not project_root.is_dir():
420
+ raise FileServiceError(
421
+ message="Invalid project root",
422
+ operation="validate_project_root",
423
+ target_path=str(project_root),
424
+ details=(
425
+ f"Project root {project_root} does not exist or is not a directory"
426
+ ),
427
+ )
428
+
429
+ def _filter_secure_files(self, files: list[Path]) -> list[Path]:
430
+ """Filter files based on security patterns.
431
+
432
+ Args:
433
+ files: List of files to filter
434
+
435
+ Returns:
436
+ List[Path]: Filtered list of files
437
+
438
+ Raises:
439
+ FileServiceError: If security filtering fails
440
+ """
441
+ try:
442
+ filtered_files = []
443
+
444
+ for file_path in files:
445
+ try:
446
+ # Check if file path contains security-sensitive patterns
447
+ if contains_security_sensitive_pattern(file_path):
448
+ self.logger.debug(
449
+ f"Skipping security-sensitive file: {file_path}"
450
+ )
451
+ continue
452
+
453
+ # Additional security validation using SecurityValidator
454
+ try:
455
+ validation_result = self.security_validator.validate_file_path(
456
+ str(file_path)
457
+ )
458
+ if validation_result.is_valid:
459
+ filtered_files.append(file_path)
460
+ else:
461
+ # If security validation fails, log and skip the file
462
+ self.logger.warning(
463
+ f"Security validation failed for {file_path}: {validation_result.validation_reason}"
464
+ )
465
+ except (OSError, SecurityServiceError, TypeError, ValueError) as e:
466
+ # Security validation errors must fail closed.
467
+ self.logger.warning(
468
+ f"Security validation failed for {file_path}: {e}"
469
+ )
470
+
471
+ except (OSError, SecurityServiceError, TypeError, ValueError) as e:
472
+ # Log individual file processing errors but continue
473
+ self.logger.warning(f"Error processing file {file_path}: {e}")
474
+
475
+ return filtered_files
476
+
477
+ except (OSError, SecurityServiceError, TypeError, ValueError) as e:
478
+ raise FileServiceError(
479
+ message=f"Security filtering failed: {e}",
480
+ operation="filter_files",
481
+ target_path="multiple",
482
+ details=f"Failed to filter {len(files)} files",
483
+ ) from e