Coverage for src / ezcompiler / adapters / uploader_factory.py: 40.00%

34 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-27 06:49 +0000

1# /////////////////////////////////////////////////////////////// 

2# UPLOADER_FACTORY - Uploader factory for creating uploader instances 

3# Project: ezcompiler 

4# /////////////////////////////////////////////////////////////// 

5 

6""" 

7Uploader factory - Factory for creating uploader instances. 

8 

9This module provides a centralized factory for creating and configuring 

10uploader instances based on type and configuration, with support for 

11validation and discovery of supported types. 

12""" 

13 

14from __future__ import annotations 

15 

16# /////////////////////////////////////////////////////////////// 

17# IMPORTS 

18# /////////////////////////////////////////////////////////////// 

19# Standard library imports 

20from typing import Any 

21 

22# Local imports 

23from ..shared.exceptions import UploadError 

24from ..utils.uploader_utils import UploaderUtils 

25from .base_uploader import BaseUploader 

26from .disk_uploader import DiskUploader 

27from .server_uploader import ServerUploader 

28 

29# /////////////////////////////////////////////////////////////// 

30# CLASSES 

31# /////////////////////////////////////////////////////////////// 

32 

33 

34class UploaderFactory: 

35 """ 

36 Factory class for creating uploader instances. 

37 

38 Provides a centralized way to create and configure uploader instances 

39 based on type specification, with support for validation and type discovery. 

40 

41 Example: 

42 >>> uploader = UploaderFactory.create_uploader("disk", {"overwrite": True}) 

43 >>> types = UploaderFactory.get_supported_types() 

44 >>> print(types) 

45 ['disk', 'server'] 

46 """ 

47 

48 # //////////////////////////////////////////////// 

49 # FACTORY METHODS 

50 # //////////////////////////////////////////////// 

51 

52 @staticmethod 

53 def create_uploader( 

54 upload_type: str, config: dict[str, Any] | None = None 

55 ) -> BaseUploader: 

56 """ 

57 Create an uploader instance based on the specified type. 

58 

59 Args: 

60 upload_type: Type of uploader ("disk" or "server") 

61 config: Configuration dictionary for the uploader (default: None) 

62 

63 Returns: 

64 BaseUploader: Configured uploader instance 

65 

66 Raises: 

67 UploadError: If upload type is not supported 

68 

69 Example: 

70 >>> disk_uploader = UploaderFactory.create_uploader("disk") 

71 >>> server_uploader = UploaderFactory.create_uploader( 

72 ... "server", {"server_url": "https://example.com"} 

73 ... ) 

74 """ 

75 upload_type = upload_type.lower() 

76 

77 # Validate upload type using UploaderUtils 

78 UploaderUtils.validate_upload_type(upload_type) 

79 

80 if upload_type == "disk": 

81 return DiskUploader(config) 

82 elif upload_type == "server": 

83 return ServerUploader(config) 

84 else: 

85 # This should not happen due to validation above, but kept for safety 

86 raise UploadError(f"Unsupported upload type: {upload_type}") 

87 

88 @staticmethod 

89 def create_from_config(config: dict[str, Any]) -> BaseUploader: 

90 """ 

91 Create an uploader instance from a configuration dictionary. 

92 

93 Args: 

94 config: Configuration dictionary containing: 

95 - type: Upload type ("disk" or "server") 

96 - config: Uploader-specific configuration (optional) 

97 

98 Returns: 

99 BaseUploader: Configured uploader instance 

100 

101 Raises: 

102 UploadError: If configuration is invalid or type is missing 

103 

104 Example: 

105 >>> config = { 

106 ... "type": "disk", 

107 ... "config": {"overwrite": True} 

108 ... } 

109 >>> uploader = UploaderFactory.create_from_config(config) 

110 """ 

111 if "type" not in config: 

112 raise UploadError("Upload type not specified in configuration") 

113 

114 upload_type = config["type"] 

115 uploader_config = config.get("config", {}) 

116 

117 return UploaderFactory.create_uploader(upload_type, uploader_config) 

118 

119 # //////////////////////////////////////////////// 

120 # UTILITY METHODS 

121 # //////////////////////////////////////////////// 

122 

123 @staticmethod 

124 def get_supported_types() -> list[str]: 

125 """ 

126 Get list of supported upload types. 

127 

128 Returns: 

129 list[str]: List of supported upload type names 

130 

131 Example: 

132 >>> types = UploaderFactory.get_supported_types() 

133 >>> print(types) 

134 ['disk', 'server'] 

135 """ 

136 return ["disk", "server"] 

137 

138 @staticmethod 

139 def validate_config(upload_type: str, config: dict[str, Any] | None = None) -> bool: 

140 """ 

141 Validate configuration for a specific upload type. 

142 

143 Args: 

144 upload_type: Type of uploader to validate 

145 config: Configuration to validate (default: None) 

146 

147 Returns: 

148 bool: True if configuration is valid, False otherwise 

149 

150 Note: 

151 Creates a temporary uploader instance to test configuration validity. 

152 

153 Example: 

154 >>> is_valid = UploaderFactory.validate_config("disk", {"overwrite": True}) 

155 >>> print(is_valid) 

156 True 

157 """ 

158 try: 

159 UploaderFactory.create_uploader(upload_type, config) 

160 return True 

161 except Exception: 

162 return False