various improvements
This commit is contained in:
		
							
								
								
									
										0
									
								
								compute/utils/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								compute/utils/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										41
									
								
								compute/utils/config_loader.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								compute/utils/config_loader.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
"""Configuration loader."""
 | 
			
		||||
 | 
			
		||||
import tomllib
 | 
			
		||||
from collections import UserDict
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
 | 
			
		||||
from compute.exceptions import ConfigLoaderError
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
DEFAULT_CONFIGURATION = {}
 | 
			
		||||
DEFAULT_CONFIG_FILE = '/etc/computed/computed.toml'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ConfigLoader(UserDict):
 | 
			
		||||
    """UserDict for storing configuration."""
 | 
			
		||||
 | 
			
		||||
    def __init__(self, file: Path | None = None):
 | 
			
		||||
        """
 | 
			
		||||
        Initialise ConfigLoader.
 | 
			
		||||
 | 
			
		||||
        :param file: Path to configuration file. If `file` is None
 | 
			
		||||
            use default path from DEFAULT_CONFIG_FILE constant.
 | 
			
		||||
        """
 | 
			
		||||
        # TODO @ge: load deafult configuration
 | 
			
		||||
        self.file = Path(file) if file else Path(DEFAULT_CONFIG_FILE)
 | 
			
		||||
        super().__init__(self.load())
 | 
			
		||||
 | 
			
		||||
    def load(self) -> dict:
 | 
			
		||||
        """Load confguration object from TOML file."""
 | 
			
		||||
        try:
 | 
			
		||||
            with Path(self.file).open('rb') as configfile:
 | 
			
		||||
                return tomllib.load(configfile)
 | 
			
		||||
                # TODO @ge: add config schema validation
 | 
			
		||||
        except tomllib.TOMLDecodeError as tomlerr:
 | 
			
		||||
            raise ConfigLoaderError(
 | 
			
		||||
                f'Bad TOML syntax in config file: {self.file}: {tomlerr}'
 | 
			
		||||
            ) from tomlerr
 | 
			
		||||
        except (OSError, ValueError) as readerr:
 | 
			
		||||
            raise ConfigLoaderError(
 | 
			
		||||
                f'Cannot read config file: {self.file}: {readerr}'
 | 
			
		||||
            ) from readerr
 | 
			
		||||
							
								
								
									
										18
									
								
								compute/utils/identifiers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								compute/utils/identifiers.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
"""Random identificators."""
 | 
			
		||||
 | 
			
		||||
# ruff: noqa: S311, C417
 | 
			
		||||
 | 
			
		||||
import random
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def random_mac() -> str:
 | 
			
		||||
    """Retrun random MAC address."""
 | 
			
		||||
    mac = [
 | 
			
		||||
        0x00,
 | 
			
		||||
        0x16,
 | 
			
		||||
        0x3E,
 | 
			
		||||
        random.randint(0x00, 0x7F),
 | 
			
		||||
        random.randint(0x00, 0xFF),
 | 
			
		||||
        random.randint(0x00, 0xFF),
 | 
			
		||||
    ]
 | 
			
		||||
    return ':'.join(map(lambda x: '%02x' % x, mac))
 | 
			
		||||
							
								
								
									
										39
									
								
								compute/utils/units.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								compute/utils/units.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
"""Tools for data units convertion."""
 | 
			
		||||
 | 
			
		||||
from enum import StrEnum
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DataUnit(StrEnum):
 | 
			
		||||
    """Data units enumerated."""
 | 
			
		||||
 | 
			
		||||
    BYTES = 'bytes'
 | 
			
		||||
    KIB = 'KiB'
 | 
			
		||||
    MIB = 'MiB'
 | 
			
		||||
    GIB = 'GiB'
 | 
			
		||||
    TIB = 'TiB'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class InvalidDataUnitError(ValueError):
 | 
			
		||||
    """Data unit is not valid."""
 | 
			
		||||
 | 
			
		||||
    def __init__(self, msg: str):
 | 
			
		||||
        """Initialise InvalidDataUnitError."""
 | 
			
		||||
        super().__init__(
 | 
			
		||||
            f'{msg}, valid units are: {", ".join(list(DataUnit))}'
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def to_bytes(value: int, unit: DataUnit = DataUnit.BYTES) -> int:
 | 
			
		||||
    """Convert value to bytes. See `DataUnit`."""
 | 
			
		||||
    try:
 | 
			
		||||
        _ = DataUnit(unit)
 | 
			
		||||
    except ValueError as e:
 | 
			
		||||
        raise InvalidDataUnitError(e) from e
 | 
			
		||||
    powers = {
 | 
			
		||||
        DataUnit.BYTES: 0,
 | 
			
		||||
        DataUnit.KIB: 1,
 | 
			
		||||
        DataUnit.MIB: 2,
 | 
			
		||||
        DataUnit.GIB: 3,
 | 
			
		||||
        DataUnit.TIB: 4,
 | 
			
		||||
    }
 | 
			
		||||
    return value * pow(1024, powers[unit])
 | 
			
		||||
		Reference in New Issue
	
	Block a user