core
napt.core
Core orchestration for NAPT.
This module provides high-level orchestration functions that coordinate the complete workflow for recipe validation, package building, and deployment.
Two-Path Architecture:
The orchestration automatically selects the optimal path based on what each discovery strategy can do:
-
Version-First Path (web_scrape, api_github, api_json): These strategies can check the version without downloading the file. NAPT compares the discovered version to the cached version. If they match and the file already exists, the download is skipped entirely. This makes update checks very fast (~100-300ms) since no large installer files are downloaded.
-
File-First Path (url_download): This strategy requires downloading the file to extract the version. NAPT uses HTTP ETag headers to check if the file has changed. If the server responds with HTTP 304 (Not Modified), the existing cached file is reused, avoiding unnecessary re-downloads.
Design Principles:
- Each function has a single, clear responsibility
- Functions return structured data (dataclasses) for easy testing and extension
- Error handling uses exceptions; CLI layer formats for user display
- Discovery strategies are dynamically loaded via registry pattern
- Configuration is immutable once loaded
Example
Programmatic usage:
from pathlib import Path
from napt.core import discover_recipe
result = discover_recipe(
recipe_path=Path("recipes/Google/chrome.yaml"),
output_dir=Path("./downloads"),
)
print(f"App: {result.app_name}")
print(f"Version: {result.version}")
print(f"SHA-256: {result.sha256}")
# Version-first strategies: may have skipped download if unchanged!
derive_file_path_from_url
Derive file path from URL using same logic as download_file.
This function ensures version-first strategies can locate cached files without downloading by following the same naming convention as the download module (app-scoped subdirectory).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Download URL. |
required |
output_dir
|
Path
|
Base downloads directory. |
required |
app_id
|
str
|
Application identifier used to scope the subdirectory. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Expected path to the file under output_dir/app_id/. |
Example
Get expected file path for a download URL:
Source code in napt/core.py
discover_recipe
discover_recipe(recipe_path: Path, output_dir: Path | None = None, state_file: Path | None = Path('state/versions.json'), stateless: bool = False) -> DiscoverResult
Discover the latest version by loading config and downloading installer.
This is the main entry point for the 'napt discover' command. It orchestrates the entire discovery workflow using a two-path architecture optimized for version-first strategies.
The function uses duck typing to detect strategy capabilities:
VERSION-FIRST PATH (if strategy has get_version_info method):
- Load effective configuration (org + vendor + recipe merged)
- Call strategy.get_version_info() to discover version (no download)
- Compare discovered version to cached known_version
- If match and file exists -> skip download entirely (fast path!)
- If changed or missing -> download installer via download_file()
- Update state and return results
FILE-FIRST PATH (if strategy has only discover_version method):
- Load effective configuration (org + vendor + recipe merged)
- Call strategy.discover_version() with cached ETag
- Strategy handles conditional request (HTTP 304 vs 200)
- Extract version from downloaded file
- Update state and return results
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recipe_path
|
Path
|
Path to the recipe YAML file. Must exist and be readable. The path is resolved to absolute form. |
required |
output_dir
|
Path | None
|
Directory to download the installer to. Created if it doesn't exist. The downloaded file will be named based on Content-Disposition header or URL path. |
None
|
state_file
|
Path | None
|
Path to state file for version tracking and ETag caching. Default is "state/versions.json". Set to None to disable. |
Path('state/versions.json')
|
stateless
|
bool
|
If True, disable state tracking (no caching, always download). Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
DiscoverResult
|
Discovery results and metadata including version, file path, and SHA-256 hash. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
On missing or invalid configuration fields (no app defined, missing 'source.strategy' field, unknown discovery strategy name), YAML parse errors (from config loader), or if recipe file doesn't exist. |
NetworkError
|
On download failures or version extraction errors. |
Example
Basic version discovery:
from pathlib import Path
result = discover_recipe(
Path("recipes/Google/chrome.yaml"),
Path("./downloads")
)
print(result.version) # 141.0.7390.123
Handling errors:
Note
The discovery strategy must be registered before calling this function. Version-first strategies (web_scrape, api_github, api_json) can skip downloads entirely when version unchanged (fast path optimization). File-first strategy (url_download) uses ETag conditional requests. Downloaded files are written atomically (.part then renamed). Progress output goes to stdout via the download module. Strategy type detected via duck typing (hasattr for get_version_info).
Source code in napt/core.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |