Skip to content

Commit

Permalink
Merge pull request #112 from pierec/support-settings-factories
Browse files Browse the repository at this point in the history
Support settings factories
  • Loading branch information
tobymao authored Feb 19, 2024
2 parents 6607af6 + 7735a27 commit e43751f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ To start the worker, assuming the previous is available in the python path
saq module.file.settings
```

> **_Note:_** `module.file.settings` can also be a callable returning the settings dictionary.
To enqueue jobs

```python
Expand Down
7 changes: 6 additions & 1 deletion saq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,12 @@ def import_settings(settings: str) -> dict[str, t.Any]:
# given a.b.c, parses out a.b as the module path and c as the variable
module_path, name = settings.strip().rsplit(".", 1)
module = importlib.import_module(module_path)
return getattr(module, name)
settings_obj = getattr(module, name)

if callable(settings_obj):
settings_obj = settings_obj()

return settings_obj


def start(
Expand Down
72 changes: 72 additions & 0 deletions tests/test_settings_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from __future__ import annotations

import contextlib
import secrets
import sys
import tempfile
import textwrap
import unittest
from pathlib import Path

from saq.worker import import_settings


class TestSettingsImport(unittest.TestCase):
def setUp(self) -> None:
self.cm = cm = contextlib.ExitStack()

tempdir = Path(cm.enter_context(tempfile.TemporaryDirectory()))
root_module_name = "foo" + secrets.token_urlsafe(2)
file_tree = [
tempdir / root_module_name / "__init__.py",
tempdir / root_module_name / "bar" / "__init__.py",
tempdir / root_module_name / "bar" / "settings.py",
]
for path in file_tree:
path.parent.mkdir(exist_ok=True, parents=True)
path.touch()

file_tree[-1].write_text(
textwrap.dedent(
"""
static = {
"functions": ["pretend_its_a_fn"],
"concurrency": 100
}
def factory():
return {
"functions": ["pretend_its_some_other_fn"],
"concurrency": static["concurrency"] + 100
}
"""
).strip()
)
sys.path.append(str(tempdir))

self.module_path = f"{root_module_name}.bar.settings"

def tearDown(self) -> None:
self.cm.close()

def test_imports_settings_from_module_path(self) -> None:
settings = import_settings(self.module_path + ".static")

self.assertDictEqual(
settings,
{
"functions": ["pretend_its_a_fn"],
"concurrency": 100,
},
)

def test_calls_settings_factory(self) -> None:
settings = import_settings(self.module_path + ".factory")

self.assertDictEqual(
settings,
{
"functions": ["pretend_its_some_other_fn"],
"concurrency": 200,
},
)

0 comments on commit e43751f

Please sign in to comment.