|
98 | 98 | raise baton from err |
99 | 99 |
|
100 | 100 |
|
| 101 | +def validate_buffer_size(size: int) -> bool: |
| 102 | + """ |
| 103 | + Validate if the buffer size is a positive integer. |
| 104 | +
|
| 105 | + Arguments: |
| 106 | + size (int) -- The buffer size to validate. |
| 107 | +
|
| 108 | + Returns: |
| 109 | + bool: True if the buffer size is valid (> 0), False otherwise. |
| 110 | +
|
| 111 | + Raises: |
| 112 | + ValueError: If the size cannot be converted to an integer. |
| 113 | +
|
| 114 | + Minimum Acceptance Testing: |
| 115 | + >>> validate_buffer_size(1316) |
| 116 | + True |
| 117 | + >>> validate_buffer_size(1) |
| 118 | + True |
| 119 | + >>> validate_buffer_size(0) |
| 120 | + False |
| 121 | + >>> validate_buffer_size(-1) |
| 122 | + False |
| 123 | + >>> try: |
| 124 | + ... validate_buffer_size('invalid') |
| 125 | + ... except ValueError: |
| 126 | + ... print('ValueError raised') |
| 127 | + ValueError raised |
| 128 | + """ |
| 129 | + try: |
| 130 | + size_num = int(size) |
| 131 | + return size_num > 0 |
| 132 | + except (ValueError, TypeError) as err: |
| 133 | + raise ValueError(f"Invalid buffer size value: {size}. Must be a positive integer.") from err |
| 134 | + |
| 135 | + |
101 | 136 | def validate_port(port: int) -> bool: |
102 | 137 | """ |
103 | 138 | Validate if the port number is within the dynamic/private port range. |
@@ -202,6 +237,49 @@ def validate_ttl(ttl: int) -> bool: |
202 | 237 | ) from err |
203 | 238 |
|
204 | 239 |
|
| 240 | +def load_buffer() -> int: |
| 241 | + """ |
| 242 | + Load and validate the multicast buffer size from environment variable. |
| 243 | +
|
| 244 | + This function attempts to load the buffer size from the MULTICAST_PORT |
| 245 | + environment variable. If the value is valid, it updates the global |
| 246 | + _MCAST_DEFAULT_PORT. Invalid values trigger warnings and fall back to |
| 247 | + the default. |
| 248 | +
|
| 249 | + Returns: |
| 250 | + int: The validated port number to use for multicast operations. |
| 251 | + Returns the default port if the environment value is invalid. |
| 252 | +
|
| 253 | + Raises: |
| 254 | + ImportError: If the multicast module cannot be imported. |
| 255 | +
|
| 256 | + [WIP - DRAFT] |
| 257 | +
|
| 258 | + """ |
| 259 | + # Import globals that we'll potentially update |
| 260 | + from multicast import _MCAST_DEFAULT_BUFFER_SIZE |
| 261 | + try: |
| 262 | + buffer_size = int( |
| 263 | + os.getenv("MULTICAST_BUFFER_SIZE", _MCAST_DEFAULT_BUFFER_SIZE) # skipcq: PYL-W1508 |
| 264 | + ) |
| 265 | + except ValueError: |
| 266 | + warnings.warn( |
| 267 | + "Invalid MULTICAST_BUFFER_SIZE value, using default 1316", |
| 268 | + stacklevel=2 |
| 269 | + ) |
| 270 | + buffer_size = _MCAST_DEFAULT_BUFFER_SIZE |
| 271 | + # Validate and potentially update port |
| 272 | + if validate_buffer_size(buffer_size): |
| 273 | + globals()["_MCAST_DEFAULT_BUFFER_SIZE"] = buffer_size |
| 274 | + else: |
| 275 | + warnings.warn( |
| 276 | + f"Invalid MULTICAST_BUFFER_SIZE {buffer_size}, using default 1316", |
| 277 | + stacklevel=2 |
| 278 | + ) |
| 279 | + buffer_size = _MCAST_DEFAULT_BUFFER_SIZE |
| 280 | + return buffer_size |
| 281 | + |
| 282 | + |
205 | 283 | def load_port() -> int: |
206 | 284 | """ |
207 | 285 | Load and validate the multicast port from environment variable. |
@@ -673,8 +751,8 @@ def load_config() -> dict: |
673 | 751 | ... config = load_config() |
674 | 752 | ... len(w) == 0 # expected failure - Warning was NOT issued |
675 | 753 | True |
676 | | - >>> config['buffer_size'] # expected failure - undefined or Falls back to default |
677 | | - -1024 |
| 754 | + >>> config['buffer_size'] # Undefined or Falls-back to default |
| 755 | + 1316 |
678 | 756 |
|
679 | 757 | # Cleanup |
680 | 758 | >>> os.environ.pop('MULTICAST_BUFFER_SIZE', None) |
@@ -705,7 +783,7 @@ def load_config() -> dict: |
705 | 783 | ttl = load_TTL() |
706 | 784 | groups_str = os.getenv("MULTICAST_GROUPS", "") |
707 | 785 | bind_addr = os.getenv("MULTICAST_BIND_ADDR", group) # skipcq: PYL-W1508 |
708 | | - buffer_size = int(os.getenv("MULTICAST_BUFFER_SIZE", 1316)) # skipcq: PYL-W1508 |
| 786 | + buffer_size = load_buffer() |
709 | 787 | # Process and validate groups |
710 | 788 | groups = set() |
711 | 789 | if groups_str: |
|
0 commit comments