Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/crawlee/_service_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
from typing import TYPE_CHECKING

from crawlee._utils.docs import docs_group
from crawlee.configuration import Configuration
from crawlee.errors import ServiceConflictError
from crawlee.events import EventManager, LocalEventManager
from crawlee.storage_clients import FileSystemStorageClient, StorageClient

if TYPE_CHECKING:
from crawlee.configuration import Configuration
from crawlee.events import EventManager
from crawlee.storage_clients import StorageClient
from crawlee.storages._storage_instance_manager import StorageInstanceManager

from logging import getLogger
Expand Down Expand Up @@ -38,6 +37,8 @@ def __init__(
def get_configuration(self) -> Configuration:
"""Get the configuration."""
if self._configuration is None:
from crawlee.configuration import Configuration # noqa: PLC0415

logger.debug('No configuration set, implicitly creating and using default Configuration.')
self._configuration = Configuration()

Expand All @@ -52,17 +53,23 @@ def set_configuration(self, configuration: Configuration) -> None:
Raises:
ServiceConflictError: If the configuration has already been retrieved before.
"""
from crawlee.errors import ServiceConflictError # noqa: PLC0415

if self._configuration is configuration:
# Same instance, no need to anything
return
if self._configuration:
from crawlee.configuration import Configuration # noqa: PLC0415

raise ServiceConflictError(Configuration, configuration, self._configuration)

self._configuration = configuration

def get_event_manager(self) -> EventManager:
"""Get the event manager."""
if self._event_manager is None:
from crawlee.events import LocalEventManager # noqa: PLC0415

logger.debug('No event manager set, implicitly creating and using default LocalEventManager.')
if self._configuration is None:
logger.warning(
Expand All @@ -82,17 +89,23 @@ def set_event_manager(self, event_manager: EventManager) -> None:
Raises:
ServiceConflictError: If the event manager has already been retrieved before.
"""
from crawlee.errors import ServiceConflictError # noqa: PLC0415

if self._event_manager is event_manager:
# Same instance, no need to anything
return
if self._event_manager:
from crawlee.events import EventManager # noqa: PLC0415

raise ServiceConflictError(EventManager, event_manager, self._event_manager)

self._event_manager = event_manager

def get_storage_client(self) -> StorageClient:
"""Get the storage client."""
if self._storage_client is None:
from crawlee.storage_clients import FileSystemStorageClient # noqa: PLC0415

logger.debug('No storage client set, implicitly creating and using default FileSystemStorageClient.')
if self._configuration is None:
logger.warning(
Expand All @@ -112,10 +125,14 @@ def set_storage_client(self, storage_client: StorageClient) -> None:
Raises:
ServiceConflictError: If the storage client has already been retrieved before.
"""
from crawlee.errors import ServiceConflictError # noqa: PLC0415

if self._storage_client is storage_client:
# Same instance, no need to anything
return
if self._storage_client:
from crawlee.storage_clients import StorageClient # noqa: PLC0415

raise ServiceConflictError(StorageClient, storage_client, self._storage_client)

self._storage_client = storage_client
Expand Down