spectrochempy.migrate_legacy_fileο
- migrate_legacy_file(source, destination=None, *, allow_unsafe_legacy=False, overwrite=False, verbose=False)[source]ο
Migrate a legacy SCP/PSCP file to the safe raw-base64 format.
Legacy SCP/PSCP files produced before SpectroChemPy 0.8 may contain pickle-based array payloads. Loading such files with the default
allow_unsafe_legacy=Falseraises an error. This function converts the file to the current safe format (raw-base64).Warning
Migration does execute
pickle.loadson the legacy payload to reconstruct the in-memory object. Only migrate files whose provenance you fully trust. Setallow_unsafe_legacy=Trueto acknowledge this risk explicitly.The migrated file is a proper SCP/ZIP archive and can be loaded with the default
allow_unsafe_legacy=False. The source file is never modified or deleted.Writes are atomic: the destination is only replaced after the migrated file has been written, verified as loadable with safe defaults, and then moved into place via
os.replace().- Parameters:
source (str or pathlib.Path) β Path to the
.scpor.pscpfile to migrate.destination (str or pathlib.Path, optional) β Path for the migrated output file. When None, a
_migratedsuffix is inserted before the extension in the source path.allow_unsafe_legacy (bool, default False) β Required to be ``True``. Acknowledges that the source file will be deserialised via
pickle.loadsduring migration. Set this toTrueonly when the source file comes from a known and trusted origin.overwrite (bool, default False) β Allow overwriting an existing destination file. When False and the destination already exists, a
FileExistsErroris raised.verbose (bool, default False) β Print a summary of the migration to stdout.
- Returns:
pathlib.Path β Path of the migrated file.
- Raises:
FileNotFoundError β If source does not exist.
ValueError β If source does not have a
.scpor.pscpextension, if source and destination resolve to the same path, or if the destination suffix does not match the source suffix.FileExistsError β If the destination already exists and overwrite is False.
spectrochempy.utils.exceptions.SpectroChemPyError β If allow_unsafe_legacy is
False, if the source is already in safe format, or if the migration fails for any other reason.
See also
spectrochempy.loadLoad an SCP/PSCP file.
spectrochempy.NDDataset.saveSave an NDDataset to SCP format.
spectrochempy.Project.saveSave a Project to PSCP format.
Examples
>>> from spectrochempy import migrate_legacy_file >>> # migrate a single file (requires trust acknowledgement) >>> migrated = migrate_legacy_file("old_data.scp", ... allow_unsafe_legacy=True) >>> # migrate to a specific path >>> migrated = migrate_legacy_file("old_data.scp", "new_data.scp", ... allow_unsafe_legacy=True, ... overwrite=True)