from warnings import warn from dynamix_sdk import config from dynamix_sdk.api.system.info.version import SystemInfoVersionResultModel from dynamix_sdk.exceptions import IncompatibleAPIError from dynamix_sdk.utils import F_DECORATOR from .api import API class Dynamix: _config: config.ConfigWithAuth _COMPATIBLE_DX_MINOR_VERSION: str = '4.7' _COMPATIBLE_DX_BUILD: int = 1799 _COMPATIBILITY_WITH_NEWER_DX_BUILDS: bool = True _sys_info_version_result: SystemInfoVersionResultModel api: API def __init__( self, *, url: str, auth: str | config.AuthenticatorProtocol, verify_ssl: bool = True, http503_attempts: int = 10, http503_attempts_interval: int = 5, ignore_api_compatibility: bool = False, wrap_request_exceptions: bool = False, f_decorators: list[F_DECORATOR] | None = None, ): self._config = config.ConfigWithAuth( base_api_path='/restmachine', url=url, auth=auth, verify_ssl=verify_ssl, http503_attempts=http503_attempts, http503_attempts_interval=http503_attempts_interval, wrap_request_exceptions=wrap_request_exceptions, f_decorators=f_decorators, ) self.api = API(config=self._config) self._sys_info_version_result = self.api.system.info.version() self._check_dx_compatibility(ignore_api_compatibility) @property def compatible_dx_minor_version(self) -> str: return self._COMPATIBLE_DX_MINOR_VERSION @property def compatible_dx_build(self) -> int: return self._COMPATIBLE_DX_BUILD @property def compatibility_with_newer_dx_builds(self) -> bool: return self._COMPATIBILITY_WITH_NEWER_DX_BUILDS @property def dx_version(self) -> str: return self._sys_info_version_result.version @property def dx_build(self) -> int: return self._sys_info_version_result.build_num def _check_dx_compatibility(self, ignore_api_compatibility: bool): if ( self.dx_version.rsplit('.', 1)[0] != self.compatible_dx_minor_version ): message = ( f'Incompatible minor Dynamix version. ' f'Dynamix version: {self.dx_version}. ' f'Compatible minor version: ' f'{self.compatible_dx_minor_version}.' ) if ignore_api_compatibility: warn(message=message) return else: raise IncompatibleAPIError(message=message) build_is_compatible = ( ( self.compatibility_with_newer_dx_builds and self.dx_build >= self.compatible_dx_build ) or ( not self.compatibility_with_newer_dx_builds and self.dx_build == self.compatible_dx_build ) ) if not build_is_compatible: compatible_build_str = f'{self.compatible_dx_build}' if self.compatibility_with_newer_dx_builds: compatible_build_str = f'>={compatible_build_str}' message = ( f'Incompatible Dynamix build. ' f'Dynamix build: {self.dx_build}. ' f'Compatible build: {compatible_build_str}.' ) if ignore_api_compatibility: warn(message=message) else: raise IncompatibleAPIError(message=message)