Service locator for managing the services used by Crawlee. All services are initialized to its default value lazily.
| 18 | |
| 19 | @docs_group('Configuration') |
| 20 | class ServiceLocator: |
| 21 | """Service locator for managing the services used by Crawlee. |
| 22 | |
| 23 | All services are initialized to its default value lazily. |
| 24 | """ |
| 25 | |
| 26 | global_storage_instance_manager: StorageInstanceManager | None = None |
| 27 | |
| 28 | def __init__( |
| 29 | self, |
| 30 | configuration: Configuration | None = None, |
| 31 | event_manager: EventManager | None = None, |
| 32 | storage_client: StorageClient | None = None, |
| 33 | ) -> None: |
| 34 | self._configuration = configuration |
| 35 | self._event_manager = event_manager |
| 36 | self._storage_client = storage_client |
| 37 | |
| 38 | def get_configuration(self) -> Configuration: |
| 39 | """Get the configuration.""" |
| 40 | if self._configuration is None: |
| 41 | logger.debug('No configuration set, implicitly creating and using default Configuration.') |
| 42 | self._configuration = Configuration() |
| 43 | |
| 44 | return self._configuration |
| 45 | |
| 46 | def set_configuration(self, configuration: Configuration) -> None: |
| 47 | """Set the configuration. |
| 48 | |
| 49 | Args: |
| 50 | configuration: The configuration to set. |
| 51 | |
| 52 | Raises: |
| 53 | ServiceConflictError: If the configuration has already been retrieved before. |
| 54 | """ |
| 55 | if self._configuration is configuration: |
| 56 | # Same instance, no need to anything |
| 57 | return |
| 58 | if self._configuration: |
| 59 | raise ServiceConflictError(Configuration, configuration, self._configuration) |
| 60 | |
| 61 | self._configuration = configuration |
| 62 | |
| 63 | def get_event_manager(self) -> EventManager: |
| 64 | """Get the event manager.""" |
| 65 | if self._event_manager is None: |
| 66 | logger.debug('No event manager set, implicitly creating and using default LocalEventManager.') |
| 67 | if self._configuration is None: |
| 68 | logger.warning( |
| 69 | 'Implicit creation of event manager will implicitly set configuration as side effect. ' |
| 70 | 'It is advised to explicitly first set the configuration instead.' |
| 71 | ) |
| 72 | self._event_manager = LocalEventManager().from_config(config=self._configuration) |
| 73 | |
| 74 | return self._event_manager |
| 75 | |
| 76 | def set_event_manager(self, event_manager: EventManager) -> None: |
| 77 | """Set the event manager. |
no outgoing calls