Esempio n. 1
0
def create_profile(workspace_dir: pathlib.Path, do_logging: bool, force_create: bool) -> typing.Tuple[typing.Optional[Profile], bool]:
    library_path = _migrate_library(workspace_dir, do_logging)
    if not force_create and not os.path.exists(library_path):
        return None, False
    create_new_document = not os.path.exists(library_path)
    if do_logging:
        if create_new_document:
            logging.info(f"Creating new document: {library_path}")
        else:
            logging.info(f"Using existing document {library_path}")
    auto_migrations = list()
    auto_migrations.append(AutoMigration(pathlib.Path(workspace_dir) / "Nion Swift Workspace.nslib", [pathlib.Path(workspace_dir) / "Nion Swift Data"]))
    auto_migrations.append(AutoMigration(pathlib.Path(workspace_dir) / "Nion Swift Workspace.nslib", [pathlib.Path(workspace_dir) / "Nion Swift Data 10"]))
    auto_migrations.append(AutoMigration(pathlib.Path(workspace_dir) / "Nion Swift Workspace.nslib", [pathlib.Path(workspace_dir) / "Nion Swift Data 11"]))
    auto_migrations.append(AutoMigration(pathlib.Path(workspace_dir) / "Nion Swift Library 12.nslib", [pathlib.Path(workspace_dir) / "Nion Swift Data 12"]))
    # NOTE: when adding an AutoMigration here, also add the corresponding file copy in _migrate_library
    storage_system = FileStorageSystem.FileStorageSystem(library_path, [pathlib.Path(workspace_dir) / f"Nion Swift Data {DataItem.DataItem.storage_version}"], auto_migrations=auto_migrations)
    cache_filename = f"Nion Swift Cache {DataItem.DataItem.storage_version}.nscache"
    cache_path = workspace_dir / cache_filename
    storage_cache = Cache.DbStorageCache(cache_path)
    return Profile(storage_system=storage_system, storage_cache=storage_cache, ignore_older_files=False), create_new_document
def get_storage_system(workspace_dir):
    # This function is adapted from Swift's profile
    workspace_dir = pathlib.Path(workspace_dir)
    library_path = Profile._migrate_library(workspace_dir, do_logging=True)
    this_storage_version = DataItem.DataItem.storage_version
    auto_migrations = list()
    auto_migrations.append(
        Profile.AutoMigration(
            pathlib.Path(workspace_dir) / "Nion Swift Workspace.nslib",
            [pathlib.Path(workspace_dir) / "Nion Swift Data"]))
    auto_migrations.append(
        Profile.AutoMigration(
            pathlib.Path(workspace_dir) / "Nion Swift Workspace.nslib",
            [pathlib.Path(workspace_dir) / "Nion Swift Data 10"]))
    auto_migrations.append(
        Profile.AutoMigration(
            pathlib.Path(workspace_dir) / "Nion Swift Workspace.nslib",
            [pathlib.Path(workspace_dir) / "Nion Swift Data 11"]))
    # Attemp at being future proof
    if this_storage_version > 12:
        for storage_version in range(12, this_storage_version):
            auto_migrations.append(
                Profile.AutoMigration(
                    pathlib.Path(workspace_dir) /
                    f"Nion Swift Library {storage_version}.nslib", [
                        pathlib.Path(workspace_dir) /
                        f"Nion Swift Data {storage_version}"
                    ]))

    # NOTE: when adding an AutoMigration here, also add the corresponding file
    # copy in _migrate_library
    storage_system = FileStorageSystem.FileStorageSystem(
        library_path, [
            pathlib.Path(workspace_dir) /
            f"Nion Swift Data {this_storage_version}"
        ],
        auto_migrations=auto_migrations)
    return storage_system