|
| 1 | +"""Environment variable management for MDIO operations.""" |
| 2 | + |
| 3 | +from os import getenv |
| 4 | +from typing import Final |
| 5 | + |
| 6 | +from psutil import cpu_count |
| 7 | + |
| 8 | +from mdio.converters.exceptions import EnvironmentFormatError |
| 9 | + |
| 10 | + |
| 11 | +class Environment: |
| 12 | + """Unified API for accessing and validating MDIO environment variables.""" |
| 13 | + |
| 14 | + # Environment variable keys and defaults |
| 15 | + _EXPORT_CPUS_KEY: Final[str] = "MDIO__EXPORT__CPU_COUNT" |
| 16 | + _IMPORT_CPUS_KEY: Final[str] = "MDIO__IMPORT__CPU_COUNT" |
| 17 | + _GRID_SPARSITY_RATIO_WARN_KEY: Final[str] = "MDIO__GRID__SPARSITY_RATIO_WARN" |
| 18 | + _GRID_SPARSITY_RATIO_LIMIT_KEY: Final[str] = "MDIO__GRID__SPARSITY_RATIO_LIMIT" |
| 19 | + _SAVE_SEGY_FILE_HEADER_KEY: Final[str] = "MDIO__IMPORT__SAVE_SEGY_FILE_HEADER" |
| 20 | + _MDIO_SEGY_SPEC_KEY: Final[str] = "MDIO__SEGY__SPEC" |
| 21 | + _RAW_HEADERS_KEY: Final[str] = "MDIO__IMPORT__RAW_HEADERS" |
| 22 | + _IGNORE_CHECKS_KEY: Final[str] = "MDIO_IGNORE_CHECKS" |
| 23 | + _CLOUD_NATIVE_KEY: Final[str] = "MDIO__IMPORT__CLOUD_NATIVE" |
| 24 | + |
| 25 | + # Default values |
| 26 | + _EXPORT_CPUS_DEFAULT: Final[int] = cpu_count(logical=True) |
| 27 | + _IMPORT_CPUS_DEFAULT: Final[int] = cpu_count(logical=True) |
| 28 | + _GRID_SPARSITY_RATIO_WARN_DEFAULT: Final[str] = "2" |
| 29 | + _GRID_SPARSITY_RATIO_LIMIT_DEFAULT: Final[str] = "10" |
| 30 | + _SAVE_SEGY_FILE_HEADER_DEFAULT: Final[str] = "false" |
| 31 | + _MDIO_SEGY_SPEC_DEFAULT: Final[None] = None |
| 32 | + _RAW_HEADERS_DEFAULT: Final[str] = "false" |
| 33 | + _IGNORE_CHECKS_DEFAULT: Final[str] = "false" |
| 34 | + _CLOUD_NATIVE_DEFAULT: Final[str] = "false" |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def _get_env_value(cls, key: str, default: str | int | None) -> str | None: |
| 38 | + """Get environment variable value with fallback to default.""" |
| 39 | + if isinstance(default, int): |
| 40 | + default = str(default) |
| 41 | + return getenv(key, default) |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def _parse_bool(value: str | None) -> bool: |
| 45 | + """Parse string value to boolean.""" |
| 46 | + if value is None: |
| 47 | + return False |
| 48 | + return value.lower() in ("1", "true", "yes", "on") |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def _parse_int(value: str | None, key: str) -> int: |
| 52 | + """Parse string value to integer with validation.""" |
| 53 | + if value is None: |
| 54 | + raise EnvironmentFormatError(key, "int") |
| 55 | + try: |
| 56 | + return int(value) |
| 57 | + except ValueError as e: |
| 58 | + raise EnvironmentFormatError(key, "int") from e |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def _parse_float(value: str | None, key: str) -> float: |
| 62 | + """Parse string value to float with validation.""" |
| 63 | + if value is None: |
| 64 | + raise EnvironmentFormatError(key, "float") |
| 65 | + try: |
| 66 | + return float(value) |
| 67 | + except ValueError as e: |
| 68 | + raise EnvironmentFormatError(key, "float") from e |
| 69 | + |
| 70 | + @classmethod |
| 71 | + def export_cpus(cls) -> int: |
| 72 | + """Number of CPUs to use for export operations.""" |
| 73 | + value = cls._get_env_value(cls._EXPORT_CPUS_KEY, cls._EXPORT_CPUS_DEFAULT) |
| 74 | + return cls._parse_int(value, cls._EXPORT_CPUS_KEY) |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def import_cpus(cls) -> int: |
| 78 | + """Number of CPUs to use for import operations.""" |
| 79 | + value = cls._get_env_value(cls._IMPORT_CPUS_KEY, cls._IMPORT_CPUS_DEFAULT) |
| 80 | + return cls._parse_int(value, cls._IMPORT_CPUS_KEY) |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def grid_sparsity_ratio_warn(cls) -> float: |
| 84 | + """Sparsity ratio threshold for warnings.""" |
| 85 | + value = cls._get_env_value(cls._GRID_SPARSITY_RATIO_WARN_KEY, cls._GRID_SPARSITY_RATIO_WARN_DEFAULT) |
| 86 | + return cls._parse_float(value, cls._GRID_SPARSITY_RATIO_WARN_KEY) |
| 87 | + |
| 88 | + @classmethod |
| 89 | + def grid_sparsity_ratio_limit(cls) -> float: |
| 90 | + """Sparsity ratio threshold for errors.""" |
| 91 | + value = cls._get_env_value(cls._GRID_SPARSITY_RATIO_LIMIT_KEY, cls._GRID_SPARSITY_RATIO_LIMIT_DEFAULT) |
| 92 | + return cls._parse_float(value, cls._GRID_SPARSITY_RATIO_LIMIT_KEY) |
| 93 | + |
| 94 | + @classmethod |
| 95 | + def save_segy_file_header(cls) -> bool: |
| 96 | + """Whether to save SEG-Y file headers.""" |
| 97 | + value = cls._get_env_value(cls._SAVE_SEGY_FILE_HEADER_KEY, cls._SAVE_SEGY_FILE_HEADER_DEFAULT) |
| 98 | + return cls._parse_bool(value) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def mdio_segy_spec(cls) -> str | None: |
| 102 | + """Path to MDIO SEG-Y specification file.""" |
| 103 | + return cls._get_env_value(cls._MDIO_SEGY_SPEC_KEY, cls._MDIO_SEGY_SPEC_DEFAULT) |
| 104 | + |
| 105 | + @classmethod |
| 106 | + def raw_headers(cls) -> bool: |
| 107 | + """Whether to preserve raw headers.""" |
| 108 | + value = cls._get_env_value(cls._RAW_HEADERS_KEY, cls._RAW_HEADERS_DEFAULT) |
| 109 | + return cls._parse_bool(value) |
| 110 | + |
| 111 | + @classmethod |
| 112 | + def ignore_checks(cls) -> bool: |
| 113 | + """Whether to ignore validation checks.""" |
| 114 | + value = cls._get_env_value(cls._IGNORE_CHECKS_KEY, cls._IGNORE_CHECKS_DEFAULT) |
| 115 | + return cls._parse_bool(value) |
| 116 | + |
| 117 | + @classmethod |
| 118 | + def cloud_native(cls) -> bool: |
| 119 | + """Whether to use cloud-native mode for SEG-Y processing.""" |
| 120 | + value = cls._get_env_value(cls._CLOUD_NATIVE_KEY, cls._CLOUD_NATIVE_DEFAULT) |
| 121 | + return cls._parse_bool(value) |
0 commit comments