Skip to content

Commit 296a613

Browse files
committed
Fix issue with the handling of --config-dir.
This patch fixes #389.
1 parent b030c7b commit 296a613

File tree

3 files changed

+84
-19
lines changed

3 files changed

+84
-19
lines changed

PyFunceble/config/loader.py

+82-17
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ class ConfigLoader:
9292
_path_to_config: Optional[str] = None
9393
_remote_config_location: Optional[str] = None
9494
path_to_default_config: Optional[str] = None
95-
path_to_overwrite_config: Optional[str] = None
95+
_path_to_overwrite_config: Optional[str] = None
9696

9797
_custom_config: dict = {}
9898
_merge_upstream: bool = False
9999
_config_dir: Optional[str] = None
100+
__config_loaded: bool = False
100101

101102
file_helper: FileHelper = FileHelper()
102103
dict_helper: DictHelper = DictHelper()
@@ -115,18 +116,8 @@ def __init__(
115116
else:
116117
self.config_dir = PyFunceble.storage.CONFIG_DIRECTORY
117118

118-
self.path_to_config = os.path.join(
119-
self.config_dir,
120-
PyFunceble.storage.CONFIGURATION_FILENAME,
121-
)
122-
123119
self.path_to_remote_config = None
124120

125-
self.path_to_overwrite_config = os.path.join(
126-
self.config_dir,
127-
".PyFunceble.overwrite.yaml",
128-
)
129-
130121
if merge_upstream is not None:
131122
self.merge_upstream = merge_upstream
132123
elif EnvironmentVariableHelper("PYFUNCEBLE_AUTO_CONFIGURATION").exists():
@@ -206,14 +197,82 @@ def conditional_switch(config: dict) -> dict:
206197

207198
return config
208199

209-
@staticmethod
210-
def is_already_loaded() -> bool:
200+
def is_already_loaded(self) -> bool:
211201
"""
212202
Checks if the configuration was already loaded.
213203
"""
214204

215205
return bool(PyFunceble.storage.CONFIGURATION)
216206

207+
def __is_completely_loaded(self) -> bool:
208+
"""
209+
Checks if the configuration was completely loaded.
210+
"""
211+
212+
return self.is_already_loaded() and bool(self.__config_loaded)
213+
214+
@property
215+
def path_to_config(self) -> Optional[str]:
216+
"""
217+
Provides the current state of the :code:`_path_to_config` attribute.
218+
"""
219+
220+
if self._path_to_config is None:
221+
self._path_to_config = os.path.join(
222+
self.config_dir,
223+
PyFunceble.storage.CONFIGURATION_FILENAME,
224+
)
225+
226+
return self._path_to_config
227+
228+
@path_to_config.setter
229+
def path_to_config(self, value: str) -> None:
230+
"""
231+
Sets the path to the configuration file.
232+
233+
:param value:
234+
The value to set.
235+
236+
:raise TypeError:
237+
When value is not a :py:class:`str`.
238+
"""
239+
240+
if not isinstance(value, str):
241+
raise TypeError(f"<value> should be {str}, {type(value)} given.")
242+
243+
self._path_to_config = value
244+
245+
@property
246+
def path_to_overwrite_config(self) -> Optional[str]:
247+
"""
248+
Provides the current state of the :code:`_path_to_overwrite_config` attribute.
249+
"""
250+
251+
if self._path_to_overwrite_config is None:
252+
self._path_to_overwrite_config = os.path.join(
253+
self.config_dir,
254+
".PyFunceble.overwrite.yaml",
255+
)
256+
257+
return self._path_to_overwrite_config
258+
259+
@path_to_overwrite_config.setter
260+
def path_to_overwrite_config(self, value: str) -> None:
261+
"""
262+
Sets the path to the overwrite configuration file.
263+
264+
:param value:
265+
The value to set.
266+
267+
:raise TypeError:
268+
When value is not a :py:class:`str`.
269+
"""
270+
271+
if not isinstance(value, str):
272+
raise TypeError(f"<value> should be {str}, {type(value)} given.")
273+
274+
self._path_to_overwrite_config = value
275+
217276
@property
218277
def config_dir(self) -> Optional[str]:
219278
"""
@@ -239,6 +298,9 @@ def config_dir(self, value: str) -> None:
239298
raise TypeError(f"<value> should be {str}, {type(value)} given.")
240299

241300
self._config_dir = value
301+
# Reset the path to the configuration file.
302+
self._path_to_config = None
303+
self._path_to_overwrite_config = None
242304

243305
def set_config_dir(self, value: str) -> "ConfigLoader":
244306
"""
@@ -385,9 +447,8 @@ def install_missing_infrastructure_files(
385447

386448
return self
387449

388-
@classmethod
389450
def download_dynamic_infrastructure_files(
390-
cls,
451+
self,
391452
) -> "ConfigLoader":
392453
"""
393454
Downloads all the dynamicly (generated) infrastructure files.
@@ -400,7 +461,7 @@ def download_dynamic_infrastructure_files(
400461

401462
## pragma: no cover ## Underlying download methods already tested.
402463

403-
if not cls.is_already_loaded():
464+
if not self.is_already_loaded():
404465
IANADownloader().start()
405466
PublicSuffixDownloader().start()
406467
UserAgentsDownloader().start()
@@ -494,7 +555,7 @@ def download_remote_config(src: str, dest: str = None) -> None:
494555
config,
495556
)
496557

497-
if not self.is_already_loaded():
558+
if not self.__is_completely_loaded():
498559
self.install_missing_infrastructure_files()
499560
self.download_dynamic_infrastructure_files()
500561
download_remote_config(
@@ -593,6 +654,8 @@ def start(self) -> "ConfigLoader":
593654
# Early load user agents to allow usage of defined user agents.
594655
UserAgentDataset().get_latest()
595656

657+
self.__config_loaded = True
658+
596659
return self
597660

598661
def destroy(self, keep_custom: bool = False) -> "ConfigLoader":
@@ -620,4 +683,6 @@ def destroy(self, keep_custom: bool = False) -> "ConfigLoader":
620683
# This is not a mistake.
621684
self._custom_config = {}
622685

686+
self.__config_loaded = False
687+
623688
return self

PyFunceble/storage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
from PyFunceble.storage_facility import get_config_directory
6161

6262
PROJECT_NAME: str = "PyFunceble"
63-
PROJECT_VERSION: str = "4.3.0a12.dev (Blue Duckling: Tulip)"
63+
PROJECT_VERSION: str = "4.3.0a13.dev (Blue Duckling: Tulip)"
6464

6565
DISTRIBUTED_CONFIGURATION_FILENAME: str = ".PyFunceble_production.yaml"
6666

version.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
current_version: '4.3.0a12.dev (Blue Duckling: Tulip)'
1+
current_version: '4.3.0a13.dev (Blue Duckling: Tulip)'
22
deprecated:
33
- 3.0.21
44
- 3.1.20

0 commit comments

Comments
 (0)