def __establish_profile( self, profile_path: pathlib.Path ) -> typing.Tuple[typing.Optional[Profile.Profile], bool]: assert profile_path.is_absolute( ) # prevents tests from creating temporary files in test directory create_new_profile = not profile_path.exists() if create_new_profile: logging.getLogger("loader").info( f"Creating new profile {profile_path}") profile_json = json.dumps({ "version": FileStorageSystem.PROFILE_VERSION, "uuid": str(uuid.uuid4()) }) profile_path.write_text(profile_json, "utf-8") else: logging.getLogger("loader").info( f"Using existing profile {profile_path}") storage_system = FileStorageSystem.FilePersistentStorageSystem( profile_path) storage_system.load_properties() cache_path = profile_path.parent / pathlib.Path( profile_path.stem + " Cache").with_suffix(".nscache") logging.getLogger("loader").info(f"Using cache {cache_path}") storage_cache = Cache.DbStorageCache(cache_path) profile = Profile.Profile(storage_system=storage_system, storage_cache=storage_cache) profile.read_profile() return profile, create_new_profile
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