Skip to content

Commit 66c0d53

Browse files
committed
minor
1 parent 6ccad08 commit 66c0d53

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

packages/service-library/src/servicelib/aiohttp/application_setup.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22
import inspect
33
import logging
4+
import warnings
45
from collections.abc import Callable
56
from contextlib import ContextDecorator
67
from copy import deepcopy
@@ -257,42 +258,27 @@ def app_module_setup(
257258
config_section: str | None = None,
258259
config_enabled: str | None = None,
259260
) -> Callable:
260-
"""Decorator that marks a function as 'a setup function' for a given module in an application
261-
262-
- Marks a function as 'setup' of a given module in an application
263-
- Ensures setup executed ONLY ONCE per app
264-
- Addon modules:
265-
- toggles run using 'enabled' entry in config file
266-
- logs execution
267-
268-
See packages/service-library/tests/test_application_setup.py
269-
270-
:param module_name: typically __name__
271-
:param depends: list of module_names that must be called first, defaults to None
272-
:param config_section: explicit configuration section, defaults to None (i.e. the name of the module, or last entry of the name if dotted)
273-
:param config_enabled: option in config to enable, defaults to None which is '$(module-section).enabled' (config_section and config_enabled are mutually exclusive)
274-
:param settings_name: field name in the app's settings that corresponds to this module. Defaults to the name of the module with app prefix.
275-
:raises DependencyError
276-
:raises ApplicationSetupError
277-
:return: True if setup was completed or False if setup was skipped
261+
"""Decorator marking a function as a module setup for an application.
262+
263+
Ensures one-time execution and tracks setup completion. For addons, setup can be
264+
toggled via config [deprecated] or settings.
265+
266+
:param settings_name: Field name in app settings for this module
267+
:raises DependencyError: If required dependent modules are not initialized
268+
:raises ApplicationSetupError: If setup fails
269+
:return: True if setup completed, False if skipped
278270
:rtype: bool
279271
280272
:Example:
281-
from servicelib.aiohttp.application_setup import app_module_setup
282-
283273
@app_module_setup('mysubsystem', ModuleCategory.SYSTEM, logger=log)
284-
def setup(app: web.Application):
274+
def setup_mysubsystem(app: web.Application):
285275
...
286276
"""
287-
# TODO: resilience to failure. if this setup fails, then considering dependencies, is it fatal or app can start?
288-
# TODO: enforce signature as def setup(app: web.Application, **kwargs) -> web.Application
289-
290277
module_name, depends, section, config_enabled = _parse_and_validate_arguments(
291278
module_name, depends, config_section, config_enabled
292279
)
293280

294-
# metadata info
295-
def _setup_metadata() -> SetupMetadataDict:
281+
def _get_metadata() -> SetupMetadataDict:
296282
return SetupMetadataDict(
297283
module_name=module_name,
298284
dependencies=depends,
@@ -317,7 +303,11 @@ def _wrapper(app: web.Application, *args, **kargs) -> bool:
317303

318304
if settings_name is None:
319305
# Fall back to config if settings_name is not explicitly defined
320-
# TODO: deprecate
306+
warnings.warn(
307+
f"Using config-based enabling/disabling for addon '{module_name}' is deprecated. Please use settings instead.",
308+
DeprecationWarning,
309+
stacklevel=2,
310+
)
321311
cfg = app[APP_CONFIG_KEY]
322312
is_enabled = _is_addon_enabled_from_config(
323313
cfg, config_enabled, section
@@ -367,7 +357,7 @@ def _wrapper(app: web.Application, *args, **kargs) -> bool:
367357
_wrapper.__wrapped__ == setup_func
368358
), "this is added by functools.wraps decorator" # nosec
369359

370-
setattr(_wrapper, "metadata", _setup_metadata) # noqa: B010
360+
setattr(_wrapper, "metadata", _get_metadata) # noqa: B010
371361
setattr(_wrapper, "mark_as_simcore_servicelib_setup_func", True) # noqa: B010
372362

373363
return _wrapper
@@ -376,7 +366,6 @@ def _wrapper(app: web.Application, *args, **kargs) -> bool:
376366

377367

378368
def is_setup_function(fun: Callable) -> bool:
379-
# TODO: use _SetupFunc protocol to check in runtime
380369
return (
381370
inspect.isfunction(fun)
382371
and hasattr(fun, "mark_as_simcore_servicelib_setup_func")

0 commit comments

Comments
 (0)