Skip to content

Commit

Permalink
Move filepaths to config
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Nov 22, 2024
1 parent b5cb0d7 commit 6ec11a4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
3 changes: 0 additions & 3 deletions src/aiida/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
)
__paper_short__ = 'S. P. Huber et al., Scientific Data 7, 300 (2020).'

# Initialize the configuration directory settings
AiiDAConfigDir.set_configuration_directory()


def get_strict_version():
"""Return a distutils StrictVersion instance with the current distribution version
Expand Down
18 changes: 9 additions & 9 deletions src/aiida/engine/daemon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ def __init__(self, profile: Profile):
from aiida.common.docs import URL_NO_BROKER

type_check(profile, Profile)
config = get_config()
self._config = get_config()
self._profile = profile
self._socket_directory: str | None = None
self._daemon_timeout: int = config.get_option('daemon.timeout', scope=profile.name)
self._daemon_timeout: int = self._config.get_option('daemon.timeout', scope=profile.name)

if self._profile.process_control_backend is None:
raise ConfigurationError(
Expand Down Expand Up @@ -156,31 +156,31 @@ def virtualenv(self) -> str | None:

@property
def circus_log_file(self) -> str:
return self.profile.filepaths['circus']['log']
return self._config.filepaths(self.profile)['circus']['log']

@property
def circus_pid_file(self) -> str:
return self.profile.filepaths['circus']['pid']
return self._config.filepaths(self.profile)['circus']['pid']

@property
def circus_port_file(self) -> str:
return self.profile.filepaths['circus']['port']
return self._config.filepaths(self.profile)['circus']['port']

@property
def circus_socket_file(self) -> str:
return self.profile.filepaths['circus']['socket']['file']
return self._config.filepaths(self.profile)['circus']['socket']['file']

@property
def circus_socket_endpoints(self) -> dict[str, str]:
return self.profile.filepaths['circus']['socket']
return self._config.filepaths(self.profile)['circus']['socket']

@property
def daemon_log_file(self) -> str:
return self.profile.filepaths['daemon']['log']
return self._config.filepaths(self.profile)['daemon']['log']

@property
def daemon_pid_file(self) -> str:
return self.profile.filepaths['daemon']['pid']
return self._config.filepaths(self.profile)['daemon']['pid']

def get_circus_port(self) -> int:
"""Retrieve the port for the circus controller, which should be written to the circus port file.
Expand Down
31 changes: 31 additions & 0 deletions src/aiida/manage/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io
import json
import os
from pathlib import Path
import uuid
from typing import Any, Dict, List, Optional, Tuple

Expand Down Expand Up @@ -780,3 +781,33 @@ def _atomic_write(self, filepath=None):

handle.flush()
os.rename(handle.name, self.filepath)


def filepaths(self, profile: Profile):
"""Return the filepaths used by this profile.
:return: a dictionary of filepaths
"""
from aiida.manage.configuration.settings import AiiDAConfigPathResolver

_config_path_resolver: AiiDAConfigPathResolver = AiiDAConfigPathResolver(Path(self.dirpath))
daemon_dir = _config_path_resolver.daemon_dir
daemon_log_dir = _config_path_resolver.daemon_log_dir

return {
'circus': {
'log': str(daemon_log_dir / f'circus-{profile.name}.log'),
'pid': str(daemon_dir / f'circus-{profile.name}.pid'),
'port': str(daemon_dir / f'circus-{profile.name}.port'),
'socket': {
'file': str(daemon_dir / f'circus-{profile.name}.sockets'),
'controller': 'circus.c.sock',
'pubsub': 'circus.p.sock',
'stats': 'circus.s.sock',
},
},
'daemon': {
'log': str(daemon_log_dir / f'aiida-{profile.name}.log'),
'pid': str(daemon_dir / f'aiida-{profile.name}.pid'),
},
}
19 changes: 8 additions & 11 deletions src/aiida/manage/configuration/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from aiida.common import exceptions
from aiida.common.lang import type_check
from aiida.manage.configuration.settings import AiiDAConfigPathResolver

from .options import parse_option

Expand Down Expand Up @@ -49,7 +48,7 @@ class Profile:
)

def __init__(
self, name: str, config: abc.Mapping[str, Any], config_folder: pathlib.Path | None = None, validate: bool = True
self, name: str, config: abc.Mapping[str, Any], validate: bool = True
):
"""Load a profile with the profile configuration."""
type_check(config, abc.Mapping)
Expand All @@ -67,8 +66,6 @@ def __init__(

self._attributes[self.KEY_UUID] = uuid4().hex

self._config_path_resolver: AiiDAConfigPathResolver = AiiDAConfigPathResolver(config_folder)

def __repr__(self) -> str:
return f'Profile<uuid={self.uuid!r} name={self.name!r}>'

Expand All @@ -88,11 +85,6 @@ 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

@property
def default_user_email(self) -> str | None:
"""Return the default user email."""
Expand Down Expand Up @@ -245,8 +237,13 @@ def filepaths(self):
:return: a dictionary of filepaths
"""
daemon_dir = self._config_path_resolver.daemon_dir
daemon_log_dir = self._config_path_resolver.daemon_log_dir
from aiida.common.warnings import warn_deprecation
from aiida.manage.configuration.settings import AiiDAConfigPathResolver

warn_deprecation('This method has been deprecated', version=3)

daemon_dir = AiiDAConfigPathResolver().daemon_dir
daemon_log_dir = AiiDAConfigPathResolver().daemon_log_dir

return {
'circus': {
Expand Down
6 changes: 5 additions & 1 deletion src/aiida/manage/configuration/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AiiDAConfigPathResolver:
The locations are all trivially derived from the config location,
"""

def __init__(self, config_folder: pathlib.Path | None) -> None:
def __init__(self, config_folder: pathlib.Path | None = None) -> None:
self._aiida_path = config_folder or AiiDAConfigDir.get_configuration_directory()

@property
Expand Down Expand Up @@ -148,3 +148,7 @@ def _get_configuration_directory_from_envvar() -> pathlib.Path | None:
break

return dirpath_config or default_dirpath_config


# Initialize the configuration directory settings
AiiDAConfigDir.set_configuration_directory()
3 changes: 2 additions & 1 deletion src/aiida/manage/profile_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from aiida.common.exceptions import LockedProfileError, LockingProfileError
from aiida.common.lang import type_check
from aiida.manage.configuration import Profile
from aiida.manage.configuration.settings import AiiDAConfigPathResolver


@typing.final
Expand Down Expand Up @@ -49,7 +50,7 @@ def __init__(self, profile: Profile):
_ = type_check(profile, Profile)
self.profile = profile
self.process = psutil.Process(os.getpid())
self._dirpath_records = profile.config_path_resolver.access_control_dir / profile.name
self._dirpath_records = AiiDAConfigPathResolver().access_control_dir / profile.name
self._dirpath_records.mkdir(exist_ok=True)

def request_access(self) -> None:
Expand Down

0 comments on commit 6ec11a4

Please sign in to comment.