|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from crawlee._utils.docs import docs_group |
| 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 |
| 8 | + |
| 9 | + |
| 10 | +@docs_group('Classes') |
| 11 | +class ServiceLocator: |
| 12 | + """Service locator for managing the services used by Crawlee. |
| 13 | +
|
| 14 | + All services are initialized to its default value lazily. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self) -> None: |
| 18 | + self._configuration: Configuration | None = None |
| 19 | + self._event_manager: EventManager | None = None |
| 20 | + self._storage_client: BaseStorageClient | None = None |
| 21 | + |
| 22 | + # Flags to check if the services were already set. |
| 23 | + self._configuration_was_set = False |
| 24 | + self._event_manager_was_set = False |
| 25 | + self._storage_client_was_set = False |
| 26 | + |
| 27 | + def get_configuration(self) -> Configuration: |
| 28 | + """Get the configuration.""" |
| 29 | + if self._configuration is None: |
| 30 | + self._configuration = Configuration() |
| 31 | + |
| 32 | + return self._configuration |
| 33 | + |
| 34 | + def set_configuration(self, configuration: Configuration) -> None: |
| 35 | + """Set the configuration. |
| 36 | +
|
| 37 | + Args: |
| 38 | + configuration: The configuration to set. |
| 39 | +
|
| 40 | + Raises: |
| 41 | + ServiceConflictError: If the configuration was already set. |
| 42 | + """ |
| 43 | + if self._configuration_was_set: |
| 44 | + raise ServiceConflictError(Configuration, configuration, self._configuration) |
| 45 | + |
| 46 | + self._configuration = configuration |
| 47 | + self._configuration_was_set = True |
| 48 | + |
| 49 | + def get_event_manager(self) -> EventManager: |
| 50 | + """Get the event manager.""" |
| 51 | + if self._event_manager is None: |
| 52 | + from crawlee.events import LocalEventManager |
| 53 | + |
| 54 | + self._event_manager = LocalEventManager() |
| 55 | + |
| 56 | + return self._event_manager |
| 57 | + |
| 58 | + def set_event_manager(self, event_manager: EventManager) -> None: |
| 59 | + """Set the event manager. |
| 60 | +
|
| 61 | + Args: |
| 62 | + event_manager: The event manager to set. |
| 63 | +
|
| 64 | + Raises: |
| 65 | + ServiceConflictError: If the event manager was already set. |
| 66 | + """ |
| 67 | + if self._event_manager_was_set: |
| 68 | + raise ServiceConflictError(EventManager, event_manager, self._event_manager) |
| 69 | + |
| 70 | + self._event_manager = event_manager |
| 71 | + self._event_manager_was_set = True |
| 72 | + |
| 73 | + def get_storage_client(self) -> BaseStorageClient: |
| 74 | + """Get the storage client.""" |
| 75 | + if self._storage_client is None: |
| 76 | + from crawlee.memory_storage_client import MemoryStorageClient |
| 77 | + |
| 78 | + self._storage_client = MemoryStorageClient.from_config() |
| 79 | + |
| 80 | + return self._storage_client |
| 81 | + |
| 82 | + def set_storage_client(self, storage_client: BaseStorageClient) -> None: |
| 83 | + """Set the storage client. |
| 84 | +
|
| 85 | + Args: |
| 86 | + storage_client: The storage client to set. |
| 87 | +
|
| 88 | + Raises: |
| 89 | + ServiceConflictError: If the storage client was already set. |
| 90 | + """ |
| 91 | + if self._storage_client_was_set: |
| 92 | + raise ServiceConflictError(BaseStorageClient, storage_client, self._storage_client) |
| 93 | + |
| 94 | + self._storage_client = storage_client |
| 95 | + self._storage_client_was_set = True |
| 96 | + |
| 97 | + |
| 98 | +service_locator = ServiceLocator() |
0 commit comments