Skip to content

Commit 9baf3ca

Browse files
authored
Use only one global var for marking config folder tree (#6610)
The changes reduce the use of global variables by replacing `DAEMON_DIR`, `DAEMON_LOG_DIR` and `ACCESS_CONTROL_DIR` as derived from helper class `AiiDAConfigPathResolver`. The `AIIDA_CONFIG_FOLDER` is moved as the class attribute `_glb_aiida_config_folder` of `AiiDAConfigDir` which provide `setter/getter` method for access and set the config_folder globally. Meanwhile, the `filepaths` method is depracted from profile and moved to config module. It now called with passing the profile. Since the derived files makes more sense as attaching to the config folder location for every profile.
1 parent f74adb9 commit 9baf3ca

File tree

17 files changed

+175
-123
lines changed

17 files changed

+175
-123
lines changed

.pre-commit-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ repos:
125125
src/aiida/engine/processes/ports.py|
126126
src/aiida/manage/configuration/__init__.py|
127127
src/aiida/manage/configuration/config.py|
128-
src/aiida/manage/configuration/profile.py|
129128
src/aiida/manage/external/rmq/launcher.py|
130129
src/aiida/manage/tests/main.py|
131130
src/aiida/manage/tests/pytest_fixtures.py|

src/aiida/cmdline/commands/cmd_presto.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def detect_postgres_config(
6767
"""
6868
import secrets
6969

70-
from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER
70+
from aiida.manage.configuration.settings import AiiDAConfigDir
7171
from aiida.manage.external.postgres import Postgres
7272

7373
dbinfo = {
@@ -92,13 +92,15 @@ def detect_postgres_config(
9292
except Exception as exception:
9393
raise ConnectionError(f'Unable to automatically create the PostgreSQL user and database: {exception}')
9494

95+
aiida_config_folder = AiiDAConfigDir.get()
96+
9597
return {
9698
'database_hostname': postgres_hostname,
9799
'database_port': postgres_port,
98100
'database_name': database_name,
99101
'database_username': database_username,
100102
'database_password': database_password,
101-
'repository_uri': f'file://{AIIDA_CONFIG_FOLDER / "repository" / profile_name}',
103+
'repository_uri': f'file://{aiida_config_folder / "repository" / profile_name}',
102104
}
103105

104106

src/aiida/cmdline/commands/cmd_profile.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ def profile_list():
169169
# This can happen for a fresh install and the `verdi setup` has not yet been run. In this case it is still nice
170170
# to be able to see the configuration directory, for instance for those who have set `AIIDA_PATH`. This way
171171
# they can at least verify that it is correctly set.
172-
from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER
172+
from aiida.manage.configuration.settings import AiiDAConfigDir
173173

174-
echo.echo_report(f'configuration folder: {AIIDA_CONFIG_FOLDER}')
174+
echo.echo_report(f'configuration folder: {AiiDAConfigDir.get()}')
175175
echo.echo_critical(str(exception))
176176
else:
177177
echo.echo_report(f'configuration folder: {config.dirpath}')

src/aiida/cmdline/commands/cmd_status.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ def verdi_status(print_traceback, no_rmq):
6161
from aiida.common.docs import URL_NO_BROKER
6262
from aiida.common.exceptions import ConfigurationError
6363
from aiida.engine.daemon.client import DaemonException, DaemonNotRunningException
64-
from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER
64+
from aiida.manage.configuration.settings import AiiDAConfigDir
6565
from aiida.manage.manager import get_manager
6666

6767
exit_code = ExitCode.SUCCESS
68+
configure_directory = AiiDAConfigDir.get()
6869

6970
print_status(ServiceStatus.UP, 'version', f'AiiDA v{__version__}')
70-
print_status(ServiceStatus.UP, 'config', AIIDA_CONFIG_FOLDER)
71+
print_status(ServiceStatus.UP, 'config', configure_directory)
7172

7273
manager = get_manager()
7374

src/aiida/cmdline/params/options/commands/setup.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ def get_repository_uri_default(ctx):
6666
"""
6767
import os
6868

69-
from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER
69+
from aiida.manage.configuration.settings import AiiDAConfigDir
7070

7171
validate_profile_parameter(ctx)
72+
configure_directory = AiiDAConfigDir.get()
7273

73-
return os.path.join(AIIDA_CONFIG_FOLDER, 'repository', ctx.params['profile'].name)
74+
return os.path.join(configure_directory, 'repository', ctx.params['profile'].name)
7475

7576

7677
def get_quicksetup_repository_uri(ctx, param, value):

src/aiida/engine/daemon/client.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ def __init__(self, profile: Profile):
9494
from aiida.common.docs import URL_NO_BROKER
9595

9696
type_check(profile, Profile)
97-
config = get_config()
97+
self._config = get_config()
9898
self._profile = profile
9999
self._socket_directory: str | None = None
100-
self._daemon_timeout: int = config.get_option('daemon.timeout', scope=profile.name)
100+
self._daemon_timeout: int = self._config.get_option('daemon.timeout', scope=profile.name)
101101

102102
if self._profile.process_control_backend is None:
103103
raise ConfigurationError(
@@ -156,31 +156,31 @@ def virtualenv(self) -> str | None:
156156

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

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

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

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

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

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

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

185185
def get_circus_port(self) -> int:
186186
"""Retrieve the port for the circus controller, which should be written to the circus port file.

src/aiida/manage/configuration/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@
6565

6666

6767
def get_config_path():
68-
"""Returns path to .aiida configuration directory."""
69-
from .settings import AIIDA_CONFIG_FOLDER, DEFAULT_CONFIG_FILE_NAME
68+
"""Returns path to aiida configuration file."""
69+
from .settings import DEFAULT_CONFIG_FILE_NAME, AiiDAConfigDir
7070

71-
return os.path.join(AIIDA_CONFIG_FOLDER, DEFAULT_CONFIG_FILE_NAME)
71+
return os.path.join(AiiDAConfigDir.get(), DEFAULT_CONFIG_FILE_NAME)
7272

7373

7474
def load_config(create=False) -> 'Config':

src/aiida/manage/configuration/config.py

+30
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import json
2222
import os
2323
import uuid
24+
from pathlib import Path
2425
from typing import Any, Dict, List, Optional, Tuple
2526

2627
from pydantic import (
@@ -780,3 +781,32 @@ def _atomic_write(self, filepath=None):
780781

781782
handle.flush()
782783
os.rename(handle.name, self.filepath)
784+
785+
def filepaths(self, profile: Profile):
786+
"""Return the filepaths used by a profile.
787+
788+
:return: a dictionary of filepaths
789+
"""
790+
from aiida.manage.configuration.settings import AiiDAConfigPathResolver
791+
792+
_config_path_resolver: AiiDAConfigPathResolver = AiiDAConfigPathResolver(Path(self.dirpath))
793+
daemon_dir = _config_path_resolver.daemon_dir
794+
daemon_log_dir = _config_path_resolver.daemon_log_dir
795+
796+
return {
797+
'circus': {
798+
'log': str(daemon_log_dir / f'circus-{profile.name}.log'),
799+
'pid': str(daemon_dir / f'circus-{profile.name}.pid'),
800+
'port': str(daemon_dir / f'circus-{profile.name}.port'),
801+
'socket': {
802+
'file': str(daemon_dir / f'circus-{profile.name}.sockets'),
803+
'controller': 'circus.c.sock',
804+
'pubsub': 'circus.p.sock',
805+
'stats': 'circus.s.sock',
806+
},
807+
},
808+
'daemon': {
809+
'log': str(daemon_log_dir / f'aiida-{profile.name}.log'),
810+
'pid': str(daemon_dir / f'aiida-{profile.name}.pid'),
811+
},
812+
}

src/aiida/manage/configuration/profile.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(self, name: str, config: Mapping[str, Any], validate=True):
5656
)
5757

5858
self._name = name
59-
self._attributes: Dict[str, Any] = deepcopy(config)
59+
self._attributes: Dict[str, Any] = deepcopy(config) # type: ignore[arg-type]
6060

6161
# Create a default UUID if not specified
6262
if self._attributes.get(self.KEY_UUID, None) is None:
@@ -235,22 +235,28 @@ def filepaths(self):
235235
236236
:return: a dictionary of filepaths
237237
"""
238-
from .settings import DAEMON_DIR, DAEMON_LOG_DIR
238+
from aiida.common.warnings import warn_deprecation
239+
from aiida.manage.configuration.settings import AiiDAConfigPathResolver
240+
241+
warn_deprecation('This method has been deprecated, use `filepaths` method from `Config` obj instead', version=3)
242+
243+
daemon_dir = AiiDAConfigPathResolver().daemon_dir
244+
daemon_log_dir = AiiDAConfigPathResolver().daemon_log_dir
239245

240246
return {
241247
'circus': {
242-
'log': str(DAEMON_LOG_DIR / f'circus-{self.name}.log'),
243-
'pid': str(DAEMON_DIR / f'circus-{self.name}.pid'),
244-
'port': str(DAEMON_DIR / f'circus-{self.name}.port'),
248+
'log': str(daemon_log_dir / f'circus-{self.name}.log'),
249+
'pid': str(daemon_dir / f'circus-{self.name}.pid'),
250+
'port': str(daemon_dir / f'circus-{self.name}.port'),
245251
'socket': {
246-
'file': str(DAEMON_DIR / f'circus-{self.name}.sockets'),
252+
'file': str(daemon_dir / f'circus-{self.name}.sockets'),
247253
'controller': 'circus.c.sock',
248254
'pubsub': 'circus.p.sock',
249255
'stats': 'circus.s.sock',
250256
},
251257
},
252258
'daemon': {
253-
'log': str(DAEMON_LOG_DIR / f'aiida-{self.name}.log'),
254-
'pid': str(DAEMON_DIR / f'aiida-{self.name}.pid'),
259+
'log': str(daemon_log_dir / f'aiida-{self.name}.log'),
260+
'pid': str(daemon_dir / f'aiida-{self.name}.pid'),
255261
},
256262
}

0 commit comments

Comments
 (0)