Coverage for src / ezcompiler / services / uploader_service.py: 38.89%
30 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 06:49 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 06:49 +0000
1# ///////////////////////////////////////////////////////////////
2# UPLOADER_SERVICE - Upload orchestration service
3# Project: ezcompiler
4# ///////////////////////////////////////////////////////////////
6"""
7Uploader service - Upload orchestration service for EzCompiler.
9This module provides the UploaderService class that orchestrates file
10and directory uploads using different upload backends (disk, server).
12Services layer can use WARNING and ERROR log levels.
13"""
15from __future__ import annotations
17# ///////////////////////////////////////////////////////////////
18# IMPORTS
19# ///////////////////////////////////////////////////////////////
20# Standard library imports
21from pathlib import Path
22from typing import Any, Literal
24# Local imports
25from ..adapters.uploader_factory import UploaderFactory
26from ..shared.exceptions import UploadError
27from ..utils.validators import validate_upload_structure
29# ///////////////////////////////////////////////////////////////
30# TYPE ALIASES
31# ///////////////////////////////////////////////////////////////
33UploadType = Literal["disk", "server"]
35# ///////////////////////////////////////////////////////////////
36# CLASSES
37# ///////////////////////////////////////////////////////////////
40class UploaderService:
41 """
42 Upload orchestration service.
44 Orchestrates file and directory uploads using different upload backends.
45 Handles upload type selection, validation, and execution.
47 Example:
48 >>> service = UploaderService()
49 >>> service.upload(
50 ... source_path=Path("dist.zip"),
51 ... upload_type="disk",
52 ... destination="releases/",
53 ... upload_config={"overwrite": True}
54 ... )
55 """
57 # ////////////////////////////////////////////////
58 # UPLOAD METHODS
59 # ////////////////////////////////////////////////
61 @staticmethod
62 def upload(
63 source_path: Path,
64 upload_type: UploadType,
65 destination: str,
66 upload_config: dict[str, Any] | None = None,
67 ) -> None:
68 """
69 Upload a file or directory to the specified destination.
71 Args:
72 source_path: Path to the source file or directory
73 upload_type: Type of upload ("disk" or "server")
74 destination: Destination path or URL
75 upload_config: Additional uploader configuration options
77 Raises:
78 UploadError: If upload fails or upload type is invalid
80 Example:
81 >>> UploaderService.upload(
82 ... Path("dist.zip"),
83 ... "disk",
84 ... "releases/",
85 ... {"overwrite": True}
86 ... )
87 """
88 try:
89 # Validate upload type
90 if not validate_upload_structure(upload_type):
91 raise UploadError(f"Invalid upload type: {upload_type}")
93 # Prepare uploader configuration
94 config = upload_config or {}
95 if upload_type == "disk":
96 config["destination_path"] = destination
97 elif upload_type == "server":
98 config["server_url"] = destination
100 # Create uploader and perform upload
101 uploader = UploaderFactory.create_uploader(upload_type, config)
102 uploader.upload(source_path=source_path, destination=destination)
103 except UploadError:
104 raise
105 except Exception as e:
106 raise UploadError(f"Upload failed: {str(e)}") from e
108 # ////////////////////////////////////////////////
109 # UTILITY METHODS
110 # ////////////////////////////////////////////////
112 @staticmethod
113 def get_supported_types() -> list[str]:
114 """
115 Get list of supported upload types.
117 Returns:
118 list[str]: List of supported upload type names
120 Example:
121 >>> types = UploaderService.get_supported_types()
122 >>> print(types)
123 ['disk', 'server']
124 """
125 return UploaderFactory.get_supported_types()
127 @staticmethod
128 def validate_upload_config(
129 upload_type: UploadType, config: dict[str, Any] | None = None
130 ) -> bool:
131 """
132 Validate configuration for a specific upload type.
134 Args:
135 upload_type: Type of uploader to validate
136 config: Configuration to validate (default: None)
138 Returns:
139 bool: True if configuration is valid, False otherwise
141 Example:
142 >>> is_valid = UploaderService.validate_upload_config(
143 ... "disk", {"overwrite": True}
144 ... )
145 """
146 return UploaderFactory.validate_config(upload_type, config)