AutoArchive._application.archiving._utils — AutoArchive Developer Manual (ver. 2.1.0.dev20260309)

# Source code for AutoArchive._application.archiving._utils

    # _utils.py
    #
    # Project: AutoArchive
    # License: GNU GPLv3
    #
    # Copyright (C) 2003 - 2025 Róbert Čerňanský

    import itertools
    from typing import Iterable

    from AutoArchive._infrastructure.configuration import Options, ConfigurationBase, ArchiverTypes
    from AutoArchive._infrastructure.service._service_accessor import ServiceAccessor
    from AutoArchive._infrastructure.utils import Utils
    from AutoArchive._services.archiver import ArchiverServiceIdentification, ArchiverServiceProviderIDs, BackupTypes
    from AutoArchive._services.archiver.tar_archiver_provider_base import TarArchiverProviderBase

    class _Utils:

        # SMELL: This is duplication with service provider ID and TarArchiverProviderBase.supportedBackupTypes;
        # map should be made between ArchiverTypes and BackupTypes then get query services for supportedBackupTypes
        __ARCHIVER_TYPE_TO_PROVIDER_ID_MAP = {ArchiverTypes.Tar: ArchiverServiceProviderIDs.TarExternal,
                                              ArchiverTypes.TarGz: ArchiverServiceProviderIDs.TarExternal,
                                              ArchiverTypes.TarBz2: ArchiverServiceProviderIDs.TarExternal,
                                              ArchiverTypes.TarXz: ArchiverServiceProviderIDs.TarExternal,
                                              ArchiverTypes.TarZst: ArchiverServiceProviderIDs.TarExternal,
                                              ArchiverTypes.TarInternal: ArchiverServiceProviderIDs.TarInternal,
                                              ArchiverTypes.TarGzInternal: ArchiverServiceProviderIDs.TarInternal,
                                              ArchiverTypes.TarBz2Internal: ArchiverServiceProviderIDs.TarInternal}

        __ARCHIVER_TYPE_TO_BACKUP_TYPE_MAP = {ArchiverTypes.Tar: BackupTypes.Tar,
                                              ArchiverTypes.TarGz: BackupTypes.TarGz,
                                              ArchiverTypes.TarBz2: BackupTypes.TarBz2,
                                              ArchiverTypes.TarXz: BackupTypes.TarXz,
                                              ArchiverTypes.TarZst: BackupTypes.TarZst,
                                              ArchiverTypes.TarInternal: BackupTypes.Tar,
                                              ArchiverTypes.TarGzInternal: BackupTypes.TarGz,
                                              ArchiverTypes.TarBz2Internal: BackupTypes.TarBz2}

        @classmethod
        def getOrCreateArchiverService(cls,
                serviceAccessor: ServiceAccessor, archiveSpec: ConfigurationBase) -> TarArchiverProviderBase:
            providerIdentification = cls.__ARCHIVER_TYPE_TO_PROVIDER_ID_MAP[archiveSpec[Options.ARCHIVER]]
            return serviceAccessor.getOrCreateService(ArchiverServiceIdentification, providerIdentification)

        @classmethod
        def archiverTypeToBackupType(cls, archiverType: ArchiverTypes) -> BackupTypes:
            return cls.__ARCHIVER_TYPE_TO_BACKUP_TYPE_MAP[archiverType]

        @classmethod
        def providerIdToArchiverTypes(cls, providerId: ArchiverServiceProviderIDs) -> Iterable[ArchiverTypes]:
            return Utils.getKeysForValue(providerId, cls.__ARCHIVER_TYPE_TO_PROVIDER_ID_MAP)

        @classmethod
        @Utils.uniq
        def backupTypesToArchiverTypes(cls, backupTypes: Iterable[BackupTypes]) -> Iterable[ArchiverTypes]:
            return itertools.chain.from_iterable(
                Utils.getKeysForValue(bt, cls.__ARCHIVER_TYPE_TO_BACKUP_TYPE_MAP) for bt in backupTypes)