|
4 | 4 | from importlib import import_module |
5 | 5 | from logging import getLogger |
6 | 6 | from pathlib import Path |
7 | | -from typing import Any, Generator, List, Sequence, Union |
| 7 | +from typing import Any, Generator, List, Sequence, Union, Optional |
8 | 8 |
|
9 | 9 | logger = getLogger("taskiq.worker") |
10 | 10 |
|
@@ -35,18 +35,21 @@ def add_cwd_in_path() -> Generator[None, None, None]: |
35 | 35 | logger.warning(f"Cannot remove '{cwd}' from sys.path") |
36 | 36 |
|
37 | 37 |
|
38 | | -def import_object(object_spec: str) -> Any: |
| 38 | +def import_object(object_spec: str, app_dir: Optional[str] = None) -> Any: |
39 | 39 | """ |
40 | 40 | It parses python object spec and imports it. |
41 | 41 |
|
42 | 42 | :param object_spec: string in format like `package.module:variable` |
| 43 | + :param app_dir: directory to add in sys.path for importing. |
43 | 44 | :raises ValueError: if spec has unknown format. |
44 | 45 | :returns: imported broker. |
45 | 46 | """ |
46 | 47 | import_spec = object_spec.split(":") |
47 | 48 | if len(import_spec) != 2: |
48 | 49 | raise ValueError("You should provide object path in `module:variable` format.") |
49 | 50 | with add_cwd_in_path(): |
| 51 | + if app_dir: |
| 52 | + sys.path.insert(0, app_dir) |
50 | 53 | module = import_module(import_spec[0]) |
51 | 54 | return getattr(module, import_spec[1]) |
52 | 55 |
|
|
0 commit comments