forked from taskiq-python/taskiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
99 lines (82 loc) · 2.75 KB
/
Copy pathutils.py
File metadata and controls
99 lines (82 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import sys
from contextlib import contextmanager
from importlib import import_module
from logging import getLogger
from pathlib import Path
from typing import Any, Generator, List, Sequence, Union
from taskiq.utils import remove_suffix
logger = getLogger("taskiq.worker")
@contextmanager
def add_cwd_in_path() -> Generator[None, None, None]:
"""
Adds current directory in python path.
This context manager adds current directory in sys.path,
so all python files are discoverable now, without installing
current project.
:yield: none
"""
cwd = Path.cwd()
if str(cwd) in sys.path:
yield
else:
logger.debug(f"Inserting {cwd} in sys.path")
sys.path.insert(0, str(cwd))
try:
yield
finally:
try:
sys.path.remove(str(cwd))
except ValueError:
logger.warning(f"Cannot remove '{cwd}' from sys.path")
def import_object(object_spec: str) -> Any:
"""
It parses python object spec and imports it.
:param object_spec: string in format like `package.module:variable`
:raises ValueError: if spec has unknown format.
:returns: imported broker.
"""
import_spec = object_spec.split(":")
if len(import_spec) != 2:
raise ValueError("You should provide object path in `module:variable` format.")
with add_cwd_in_path():
module = import_module(import_spec[0])
return getattr(module, import_spec[1])
def import_from_modules(modules: List[str]) -> None:
"""
Import all modules from modules variable.
:param modules: list of modules.
"""
for module in modules:
try:
logger.info(f"Importing tasks from module {module}")
with add_cwd_in_path():
import_module(module)
except ImportError as err:
logger.warning(f"Cannot import {module}. Cause:")
logger.exception(err)
def import_tasks(
modules: List[str],
pattern: Union[str, Sequence[str]],
fs_discover: bool,
) -> None:
"""
Import tasks modules.
This function is used to
import all tasks from modules.
:param modules: list of modules to import.
:param pattern: pattern of a file if fs_discover is True.
:param fs_discover: If true it will try to import modules
from filesystem.
"""
if fs_discover:
if isinstance(pattern, str):
pattern = (pattern,)
discovered_modules = set()
for glob_pattern in pattern:
for path in Path().glob(glob_pattern):
discovered_modules.add(
remove_suffix(str(path), ".py").replace(os.path.sep, "."),
)
modules.extend(list(discovered_modules))
import_from_modules(modules)