-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored worker intial content code
- Loading branch information
Showing
3 changed files
with
118 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from __future__ import annotations | ||
|
||
import inspect | ||
from types import ModuleType | ||
from typing import Any | ||
|
||
from celery import Celery | ||
from celery.app.base import PendingConfiguration | ||
|
||
from pytest_celery.vendors.worker.defaults import DEFAULT_WORKER_APP_NAME | ||
|
||
|
||
class WorkerInitialContent: | ||
class Parser: | ||
def imports_str(self, modules: set[ModuleType]) -> str: | ||
return "".join(f"from {module.__name__} import *\n" for module in modules) | ||
|
||
def imports_src(self, modules: set[ModuleType]) -> dict: | ||
src = dict() | ||
for module in modules: | ||
src[f"{module.__name__.replace('.', '/')}.py"] = inspect.getsource(module).encode() | ||
return src | ||
|
||
def app_name(self, name: str = DEFAULT_WORKER_APP_NAME) -> str: | ||
return f"app = Celery('{name}')" | ||
|
||
def config(self, app: Celery | None = None) -> str: | ||
app = app or Celery(DEFAULT_WORKER_APP_NAME) | ||
|
||
# Accessing the app.conf.changes.data property will trigger the PendingConfiguration to be resolved | ||
# and the changes will be applied to the app.conf, so we make a clone app to avoid affecting the | ||
# original app object. | ||
tmp_app = Celery(app.main) | ||
tmp_app.conf = app.conf | ||
|
||
changes = tmp_app.conf.changes.copy() | ||
if isinstance(changes, PendingConfiguration): | ||
changes = changes.data.changes | ||
if not isinstance(changes, dict): | ||
raise TypeError(f"Unexpected type for app.conf.changes: {type(changes)}") | ||
del changes["deprecated_settings"] | ||
|
||
if changes: | ||
changes = (f"\t{repr(key)}: {repr(value)}" for key, value in changes.items()) | ||
config = "config = {\n" + ",\n".join(changes) + "\n}" if changes else "config = None" | ||
else: | ||
config = "config = None" | ||
return config | ||
|
||
def __init__(self) -> None: | ||
self.parser = self.Parser() | ||
self._initial_content = { | ||
"__init__.py": b"", | ||
"imports": dict(), | ||
} | ||
self.set_app_name() | ||
self.set_config_from_object() | ||
|
||
def set_app_module(self, app_module: ModuleType) -> None: | ||
self._app_module_src = inspect.getsource(app_module) | ||
|
||
def add_modules(self, name: str, modules: set[ModuleType]) -> None: | ||
if not name: | ||
raise ValueError("name cannot be empty") | ||
|
||
if not modules: | ||
raise ValueError("modules cannot be empty") | ||
|
||
self._initial_content["imports"][name] = self.parser.imports_str(modules) # type: ignore | ||
self._initial_content.update(self.parser.imports_src(modules)) | ||
|
||
def set_app_name(self, name: str = DEFAULT_WORKER_APP_NAME) -> None: | ||
self._app = self.parser.app_name(name) | ||
|
||
def set_config_from_object(self, app: Celery | None = None) -> None: | ||
self._config = self.parser.config(app) | ||
|
||
def generate(self) -> dict: | ||
initial_content = self._initial_content.copy() | ||
|
||
_imports: dict | Any = initial_content.pop("imports") | ||
imports = "{%s}" % "}{".join(_imports.keys()) | ||
imports = imports.format(**_imports) | ||
|
||
app, config = self._app, self._config | ||
|
||
replacement_args = { | ||
"imports": "imports = None", | ||
"app": f'app = Celery("{DEFAULT_WORKER_APP_NAME}")', | ||
"config": "config = None", | ||
} | ||
self._app_module_src = self._app_module_src.replace(replacement_args["imports"], imports) | ||
self._app_module_src = self._app_module_src.replace(replacement_args["app"], app) | ||
self._app_module_src = self._app_module_src.replace(replacement_args["config"], config) | ||
|
||
initial_content["app.py"] = self._app_module_src.encode() | ||
|
||
return initial_content |