|
1 | 1 | import importlib
|
2 | 2 | import pkgutil
|
3 |
| -from typing import Dict, Type |
| 3 | +import warnings |
| 4 | +from pathlib import Path |
| 5 | +from typing import Dict, Iterable, Type |
4 | 6 |
|
5 | 7 | from commitizen.cz.base import BaseCommitizen
|
6 | 8 | from commitizen.cz.conventional_commits import ConventionalCommitsCz
|
7 | 9 | from commitizen.cz.customize import CustomizeCommitsCz
|
8 | 10 | from commitizen.cz.jira import JiraSmartCz
|
9 | 11 |
|
| 12 | + |
| 13 | +def discover_plugins(path: Iterable[Path] = None) -> Dict[str, Type[BaseCommitizen]]: |
| 14 | + """Discover commitizen plugins on the path |
| 15 | +
|
| 16 | + Args: |
| 17 | + path (Path, optional): If provided, 'path' should be either None or a list of paths to look for |
| 18 | + modules in. If path is None, all top-level modules on sys.path.. Defaults to None. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + Dict[str, Type[BaseCommitizen]]: Registry with found plugins |
| 22 | + """ |
| 23 | + plugins = {} |
| 24 | + for finder, name, ispkg in pkgutil.iter_modules(path): |
| 25 | + try: |
| 26 | + if name.startswith("cz_"): |
| 27 | + plugins[name] = importlib.import_module(name).discover_this |
| 28 | + except AttributeError as e: |
| 29 | + warnings.warn(UserWarning(e.args[0])) |
| 30 | + continue |
| 31 | + return plugins |
| 32 | + |
| 33 | + |
10 | 34 | registry: Dict[str, Type[BaseCommitizen]] = {
|
11 | 35 | "cz_conventional_commits": ConventionalCommitsCz,
|
12 | 36 | "cz_jira": JiraSmartCz,
|
13 | 37 | "cz_customize": CustomizeCommitsCz,
|
14 | 38 | }
|
15 |
| -plugins = { |
16 |
| - name: importlib.import_module(name).discover_this # type: ignore |
17 |
| - for finder, name, ispkg in pkgutil.iter_modules() |
18 |
| - if name.startswith("cz_") |
19 |
| -} |
20 | 39 |
|
21 |
| -registry.update(plugins) |
| 40 | +registry.update(discover_plugins()) |
0 commit comments