From d7f36848412adda3c8e7874e0f87cb327b738041 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Fri, 15 Nov 2024 01:37:24 +0100 Subject: [PATCH] Remove use of ACCESS_CONTROL_DIR --- src/aiida/manage/configuration/profile.py | 15 ++++++++++++--- src/aiida/manage/configuration/settings.py | 12 ++++++------ src/aiida/manage/profile_access.py | 8 +++----- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/aiida/manage/configuration/profile.py b/src/aiida/manage/configuration/profile.py index f11f353204..48b6aef6fd 100644 --- a/src/aiida/manage/configuration/profile.py +++ b/src/aiida/manage/configuration/profile.py @@ -17,6 +17,7 @@ from typing import TYPE_CHECKING, Any, Dict, Literal, Mapping, Optional, Type from aiida.common import exceptions +from aiida.common.lang import type_check from aiida.manage.configuration.settings import AiiDAConfigPathResolver from .options import parse_option @@ -51,14 +52,13 @@ def __init__( self, name: str, config: Mapping[str, Any], config_folder: pathlib.Path | None = None, validate: bool = True ): """Load a profile with the profile configuration.""" - if not isinstance(config, abc.Mapping): - raise TypeError(f'config should be a mapping but is {type(config)}') + _ = type_check(config, abc.Mapping) if validate and not set(config.keys()).issuperset(self.REQUIRED_KEYS): raise exceptions.ConfigurationError( f'profile {name!r} configuration does not contain all required keys: {self.REQUIRED_KEYS}' ) - self._name = name + self._name: str = name self._attributes: Dict[str, Any] = deepcopy(config) # Create a default UUID if not specified @@ -88,6 +88,15 @@ def uuid(self) -> str: def uuid(self, value: str) -> None: self._attributes[self.KEY_UUID] = value + @property + def config_path_resolver(self) -> AiiDAConfigPathResolver: + """The config_path_resolver property.""" + return self._config_path_resolver + + @config_path_resolver.setter + def config_path_resolver(self, value): + self._config_path_resolver = value + @property def default_user_email(self) -> Optional[str]: """Return the default user email.""" diff --git a/src/aiida/manage/configuration/settings.py b/src/aiida/manage/configuration/settings.py index 54411ce3ff..e0e0277733 100644 --- a/src/aiida/manage/configuration/settings.py +++ b/src/aiida/manage/configuration/settings.py @@ -29,9 +29,9 @@ # Assign defaults which may be overriden in set_configuration_directory() below glb_aiida_config_folder: pathlib.Path = pathlib.Path(DEFAULT_AIIDA_PATH).expanduser() / DEFAULT_CONFIG_DIR_NAME -# DAEMON_DIR: pathlib.Path = glb_aiida_config_folder / DEFAULT_DAEMON_DIR_NAME -# DAEMON_LOG_DIR: pathlib.Path = DAEMON_DIR / DEFAULT_DAEMON_LOG_DIR_NAME -# ACCESS_CONTROL_DIR: pathlib.Path = glb_aiida_config_folder / DEFAULT_ACCESS_CONTROL_DIR_NAME +DAEMON_DIR: pathlib.Path = glb_aiida_config_folder / DEFAULT_DAEMON_DIR_NAME +DAEMON_LOG_DIR: pathlib.Path = DAEMON_DIR / DEFAULT_DAEMON_LOG_DIR_NAME + @final class AiiDAConfigPathResolver: @@ -101,8 +101,8 @@ def get_configuration_directory(): The location of the configuration directory is defined following these heuristics in order: * If the ``AIIDA_PATH`` variable is set, all the paths will be checked to see if they contain a - configuration folder. The first one to be encountered will be set as ``glb_aiida_config_folder``. If none of them - contain one, the last path defined in the environment variable considered is used. + configuration folder. The first one to be encountered will be set as ``glb_aiida_config_folder``. + If none of them contain one, the last path defined in the environment variable considered is used. * If an existing directory is still not found, the ``DEFAULT_AIIDA_PATH`` is used. :returns: The path of the configuration directory. @@ -153,7 +153,7 @@ def set_configuration_directory(aiida_config_folder: pathlib.Path | None = None) is returned by ``get_configuration_directory``. If the directory does not exist yet, it is created, together with all its subdirectories. """ - global glb_aiida_config_folder + global glb_aiida_config_folder # noqa: PLW0603 glb_aiida_config_folder = aiida_config_folder or get_configuration_directory() create_instance_directories(glb_aiida_config_folder) diff --git a/src/aiida/manage/profile_access.py b/src/aiida/manage/profile_access.py index 5b04481e66..01aa556cf8 100644 --- a/src/aiida/manage/profile_access.py +++ b/src/aiida/manage/profile_access.py @@ -19,7 +19,7 @@ from aiida.common.lang import type_check from aiida.manage.configuration import Profile - +@typing.final class ProfileAccessManager: """Class to manage access to a profile. @@ -45,12 +45,10 @@ def __init__(self, profile: Profile): :param profile: the profile whose access to manage. """ - from aiida.manage.configuration.settings import ACCESS_CONTROL_DIR - - type_check(profile, Profile) + _ = type_check(profile, Profile) self.profile = profile self.process = psutil.Process(os.getpid()) - self._dirpath_records = ACCESS_CONTROL_DIR / profile.name + self._dirpath_records = profile.config_path_resolver.access_control_dir / profile.name self._dirpath_records.mkdir(exist_ok=True) def request_access(self) -> None: