Skip to content

Commit 28ebd8d

Browse files
committed
inline finalize
1 parent 35ad0dd commit 28ebd8d

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

src/apify/_actor.py

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,12 @@ def apify_client(self) -> ApifyClientAsync:
193193
self._apify_client = self.new_client()
194194
return self._apify_client
195195

196-
@property
196+
@cached_property
197197
def configuration(self) -> Configuration:
198198
"""The Configuration instance the Actor instance uses."""
199199
if self._configuration:
200200
return self._configuration
201201

202-
return self._finalize_implicit_configuration()
203-
204-
def _finalize_implicit_configuration(self) -> Configuration:
205-
"""Set implicit configuration in the Actor and return it.
206-
207-
Changing Actor or service_locator configuration after this method was run is not possible.
208-
"""
209202
try:
210203
# Set implicit default Apify configuration, unless configuration was already set.
211204
implicit_configuration = Configuration()
@@ -216,34 +209,10 @@ def _finalize_implicit_configuration(self) -> Configuration:
216209
'Configuration in service locator was set explicitly before Actor.init was called.'
217210
'Using the existing configuration as implicit configuration for the Actor.'
218211
)
212+
219213
# Use the configuration from the service locator
220214
self._configuration = Configuration.get_global_configuration()
221-
return self.configuration
222-
223-
def _finalize_implicit_local_storage_client(self) -> StorageClient:
224-
"""Set implicit local storage client in the Actor and return it.
225-
226-
Changing Actor or service_locator storage client after this method was run is not possible.
227-
"""
228-
try:
229-
# Set implicit default local storage client, unless local storage client was already set.
230-
implicit_storage_client = ApifyFileSystemStorageClient()
231-
service_locator.set_storage_client(implicit_storage_client)
232-
self._local_storage_client = implicit_storage_client
233-
except ServiceConflictError:
234-
self.log.debug(
235-
'Storage client in service locator was set explicitly before Actor.init was called.'
236-
'Using the existing storage client as implicit storage client for the Actor.'
237-
)
238-
239-
self._local_storage_client = service_locator.get_storage_client()
240-
if type(self._local_storage_client) is FileSystemStorageClient:
241-
self.log.warning(
242-
f'Using {FileSystemStorageClient.__module__}.{FileSystemStorageClient.__name__} in Actor context is not'
243-
f' recommended and can lead to problems with reading the input file. Use '
244-
f'`apify.storage_clients.FileSystemStorageClient` instead.'
245-
)
246-
return self._local_storage_client
215+
return self._configuration
247216

248217
@cached_property
249218
def event_manager(self) -> EventManager:
@@ -269,7 +238,27 @@ def _get_local_storage_client(self) -> StorageClient:
269238
"""Get the local storage client the Actor instance uses."""
270239
if self._local_storage_client:
271240
return self._local_storage_client
272-
return self._finalize_implicit_local_storage_client()
241+
242+
try:
243+
# Set implicit default local storage client, unless local storage client was already set.
244+
implicit_storage_client = ApifyFileSystemStorageClient()
245+
service_locator.set_storage_client(implicit_storage_client)
246+
self._local_storage_client = implicit_storage_client
247+
except ServiceConflictError:
248+
self.log.debug(
249+
'Storage client in service locator was set explicitly before Actor.init was called.'
250+
'Using the existing storage client as implicit storage client for the Actor.'
251+
)
252+
253+
self._local_storage_client = service_locator.get_storage_client()
254+
if type(self._local_storage_client) is FileSystemStorageClient:
255+
self.log.warning(
256+
f'Using {FileSystemStorageClient.__module__}.{FileSystemStorageClient.__name__} in Actor context is not'
257+
f' recommended and can lead to problems with reading the input file. Use '
258+
f'`apify.storage_clients.FileSystemStorageClient` instead.'
259+
)
260+
261+
return self._local_storage_client
273262

274263
def _raise_if_not_initialized(self) -> None:
275264
if not self._is_initialized:
@@ -300,7 +289,8 @@ async def init(self) -> None:
300289
# Set explicitly the configuration in the service locator
301290
service_locator.set_configuration(self.configuration)
302291
else:
303-
self._finalize_implicit_configuration()
292+
# Ensure that the configuration (cached property) is set
293+
_ = self.configuration
304294

305295
if self._is_initialized:
306296
raise RuntimeError('The Actor was already initialized!')
@@ -323,7 +313,8 @@ async def init(self) -> None:
323313
service_locator.set_storage_client(self._cloud_storage_client)
324314
self._local_storage_client = self._cloud_storage_client
325315
else:
326-
self._finalize_implicit_local_storage_client()
316+
self._get_local_storage_client()
317+
327318
service_locator.set_event_manager(self.event_manager)
328319

329320
# The logging configuration has to be called after all service_locator set methods.
@@ -522,6 +513,7 @@ async def open_key_value_store(
522513
"""
523514
self._raise_if_not_initialized()
524515
self._raise_if_cloud_requested_but_not_configured(force_cloud=force_cloud)
516+
525517
storage_client = self._cloud_storage_client if force_cloud else self._get_local_storage_client()
526518

527519
return await KeyValueStore.open(

0 commit comments

Comments
 (0)