From 5304068f957cebd2f7340ed176b6c5547b7ddfcc Mon Sep 17 00:00:00 2001 From: Michael McAuliffe Date: Mon, 13 Feb 2023 22:34:31 -0800 Subject: [PATCH] Fix for some pathlib issues (#564) --- montreal_forced_aligner/abc.py | 12 ++++++------ montreal_forced_aligner/config.py | 22 +++++++++++++--------- montreal_forced_aligner/helper.py | 2 +- tests/conftest.py | 3 ++- tests/test_config.py | 12 ++++++++++++ 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/montreal_forced_aligner/abc.py b/montreal_forced_aligner/abc.py index 925792a9..9e8c9c22 100644 --- a/montreal_forced_aligner/abc.py +++ b/montreal_forced_aligner/abc.py @@ -522,9 +522,9 @@ def setup(self) -> None: self.initialize_database() @property - def working_directory(self) -> str: + def working_directory(self) -> Path: """Alias for a folder that contains worker information, separate from the data directory""" - return os.path.join(self.output_directory, self._current_workflow) + return self.output_directory.joinpath(self._current_workflow) @classmethod def parse_args( @@ -712,14 +712,14 @@ def identifier(self) -> str: return self.data_source_identifier @property - def output_directory(self) -> str: + def output_directory(self) -> Path: """Root temporary directory to store all of this worker's files""" - return os.path.join(GLOBAL_CONFIG.temporary_directory, self.identifier) + return GLOBAL_CONFIG.current_profile.temporary_directory.joinpath(self.identifier) @property - def log_file(self) -> str: + def log_file(self) -> Path: """Path to the worker's log file""" - return os.path.join(self.output_directory, f"{self.data_source_identifier}.log") + return self.output_directory.joinpath(f"{self.data_source_identifier}.log") def setup_logger(self) -> None: """ diff --git a/montreal_forced_aligner/config.py b/montreal_forced_aligner/config.py index f9230727..11449fca 100644 --- a/montreal_forced_aligner/config.py +++ b/montreal_forced_aligner/config.py @@ -6,9 +6,9 @@ from __future__ import annotations import os +import pathlib import re import typing -from pathlib import Path from typing import Any, Dict, List, Union import click @@ -38,7 +38,7 @@ PLDA_DIMENSION = 192 -def get_temporary_directory() -> Path: +def get_temporary_directory() -> pathlib.Path: """ Get the root temporary directory for MFA @@ -51,15 +51,17 @@ def get_temporary_directory() -> Path: ------ :class:`~montreal_forced_aligner.exceptions.RootDirectoryError` """ - TEMP_DIR = os.environ.get(MFA_ROOT_ENVIRONMENT_VARIABLE, os.path.expanduser("~/Documents/MFA")) + TEMP_DIR = pathlib.Path( + os.environ.get(MFA_ROOT_ENVIRONMENT_VARIABLE, os.path.expanduser("~/Documents/MFA")) + ) try: - os.makedirs(TEMP_DIR, exist_ok=True) + TEMP_DIR.mkdir(parents=True, exist_ok=True) except OSError: raise RootDirectoryError(TEMP_DIR, MFA_ROOT_ENVIRONMENT_VARIABLE) - return Path(TEMP_DIR) + return TEMP_DIR -def generate_config_path() -> Path: +def generate_config_path() -> pathlib.Path: """ Generate the global configuration path for MFA @@ -71,7 +73,7 @@ def generate_config_path() -> Path: return get_temporary_directory().joinpath("global_config.yaml") -def generate_command_history_path() -> Path: +def generate_command_history_path() -> pathlib.Path: """ Generate the path to the command history file @@ -147,7 +149,7 @@ class MfaProfile: blas_num_threads: int = 1 use_mp: bool = True single_speaker: bool = False - temporary_directory: str = get_temporary_directory() + temporary_directory: pathlib.Path = get_temporary_directory() github_token: typing.Optional[str] = None def __getitem__(self, item): @@ -166,6 +168,8 @@ def update(self, data: Union[Dict[str, Any], click.Context]) -> None: for k, v in data.items(): if k == "temp_directory": k = "temporary_directory" + if k == "temporary_directory": + v = pathlib.Path(v) if v is None: continue if hasattr(self, k): @@ -220,7 +224,7 @@ def save(self) -> None: def load(self) -> None: """Load MFA configuration""" with mfa_open(self.config_path, "r") as f: - data = yaml.safe_load(f) + data = yaml.load(f, Loader=yaml.Loader) for name, p in data.pop("profiles", {}).items(): self.profiles[name] = MfaProfile() self.profiles[name].update(p) diff --git a/montreal_forced_aligner/helper.py b/montreal_forced_aligner/helper.py index 7c7f9b9b..b645e570 100644 --- a/montreal_forced_aligner/helper.py +++ b/montreal_forced_aligner/helper.py @@ -158,7 +158,7 @@ def parse_old_features(config: MetaDict) -> MetaDict: return config -def configure_logger(identifier: str, log_file: Optional[str] = None) -> None: +def configure_logger(identifier: str, log_file: Optional[Path] = None) -> None: """ Configure logging for the given identifier diff --git a/tests/conftest.py b/tests/conftest.py index da4c5980..72b4c710 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import pathlib import shutil import mock @@ -85,7 +86,7 @@ def global_config(): @pytest.fixture(scope="session") def temp_dir(generated_dir, global_config): temp_dir = os.path.join(generated_dir, "temp") - global_config.current_profile.temporary_directory = temp_dir + global_config.current_profile.temporary_directory = pathlib.Path(temp_dir) global_config.save() yield temp_dir diff --git a/tests/test_config.py b/tests/test_config.py index 6ccfef04..453f3bcf 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,5 @@ import os +import pathlib import pytest @@ -168,3 +169,14 @@ def test_load(basic_corpus_dir, basic_dict_path, temp_dir, config_directory): with pytest.raises(ConfigError): params = TrainableAligner.parse_parameters(path) am_trainer.cleanup() + + +def test_config(global_config): + new_temp_path = global_config.current_profile.temporary_directory.joinpath("test") + global_config.current_profile.temporary_directory = new_temp_path + global_config.save() + global_config.load() + assert isinstance(global_config.current_profile.temporary_directory, pathlib.Path) + assert global_config.current_profile.temporary_directory == new_temp_path + global_config.current_profile.temporary_directory = new_temp_path.parent + global_config.save()