exonware-xwlazy 0.1.0.11__py3-none-any.whl → 0.1.0.19__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 (96) hide show
  1. exonware/__init__.py +22 -0
  2. exonware/xwlazy/__init__.py +0 -0
  3. exonware/xwlazy/common/__init__.py +47 -0
  4. exonware/xwlazy/common/base.py +58 -0
  5. exonware/xwlazy/common/cache.py +506 -0
  6. exonware/xwlazy/common/logger.py +268 -0
  7. exonware/xwlazy/common/services/__init__.py +72 -0
  8. exonware/xwlazy/common/services/dependency_mapper.py +234 -0
  9. exonware/xwlazy/common/services/install_async_utils.py +169 -0
  10. exonware/xwlazy/common/services/install_cache_utils.py +257 -0
  11. exonware/xwlazy/common/services/keyword_detection.py +292 -0
  12. exonware/xwlazy/common/services/spec_cache.py +173 -0
  13. exonware/xwlazy/common/strategies/__init__.py +28 -0
  14. exonware/xwlazy/common/strategies/caching_dict.py +45 -0
  15. exonware/xwlazy/common/strategies/caching_installation.py +89 -0
  16. exonware/xwlazy/common/strategies/caching_lfu.py +67 -0
  17. exonware/xwlazy/common/strategies/caching_lru.py +64 -0
  18. exonware/xwlazy/common/strategies/caching_multitier.py +60 -0
  19. exonware/xwlazy/common/strategies/caching_ttl.py +60 -0
  20. {xwlazy/lazy → exonware/xwlazy}/config.py +52 -20
  21. exonware/xwlazy/contracts.py +1410 -0
  22. exonware/xwlazy/defs.py +397 -0
  23. xwlazy/lazy/lazy_errors.py → exonware/xwlazy/errors.py +21 -8
  24. exonware/xwlazy/facade.py +1049 -0
  25. exonware/xwlazy/module/__init__.py +18 -0
  26. exonware/xwlazy/module/base.py +569 -0
  27. exonware/xwlazy/module/data.py +17 -0
  28. exonware/xwlazy/module/facade.py +247 -0
  29. exonware/xwlazy/module/importer_engine.py +2161 -0
  30. exonware/xwlazy/module/strategies/__init__.py +22 -0
  31. exonware/xwlazy/module/strategies/module_helper_lazy.py +94 -0
  32. exonware/xwlazy/module/strategies/module_helper_simple.py +66 -0
  33. exonware/xwlazy/module/strategies/module_manager_advanced.py +112 -0
  34. exonware/xwlazy/module/strategies/module_manager_simple.py +96 -0
  35. exonware/xwlazy/package/__init__.py +18 -0
  36. exonware/xwlazy/package/base.py +807 -0
  37. xwlazy/lazy/host_conf.py → exonware/xwlazy/package/conf.py +62 -10
  38. exonware/xwlazy/package/data.py +17 -0
  39. exonware/xwlazy/package/facade.py +481 -0
  40. exonware/xwlazy/package/services/__init__.py +84 -0
  41. exonware/xwlazy/package/services/async_install_handle.py +89 -0
  42. exonware/xwlazy/package/services/config_manager.py +246 -0
  43. exonware/xwlazy/package/services/discovery.py +374 -0
  44. {xwlazy/lazy → exonware/xwlazy/package/services}/host_packages.py +43 -16
  45. exonware/xwlazy/package/services/install_async.py +278 -0
  46. exonware/xwlazy/package/services/install_cache.py +146 -0
  47. exonware/xwlazy/package/services/install_interactive.py +60 -0
  48. exonware/xwlazy/package/services/install_policy.py +158 -0
  49. exonware/xwlazy/package/services/install_registry.py +56 -0
  50. exonware/xwlazy/package/services/install_result.py +17 -0
  51. exonware/xwlazy/package/services/install_sbom.py +154 -0
  52. exonware/xwlazy/package/services/install_utils.py +83 -0
  53. exonware/xwlazy/package/services/installer_engine.py +408 -0
  54. exonware/xwlazy/package/services/lazy_installer.py +720 -0
  55. {xwlazy/lazy → exonware/xwlazy/package/services}/manifest.py +42 -25
  56. exonware/xwlazy/package/services/strategy_registry.py +188 -0
  57. exonware/xwlazy/package/strategies/__init__.py +57 -0
  58. exonware/xwlazy/package/strategies/package_discovery_file.py +130 -0
  59. exonware/xwlazy/package/strategies/package_discovery_hybrid.py +85 -0
  60. exonware/xwlazy/package/strategies/package_discovery_manifest.py +102 -0
  61. exonware/xwlazy/package/strategies/package_execution_async.py +114 -0
  62. exonware/xwlazy/package/strategies/package_execution_cached.py +91 -0
  63. exonware/xwlazy/package/strategies/package_execution_pip.py +100 -0
  64. exonware/xwlazy/package/strategies/package_execution_wheel.py +107 -0
  65. exonware/xwlazy/package/strategies/package_mapping_discovery_first.py +101 -0
  66. exonware/xwlazy/package/strategies/package_mapping_hybrid.py +106 -0
  67. exonware/xwlazy/package/strategies/package_mapping_manifest_first.py +101 -0
  68. exonware/xwlazy/package/strategies/package_policy_allow_list.py +58 -0
  69. exonware/xwlazy/package/strategies/package_policy_deny_list.py +58 -0
  70. exonware/xwlazy/package/strategies/package_policy_permissive.py +47 -0
  71. exonware/xwlazy/package/strategies/package_timing_clean.py +68 -0
  72. exonware/xwlazy/package/strategies/package_timing_full.py +67 -0
  73. exonware/xwlazy/package/strategies/package_timing_smart.py +69 -0
  74. exonware/xwlazy/package/strategies/package_timing_temporary.py +67 -0
  75. exonware/xwlazy/runtime/__init__.py +18 -0
  76. exonware/xwlazy/runtime/adaptive_learner.py +131 -0
  77. exonware/xwlazy/runtime/base.py +276 -0
  78. exonware/xwlazy/runtime/facade.py +95 -0
  79. exonware/xwlazy/runtime/intelligent_selector.py +173 -0
  80. exonware/xwlazy/runtime/metrics.py +64 -0
  81. exonware/xwlazy/runtime/performance.py +39 -0
  82. exonware/xwlazy/version.py +2 -2
  83. {exonware_xwlazy-0.1.0.11.dist-info → exonware_xwlazy-0.1.0.19.dist-info}/METADATA +86 -10
  84. exonware_xwlazy-0.1.0.19.dist-info/RECORD +87 -0
  85. exonware_xwlazy-0.1.0.11.dist-info/RECORD +0 -20
  86. xwlazy/__init__.py +0 -34
  87. xwlazy/lazy/__init__.py +0 -301
  88. xwlazy/lazy/bootstrap.py +0 -106
  89. xwlazy/lazy/lazy_base.py +0 -465
  90. xwlazy/lazy/lazy_contracts.py +0 -290
  91. xwlazy/lazy/lazy_core.py +0 -3727
  92. xwlazy/lazy/logging_utils.py +0 -194
  93. xwlazy/version.py +0 -77
  94. /xwlazy/lazy/lazy_state.py → /exonware/xwlazy/common/services/state_manager.py +0 -0
  95. {exonware_xwlazy-0.1.0.11.dist-info → exonware_xwlazy-0.1.0.19.dist-info}/WHEEL +0 -0
  96. {exonware_xwlazy-0.1.0.11.dist-info → exonware_xwlazy-0.1.0.19.dist-info}/licenses/LICENSE +0 -0
@@ -1,9 +1,18 @@
1
- """Host-facing configuration helpers for enabling lazy mode via `exonware.conf`.
1
+ """
2
+ #exonware/xwlazy/src/exonware/xwlazy/package/conf.py
3
+
4
+ Host-facing configuration helpers for enabling lazy mode via `exonware.conf`.
2
5
 
3
6
  This module centralizes the legacy configuration surface so host packages no
4
7
  longer need to ship their own lazy bootstrap logic. Consumers import
5
8
  ``exonware.conf`` as before, while the real implementation now lives in
6
- ``xwlazy.lazy.host_conf`` to keep lazy concerns within the xwlazy project.
9
+ ``xwlazy.package.conf`` to keep lazy concerns within the xwlazy project.
10
+
11
+ Company: eXonware.com
12
+ Author: Eng. Muhammad AlShehri
13
+ Email: connect@exonware.com
14
+ Version: 0.1.0.19
15
+ Generation Date: 10-Oct-2025
7
16
  """
8
17
 
9
18
  from __future__ import annotations
@@ -16,14 +25,19 @@ import types
16
25
  import warnings
17
26
  from typing import Any, Dict, Optional
18
27
 
19
- from .host_packages import refresh_host_package
20
- from .lazy_core import (
28
+ # Import from new structure
29
+ from .services.host_packages import refresh_host_package
30
+ from ..facade import (
21
31
  config_package_lazy_install_enabled,
22
32
  install_import_hook,
23
33
  uninstall_import_hook,
24
34
  is_import_hook_installed,
25
35
  is_lazy_install_enabled,
26
36
  )
37
+ from ..defs import get_preset_mode
38
+ from .services.config_manager import LazyInstallConfig
39
+
40
+ __all__ = ['get_conf_module', '_PackageConfig', '_FilteredStderr', '_LazyConfModule', '_setup_global_warning_filter']
27
41
 
28
42
 
29
43
  class _PackageConfig:
@@ -42,7 +56,8 @@ class _PackageConfig:
42
56
  def lazy_install(self, value: bool) -> None:
43
57
  """Enable/disable lazy mode for this package."""
44
58
  if value:
45
- config_package_lazy_install_enabled(self._package_name, True, install_hook=False)
59
+ # Default to "smart" mode when enabling lazy install
60
+ config_package_lazy_install_enabled(self._package_name, True, mode="smart", install_hook=False)
46
61
  install_import_hook(self._package_name)
47
62
  refresh_host_package(self._package_name)
48
63
  else:
@@ -120,7 +135,7 @@ class _LazyConfModule(types.ModuleType):
120
135
  return False
121
136
  except Exception:
122
137
  try:
123
- import xwlazy # noqa: F401
138
+ import exonware.xwlazy # noqa: F401
124
139
  return True
125
140
  except ImportError:
126
141
  return False
@@ -205,6 +220,21 @@ class _LazyConfModule(types.ModuleType):
205
220
 
206
221
  if name == "lazy_install":
207
222
  return self._is_xwlazy_installed()
223
+ if name == "lazy":
224
+ # Return current lazy mode setting
225
+ # Check if any package has lazy enabled and return its mode
226
+ for pkg_name in package_names:
227
+ if is_lazy_install_enabled(pkg_name):
228
+ mode_config = LazyInstallConfig.get_mode_config(pkg_name)
229
+ if mode_config:
230
+ # Return preset name if matches, otherwise return mode string
231
+ from ..defs import PRESET_MODES
232
+ for preset_name, preset_config in PRESET_MODES.items():
233
+ if (preset_config.load_mode == mode_config.load_mode and
234
+ preset_config.install_mode == mode_config.install_mode):
235
+ return preset_name
236
+ return LazyInstallConfig.get_mode(pkg_name)
237
+ return "none"
208
238
  if name == "lazy_install_status":
209
239
  return self._get_global_lazy_status
210
240
  if name == "is_lazy_active":
@@ -221,11 +251,35 @@ class _LazyConfModule(types.ModuleType):
221
251
  if name == "lazy_install":
222
252
  if value:
223
253
  self._ensure_xwlazy_installed()
224
- self.__getattr__("xwsystem").lazy_install = True
254
+ # Enable with "smart" mode by default
255
+ package_names = ("xwsystem", "xwnode", "xwdata", "xwschema", "xwaction", "xwentity")
256
+ for pkg_name in package_names:
257
+ config_package_lazy_install_enabled(pkg_name, True, mode="smart", install_hook=True)
225
258
  else:
226
- self.__getattr__("xwsystem").lazy_install = False
259
+ package_names = ("xwsystem", "xwnode", "xwdata", "xwschema", "xwaction", "xwentity")
260
+ for pkg_name in package_names:
261
+ config_package_lazy_install_enabled(pkg_name, False, install_hook=False)
262
+ uninstall_import_hook(pkg_name)
227
263
  self._uninstall_xwlazy()
228
264
  return
265
+ if name == "lazy":
266
+ # Support exonware.conf.lazy = "lite"/"smart"/"full"/"clean"/"auto"
267
+ mode_map = {
268
+ "lite": "lite",
269
+ "smart": "smart",
270
+ "full": "full",
271
+ "clean": "clean",
272
+ "auto": "auto",
273
+ "temporary": "temporary",
274
+ "size_aware": "size_aware",
275
+ "none": "none",
276
+ }
277
+ mode = mode_map.get(str(value).lower(), "smart") # Default to "smart" instead of "auto"
278
+ # Apply to all known packages
279
+ package_names = ("xwsystem", "xwnode", "xwdata", "xwschema", "xwaction", "xwentity")
280
+ for pkg_name in package_names:
281
+ config_package_lazy_install_enabled(pkg_name, True, mode, install_hook=True)
282
+ return
229
283
  if name == "suppress_warnings":
230
284
  self._suppress_warnings = bool(value)
231
285
  self._setup_warning_filter()
@@ -275,5 +329,3 @@ def get_conf_module(name: str = "exonware.conf", doc: Optional[str] = None) -> t
275
329
  if _CONF_INSTANCE is None:
276
330
  _CONF_INSTANCE = _LazyConfModule(name, doc)
277
331
  return _CONF_INSTANCE
278
-
279
-
@@ -0,0 +1,17 @@
1
+ """
2
+ Package Data - Immutable data structure for packages.
3
+
4
+ Company: eXonware.com
5
+ Author: Eng. Muhammad AlShehri
6
+ Email: connect@exonware.com
7
+ Version: 0.1.0.19
8
+ Generation Date: 15-Nov-2025
9
+
10
+ Re-export PackageData from defs.py for backward compatibility.
11
+ """
12
+
13
+ # Re-export from defs.py
14
+ from ..defs import PackageData
15
+
16
+ __all__ = ['PackageData']
17
+
@@ -0,0 +1,481 @@
1
+ """
2
+ Package Operations Facade
3
+
4
+ Main facade: XWPackageHelper extends APackageHelper
5
+ Provides concrete implementation for all package operations.
6
+ Uses strategy pattern for caching, helper, and manager strategies.
7
+ """
8
+
9
+ import sys
10
+ import subprocess
11
+ import importlib
12
+ import importlib.util
13
+ from typing import Optional
14
+
15
+ from .base import APackageHelper
16
+ from .services import InstallerEngine, LazyInstaller
17
+ from .services.strategy_registry import StrategyRegistry
18
+ from ..common.cache import InstallationCache
19
+ from ..common.logger import get_logger
20
+
21
+ logger = get_logger("xwlazy.package.facade")
22
+
23
+ # Import strategy interfaces
24
+ from ..contracts import (
25
+ ICachingStrategy,
26
+ IPackageHelperStrategy,
27
+ IPackageManagerStrategy,
28
+ IInstallExecutionStrategy,
29
+ IInstallTimingStrategy,
30
+ IDiscoveryStrategy,
31
+ IPolicyStrategy,
32
+ IMappingStrategy,
33
+ )
34
+
35
+ # Import default strategies
36
+ from ..common.strategies import InstallationCacheWrapper
37
+ from .strategies import (
38
+ # Execution strategies
39
+ PipExecution,
40
+ # Timing strategies
41
+ SmartTiming,
42
+ # Discovery strategies
43
+ HybridDiscovery,
44
+ # Policy strategies
45
+ PermissivePolicy,
46
+ # Mapping strategies
47
+ ManifestFirstMapping,
48
+ )
49
+
50
+
51
+ class XWPackageHelper(APackageHelper):
52
+ """
53
+ Concrete implementation of APackageHelper.
54
+
55
+ Provides simple, clean API for working with packages (what you pip install).
56
+ Uses xwlazy's InstallationCache for persistent caching and LazyInstaller for installation.
57
+ """
58
+
59
+ def __init__(
60
+ self,
61
+ package_name: str = 'default',
62
+ project_root: Optional[str] = None,
63
+ *,
64
+ # Legacy strategy injection (for backward compatibility)
65
+ caching_strategy: Optional[ICachingStrategy] = None,
66
+ helper_strategy: Optional[IPackageHelperStrategy] = None,
67
+ manager_strategy: Optional[IPackageManagerStrategy] = None,
68
+ # New strategy types
69
+ execution_strategy: Optional[IInstallExecutionStrategy] = None,
70
+ timing_strategy: Optional[IInstallTimingStrategy] = None,
71
+ discovery_strategy: Optional[IDiscoveryStrategy] = None,
72
+ policy_strategy: Optional[IPolicyStrategy] = None,
73
+ mapping_strategy: Optional[IMappingStrategy] = None,
74
+ ):
75
+ """
76
+ Initialize XW package helper.
77
+
78
+ Args:
79
+ package_name: Package name for isolation (defaults to 'default')
80
+ project_root: Root directory of project (auto-detected if None)
81
+ caching_strategy: Optional caching strategy. If None, uses InstallationCache.
82
+ helper_strategy: Optional helper strategy (legacy, deprecated).
83
+ manager_strategy: Optional manager strategy (legacy, deprecated).
84
+ execution_strategy: Optional execution strategy. If None, uses PipExecution.
85
+ timing_strategy: Optional timing strategy. If None, uses SmartTiming.
86
+ discovery_strategy: Optional discovery strategy. If None, uses HybridDiscovery.
87
+ policy_strategy: Optional policy strategy. If None, uses PermissivePolicy.
88
+ mapping_strategy: Optional mapping strategy. If None, uses ManifestFirstMapping.
89
+ """
90
+ super().__init__(package_name, project_root)
91
+
92
+ # Default strategies (legacy - deprecated, kept for backward compatibility)
93
+ if caching_strategy is None:
94
+ caching_strategy = InstallationCacheWrapper()
95
+ # Legacy helper_strategy and manager_strategy are deprecated
96
+ # They are kept for backward compatibility but not used
97
+
98
+ # Check registry for stored strategies, otherwise use defaults
99
+ if execution_strategy is None:
100
+ execution_strategy = StrategyRegistry.get_package_strategy(package_name, 'execution')
101
+ if execution_strategy is None:
102
+ execution_strategy = PipExecution()
103
+ if timing_strategy is None:
104
+ timing_strategy = StrategyRegistry.get_package_strategy(package_name, 'timing')
105
+ if timing_strategy is None:
106
+ timing_strategy = SmartTiming()
107
+ if discovery_strategy is None:
108
+ discovery_strategy = StrategyRegistry.get_package_strategy(package_name, 'discovery')
109
+ if discovery_strategy is None:
110
+ discovery_strategy = HybridDiscovery(package_name, project_root)
111
+ if policy_strategy is None:
112
+ policy_strategy = StrategyRegistry.get_package_strategy(package_name, 'policy')
113
+ if policy_strategy is None:
114
+ policy_strategy = PermissivePolicy()
115
+ if mapping_strategy is None:
116
+ mapping_strategy = StrategyRegistry.get_package_strategy(package_name, 'mapping')
117
+ if mapping_strategy is None:
118
+ mapping_strategy = ManifestFirstMapping(package_name)
119
+
120
+ # Store strategies
121
+ self._caching = caching_strategy
122
+ self._helper = helper_strategy # Legacy, deprecated
123
+ self._manager = manager_strategy # Legacy, deprecated
124
+ self._execution = execution_strategy
125
+ self._timing = timing_strategy
126
+ self._discovery = discovery_strategy
127
+ self._policy = policy_strategy
128
+ self._mapping = mapping_strategy
129
+
130
+ # Legacy support (for backward compatibility)
131
+ self._install_cache = InstallationCache()
132
+ self._installer = None # Lazy init to avoid circular imports
133
+ self._install_engine = InstallerEngine(package_name)
134
+
135
+ def _get_installer(self):
136
+ """Get lazy installer instance (lazy init)."""
137
+ if self._installer is None:
138
+ self._installer = LazyInstaller(self._package_name)
139
+ return self._installer
140
+
141
+ def _check_importability(self, package_name: str) -> bool:
142
+ """
143
+ Check if package is importable.
144
+
145
+ Uses importlib.util.find_spec to check if package can be imported.
146
+
147
+ Args:
148
+ package_name: Package name to check
149
+
150
+ Returns:
151
+ True if importable, False otherwise
152
+ """
153
+ try:
154
+ spec = importlib.util.find_spec(package_name)
155
+ return spec is not None
156
+ except (ValueError, AttributeError, ImportError):
157
+ return False
158
+
159
+ def _check_persistent_cache(self, package_name: str) -> bool:
160
+ """
161
+ Check persistent cache for package installation status.
162
+
163
+ Args:
164
+ package_name: Package name to check
165
+
166
+ Returns:
167
+ True if found in persistent cache as installed, False otherwise
168
+ """
169
+ return self._install_cache.is_installed(package_name)
170
+
171
+ def _mark_installed_in_persistent_cache(self, package_name: str) -> None:
172
+ """
173
+ Mark package as installed in persistent cache.
174
+
175
+ Args:
176
+ package_name: Package name to mark
177
+ """
178
+ version = self._get_installer()._get_installed_version(package_name)
179
+ self._install_cache.mark_installed(package_name, version)
180
+
181
+ def _mark_uninstalled_in_persistent_cache(self, package_name: str) -> None:
182
+ """
183
+ Mark package as uninstalled in persistent cache.
184
+
185
+ Args:
186
+ package_name: Package name to mark
187
+ """
188
+ self._install_cache.mark_uninstalled(package_name)
189
+
190
+ def _run_install(self, *package_names: str) -> None:
191
+ """
192
+ Run pip install for packages.
193
+
194
+ Uses execution strategy and timing strategy to determine when/how to install.
195
+
196
+ Args:
197
+ *package_names: Package names to install
198
+
199
+ Raises:
200
+ RuntimeError: If installation fails
201
+ """
202
+ if not package_names:
203
+ return
204
+
205
+ # Get policy args for each package
206
+ policy_args_map = {}
207
+ for package_name in package_names:
208
+ # Check if should install now (timing strategy)
209
+ if not self._timing.should_install_now(package_name, None):
210
+ continue
211
+
212
+ # Get pip args from policy strategy
213
+ policy_args = self._policy.get_pip_args(package_name)
214
+ policy_args_map[package_name] = policy_args
215
+
216
+ # Execute installations using execution strategy
217
+ for package_name, policy_args in policy_args_map.items():
218
+ result = self._execution.execute_install(package_name, policy_args)
219
+
220
+ # Handle result
221
+ if hasattr(result, 'success') and result.success:
222
+ with self._lock:
223
+ self._installed_packages.add(package_name)
224
+ self._uninstalled_packages.discard(package_name)
225
+ self._mark_installed_in_persistent_cache(package_name)
226
+ else:
227
+ with self._lock:
228
+ self._failed_packages.add(package_name)
229
+ error_msg = getattr(result, 'error', 'Unknown error') if hasattr(result, 'error') else str(result)
230
+ raise RuntimeError(f"Failed to install {package_name}: {error_msg}")
231
+
232
+ def _run_uninstall(self, *package_names: str) -> None:
233
+ """
234
+ Run pip uninstall for packages.
235
+
236
+ Uses execution strategy for uninstallation.
237
+
238
+ Args:
239
+ *package_names: Package names to uninstall
240
+
241
+ Raises:
242
+ RuntimeError: If uninstallation fails
243
+ """
244
+ if not package_names:
245
+ return
246
+
247
+ for package_name in package_names:
248
+ # Check if should uninstall (timing strategy)
249
+ if self._timing.should_uninstall_after(package_name, None):
250
+ success = self._execution.execute_uninstall(package_name)
251
+ if success:
252
+ with self._lock:
253
+ self._installed_packages.discard(package_name)
254
+ self._uninstalled_packages.add(package_name)
255
+ self._mark_uninstalled_in_persistent_cache(package_name)
256
+ else:
257
+ raise RuntimeError(f"Failed to uninstall {package_name}")
258
+
259
+ # Abstract methods from APackage that need implementation
260
+ def _discover_from_sources(self) -> None:
261
+ """Discover dependencies from all sources."""
262
+ # Use discovery strategy
263
+ deps = self._discovery.discover(self._project_root)
264
+ # Convert to DependencyInfo format
265
+ from ..defs import DependencyInfo
266
+ for import_name, package_name in deps.items():
267
+ self.discovered_dependencies[import_name] = DependencyInfo(
268
+ import_name=import_name,
269
+ package_name=package_name,
270
+ source=self._discovery.get_source(import_name) or 'discovery',
271
+ category='discovered'
272
+ )
273
+
274
+ def _is_cache_valid(self) -> bool:
275
+ """Check if cached dependencies are still valid."""
276
+ # Delegate to discovery strategy which has cache validation logic
277
+ if hasattr(self._discovery, '_is_cache_valid'):
278
+ return self._discovery._is_cache_valid()
279
+ # Fallback: if discovery doesn't support cache validation, assume invalid
280
+ return False
281
+
282
+ def _add_common_mappings(self) -> None:
283
+ """Add common import -> package mappings."""
284
+ # Use mapping strategy to discover mappings
285
+ # This is called during initialization to populate common mappings
286
+ # The mapping strategy handles this internally
287
+ pass
288
+
289
+ def _update_file_mtimes(self) -> None:
290
+ """Update file modification times for cache validation."""
291
+ # Delegate to discovery strategy which tracks file modification times
292
+ if hasattr(self._discovery, '_update_file_mtimes'):
293
+ self._discovery._update_file_mtimes()
294
+
295
+ # Strategy swapping methods
296
+ def swap_cache_strategy(self, new_strategy: ICachingStrategy) -> None:
297
+ """
298
+ Swap cache strategy at runtime.
299
+
300
+ Args:
301
+ new_strategy: New caching strategy to use
302
+ """
303
+ self._caching = new_strategy
304
+ # Update manager if it uses caching
305
+ if hasattr(self._manager, '_caching'):
306
+ self._manager._caching = new_strategy
307
+
308
+ def swap_helper_strategy(self, new_strategy: IPackageHelperStrategy) -> None:
309
+ """
310
+ Swap helper/installer strategy at runtime.
311
+
312
+ Args:
313
+ new_strategy: New helper strategy to use
314
+ """
315
+ self._helper = new_strategy
316
+ # Update manager if it uses helper
317
+ if hasattr(self._manager, '_helper'):
318
+ self._manager._helper = new_strategy
319
+
320
+ def swap_manager_strategy(self, new_strategy: IPackageManagerStrategy) -> None:
321
+ """
322
+ Swap manager strategy at runtime.
323
+
324
+ Args:
325
+ new_strategy: New manager strategy to use
326
+ """
327
+ self._manager = new_strategy
328
+
329
+ def swap_execution_strategy(self, new_strategy: IInstallExecutionStrategy) -> None:
330
+ """
331
+ Swap execution strategy at runtime.
332
+
333
+ Args:
334
+ new_strategy: New execution strategy to use
335
+ """
336
+ self._execution = new_strategy
337
+
338
+ def swap_timing_strategy(self, new_strategy: IInstallTimingStrategy) -> None:
339
+ """
340
+ Swap timing strategy at runtime.
341
+
342
+ Args:
343
+ new_strategy: New timing strategy to use
344
+ """
345
+ self._timing = new_strategy
346
+
347
+ def swap_discovery_strategy(self, new_strategy: IDiscoveryStrategy) -> None:
348
+ """
349
+ Swap discovery strategy at runtime.
350
+
351
+ Args:
352
+ new_strategy: New discovery strategy to use
353
+ """
354
+ self._discovery = new_strategy
355
+
356
+ def swap_policy_strategy(self, new_strategy: IPolicyStrategy) -> None:
357
+ """
358
+ Swap policy strategy at runtime.
359
+
360
+ Args:
361
+ new_strategy: New policy strategy to use
362
+ """
363
+ self._policy = new_strategy
364
+
365
+ def swap_mapping_strategy(self, new_strategy: IMappingStrategy) -> None:
366
+ """
367
+ Swap mapping strategy at runtime.
368
+
369
+ Args:
370
+ new_strategy: New mapping strategy to use
371
+ """
372
+ self._mapping = new_strategy
373
+
374
+ def install_package(self, package_name: str, module_name: Optional[str] = None) -> bool:
375
+ """
376
+ Install a package.
377
+
378
+ Uses timing strategy to determine if should install now,
379
+ then uses execution strategy to perform installation.
380
+
381
+ Args:
382
+ package_name: Package name to install
383
+ module_name: Optional module name (for mapping)
384
+
385
+ Returns:
386
+ True if installed successfully, False otherwise
387
+ """
388
+ # Map module name to package name if needed (using mapping strategy)
389
+ if module_name and not package_name:
390
+ package_name = self._mapping.map_import_to_package(module_name) or module_name
391
+
392
+ # Check timing strategy
393
+ if not self._timing.should_install_now(package_name, {'module_name': module_name}):
394
+ return False
395
+
396
+ # Check policy strategy
397
+ allowed, reason = self._policy.is_allowed(package_name)
398
+ if not allowed:
399
+ raise RuntimeError(f"Package {package_name} blocked by policy: {reason}")
400
+
401
+ # Get pip args from policy
402
+ policy_args = self._policy.get_pip_args(package_name)
403
+
404
+ # Execute installation
405
+ result = self._execution.execute_install(package_name, policy_args)
406
+
407
+ # Handle result
408
+ if hasattr(result, 'success') and result.success:
409
+ with self._lock:
410
+ self._installed_packages.add(package_name)
411
+ self._uninstalled_packages.discard(package_name)
412
+ self._mark_installed_in_persistent_cache(package_name)
413
+ return True
414
+ else:
415
+ with self._lock:
416
+ self._failed_packages.add(package_name)
417
+ return False
418
+
419
+ def _check_security_policy(self, package_name: str):
420
+ """Check security policy for package."""
421
+ # Use policy strategy
422
+ return self._policy.is_allowed(package_name)
423
+
424
+ def _run_pip_install(self, package_name: str, args: list) -> bool:
425
+ """Run pip install with arguments."""
426
+ try:
427
+ self._run_install(package_name)
428
+ return True
429
+ except (RuntimeError, subprocess.CalledProcessError, OSError) as e:
430
+ logger.debug(f"Failed to install {package_name}: {e}")
431
+ return False
432
+ except Exception as e:
433
+ # Catch-all for unexpected errors, but log them
434
+ logger.warning(f"Unexpected error installing {package_name}: {e}")
435
+ return False
436
+
437
+ def is_cache_valid(self, key: str) -> bool:
438
+ """Check if cache entry is still valid."""
439
+ # Use caching strategy if it supports validation
440
+ if hasattr(self._caching, 'is_valid') and self._caching is not None:
441
+ return self._caching.is_valid(key)
442
+ # Fallback: check if key exists in cache
443
+ return self.get_cached(key) is not None
444
+
445
+ # IConfigManager methods (delegate to LazyInstallConfig)
446
+ def is_enabled(self, package_name: str) -> bool:
447
+ """
448
+ Check if lazy install is enabled for a package (from IConfigManager).
449
+
450
+ This method delegates to LazyInstallConfig to avoid method name conflict
451
+ with the instance method is_enabled().
452
+
453
+ Args:
454
+ package_name: Package name to check
455
+
456
+ Returns:
457
+ True if enabled, False otherwise
458
+ """
459
+ from .services.config_manager import LazyInstallConfig
460
+ return LazyInstallConfig.is_enabled(package_name)
461
+
462
+ def get_mode(self, package_name: str) -> str:
463
+ """Get installation mode for a package (from IConfigManager)."""
464
+ from .services.config_manager import LazyInstallConfig
465
+ return LazyInstallConfig.get_mode(package_name)
466
+
467
+ def get_load_mode(self, package_name: str):
468
+ """Get load mode for a package (from IConfigManager)."""
469
+ from .services.config_manager import LazyInstallConfig
470
+ return LazyInstallConfig.get_load_mode(package_name)
471
+
472
+ def get_install_mode(self, package_name: str):
473
+ """Get install mode for a package (from IConfigManager)."""
474
+ from .services.config_manager import LazyInstallConfig
475
+ return LazyInstallConfig.get_install_mode(package_name)
476
+
477
+ def get_mode_config(self, package_name: str):
478
+ """Get full mode configuration for a package (from IConfigManager)."""
479
+ from .services.config_manager import LazyInstallConfig
480
+ return LazyInstallConfig.get_mode_config(package_name)
481
+