Skip to content

Commit b7445ac

Browse files
committed
Define local and home config file paths in a single place
1 parent 8085cfc commit b7445ac

5 files changed

Lines changed: 128 additions & 141 deletions

File tree

simulaqron/settings/__init__.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .network_config import NetworkConfigBuilder, DEFAULT_SIMULAQRON_NETWORK_FILENAME
2-
from .simulaqron_config import SimulaqronConfig, DEFAULT_SIMULAQRON_SETTINGS_FILENAME
1+
from .network_config import NetworkConfigBuilder, DEFAULT_SIMULAQRON_NETWORK_FILENAME, LOCAL_NETWORK_SETTINGS, HOME_NETWORK_SETTINGS
2+
from .simulaqron_config import SimulaqronConfig, DEFAULT_SIMULAQRON_SETTINGS_FILENAME, LOCAL_SIMULAQRON_SETTINGS, HOME_SIMULAQRON_SETTINGS
33
from ._serialization import init_serialization
44

55
init_serialization()
@@ -12,10 +12,4 @@
1212
# Centralized way to store the config of the network. It reads the local
1313
# configuration if exists, otherwise, it simply populates the in-memory
1414
# configs object with the default values
15-
network_config = NetworkConfigBuilder.load_from_known_sources()
16-
17-
# We follow a similar approach with the network config builder: read the
18-
# file pointed by the simulaqron_settings (if exists) or initialize a new
19-
# builder that contains only the default network.
20-
# network_configs = NetworkConfigBuilder()
21-
# network_configs.read_from_file(simulaqron_settings.network_config_file)
15+
network_config = NetworkConfigBuilder.load_from_known_sources()

simulaqron/settings/_serialization.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
from typing import Type, Dict, Any, List
44

55
from dataclasses_serialization.json import JSONSerializer
6-
from dataclasses_serialization.serializer_base import (DeserializationError,
7-
dict_serialization)
6+
from dataclasses_serialization.serializer_base import DeserializationError
87

98
from .network_config import NodeConfig, NetworkConfig, NetworkConfigBuilder
10-
from ..settings.simulaqron_config import (DEFAULT_SIMULAQRON_SETTINGS_FILENAME,
11-
SimulaqronConfig, SimBackend)
9+
from ..settings import HOME_SIMULAQRON_SETTINGS
10+
from ..settings.simulaqron_config import SimulaqronConfig, SimBackend
1211

1312

1413
def init_serialization():
@@ -45,10 +44,11 @@ def path_serializer(obj: Path) -> str:
4544
# Registration of Deserializer for python enums
4645
@JSONSerializer.register_deserializer(Path)
4746
def path_deserializer(cls: Type[Path], path: str) -> Path:
48-
if path == "$DEFAULT_NETWORK":
49-
return (cls.home() / ".simulaqron" / DEFAULT_SIMULAQRON_SETTINGS_FILENAME).resolve()
50-
# If the path is given, we will resolve it later
51-
return cls(path)
47+
match path:
48+
case "$DEFAULT_NETWORK":
49+
return HOME_SIMULAQRON_SETTINGS
50+
case _:
51+
return cls(path)
5252

5353

5454
@JSONSerializer.register_deserializer(SimulaqronConfig)

simulaqron/settings/network_config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
import simulaqron._default_config
1414

15+
# Some helpers paths that point to the usual locations where the
16+
# configurations can reside:
1517
DEFAULT_SIMULAQRON_NETWORK_FILENAME = "simulaqron_network.json"
18+
HOME_NETWORK_SETTINGS = (Path.home() / ".simulaqron" / DEFAULT_SIMULAQRON_NETWORK_FILENAME).resolve()
19+
LOCAL_NETWORK_SETTINGS = (Path.cwd() / DEFAULT_SIMULAQRON_NETWORK_FILENAME).resolve()
1620

1721

1822
class NodeConfigType(StrEnum):
@@ -421,8 +425,8 @@ def _deserialize_from_file(cls, file_path: Path) -> Self:
421425

422426
@classmethod
423427
def load_from_known_sources(cls) -> Self:
424-
cwd_networks_file = (Path.cwd() / DEFAULT_SIMULAQRON_NETWORK_FILENAME).resolve()
425-
home_networks_file = (Path.home() / ".simulaqron" / DEFAULT_SIMULAQRON_NETWORK_FILENAME).resolve()
428+
cwd_networks_file = LOCAL_NETWORK_SETTINGS.resolve()
429+
home_networks_file = HOME_NETWORK_SETTINGS.resolve()
426430

427431
files_to_load = [cwd_networks_file, home_networks_file]
428432

simulaqron/settings/simulaqron_config.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
from dataclasses_serialization.json import JSONSerializer
4141
from dataclasses_serialization.json import JSONSerializerMixin
4242

43-
# This is the name of the "local" simulaqron settings.
44-
# If a file named like this is found in the CWD, it will be
45-
# automatically loaded when creating the config file
43+
# Some helpers paths that point to the usual locations where the
44+
# configurations can reside:
4645
DEFAULT_SIMULAQRON_SETTINGS_FILENAME = "simulaqron_settings.json"
46+
LOCAL_SIMULAQRON_SETTINGS = (Path.cwd() / DEFAULT_SIMULAQRON_SETTINGS_FILENAME).resolve()
47+
HOME_SIMULAQRON_SETTINGS = (Path.home() / ".simulaqron" / DEFAULT_SIMULAQRON_SETTINGS_FILENAME).resolve()
4748

4849

4950
class SimBackend(JSONSerializerMixin, Enum):
@@ -97,8 +98,8 @@ def _deserialize_from_file(cls, file_path: Path) -> Self:
9798

9899
@classmethod
99100
def load_from_known_sources(cls) -> Self:
100-
cwd_settings_file = (Path.cwd() / DEFAULT_SIMULAQRON_SETTINGS_FILENAME).resolve()
101-
home_settings_file = (Path.home() / ".simulaqron" / DEFAULT_SIMULAQRON_SETTINGS_FILENAME).resolve()
101+
cwd_settings_file = LOCAL_SIMULAQRON_SETTINGS.resolve()
102+
home_settings_file = HOME_SIMULAQRON_SETTINGS.resolve()
102103

103104
files_to_load = [cwd_settings_file, home_settings_file]
104105

0 commit comments

Comments
 (0)