1
1
from __future__ import annotations
2
2
3
- from dataclasses import dataclass , field
4
- from typing import TYPE_CHECKING
5
-
6
3
from crawlee ._utils .docs import docs_group
7
-
8
- if TYPE_CHECKING :
9
- from crawlee .base_storage_client ._base_storage_client import BaseStorageClient
10
- from crawlee .configuration import Configuration
11
- from crawlee .events ._event_manager import EventManager
4
+ from crawlee .base_storage_client ._base_storage_client import BaseStorageClient
5
+ from crawlee .configuration import Configuration
6
+ from crawlee .errors import ServiceConflictError
7
+ from crawlee .events ._event_manager import EventManager
12
8
13
9
__all__ = [
14
10
'get_configuration' ,
20
16
]
21
17
22
18
23
- @dataclass
24
19
class _ServiceLocator :
25
20
"""Service locator for managing the services used by Crawlee.
26
21
27
22
All services are initialized to its default value lazily.
28
23
"""
29
24
30
- _configuration : Configuration | None = field (default = None , init = False )
31
- _event_manager : EventManager | None = field (default = None , init = False )
32
- _storage_client : BaseStorageClient | None = field (default = None , init = False )
25
+ def __init__ (self ) -> None :
26
+ self ._configuration : Configuration | None = None
27
+ self ._event_manager : EventManager | None = None
28
+ self ._storage_client : BaseStorageClient | None = None
29
+
30
+ # Flags to check if the services were already set.
31
+ self ._configuration_was_set = False
32
+ self ._event_manager_was_set = False
33
+ self ._storage_client_was_set = False
33
34
34
35
@property
35
36
def configuration (self ) -> Configuration :
36
37
if self ._configuration is None :
37
- from crawlee .configuration import Configuration
38
-
39
38
self ._configuration = Configuration ()
40
39
41
40
return self ._configuration
42
41
43
42
@configuration .setter
44
43
def configuration (self , value : Configuration ) -> None :
45
44
self ._configuration = value
45
+ self ._configuration_was_set = True
46
46
47
47
@property
48
48
def storage_client (self ) -> BaseStorageClient :
@@ -56,6 +56,7 @@ def storage_client(self) -> BaseStorageClient:
56
56
@storage_client .setter
57
57
def storage_client (self , value : BaseStorageClient ) -> None :
58
58
self ._storage_client = value
59
+ self ._storage_client_was_set = True
59
60
60
61
@property
61
62
def event_manager (self ) -> EventManager :
@@ -69,6 +70,19 @@ def event_manager(self) -> EventManager:
69
70
@event_manager .setter
70
71
def event_manager (self , value : EventManager ) -> None :
71
72
self ._event_manager = value
73
+ self ._event_manager_was_set = True
74
+
75
+ @property
76
+ def configuration_was_set (self ) -> bool :
77
+ return self ._configuration_was_set
78
+
79
+ @property
80
+ def event_manager_was_set (self ) -> bool :
81
+ return self ._event_manager_was_set
82
+
83
+ @property
84
+ def storage_client_was_set (self ) -> bool :
85
+ return self ._storage_client_was_set
72
86
73
87
74
88
_service_locator = _ServiceLocator ()
@@ -81,8 +95,23 @@ def get_configuration() -> Configuration:
81
95
82
96
83
97
@docs_group ('Functions' )
84
- def set_configuration (configuration : Configuration ) -> None :
85
- """Set the configuration."""
98
+ def set_configuration (
99
+ configuration : Configuration ,
100
+ * ,
101
+ force : bool = False ,
102
+ ) -> None :
103
+ """Set the configuration.
104
+
105
+ Args:
106
+ configuration: The configuration to set.
107
+ force: If True, the configuration will be set even if it was already set.
108
+
109
+ Raises:
110
+ ServiceConflictError: If the configuration was already set.
111
+ """
112
+ if _service_locator .configuration_was_set and not force :
113
+ raise ServiceConflictError (Configuration , configuration , _service_locator .configuration )
114
+
86
115
_service_locator .configuration = configuration
87
116
88
117
@@ -93,8 +122,23 @@ def get_event_manager() -> EventManager:
93
122
94
123
95
124
@docs_group ('Functions' )
96
- def set_event_manager (event_manager : EventManager ) -> None :
97
- """Set the event manager."""
125
+ def set_event_manager (
126
+ event_manager : EventManager ,
127
+ * ,
128
+ force : bool = False ,
129
+ ) -> None :
130
+ """Set the event manager.
131
+
132
+ Args:
133
+ event_manager: The event manager to set.
134
+ force: If True, the event manager will be set even if it was already set.
135
+
136
+ Raises:
137
+ ServiceConflictError: If the event manager was already set.
138
+ """
139
+ if _service_locator .event_manager_was_set and not force :
140
+ raise ServiceConflictError (EventManager , event_manager , _service_locator .event_manager )
141
+
98
142
_service_locator .event_manager = event_manager
99
143
100
144
@@ -105,6 +149,21 @@ def get_storage_client() -> BaseStorageClient:
105
149
106
150
107
151
@docs_group ('Functions' )
108
- def set_storage_client (storage_client : BaseStorageClient ) -> None :
109
- """Set the storage client."""
152
+ def set_storage_client (
153
+ storage_client : BaseStorageClient ,
154
+ * ,
155
+ force : bool = False ,
156
+ ) -> None :
157
+ """Set the storage client.
158
+
159
+ Args:
160
+ storage_client: The storage client to set.
161
+ force: If True, the storage client will be set even if it was already set.
162
+
163
+ Raises:
164
+ ServiceConflictError: If the storage client was already set.
165
+ """
166
+ if _service_locator .storage_client_was_set and not force :
167
+ raise ServiceConflictError (BaseStorageClient , storage_client , _service_locator .storage_client )
168
+
110
169
_service_locator .storage_client = storage_client
0 commit comments