message-van 1.2.0__tar.gz → 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. {message_van-1.2.0 → message_van-2.0.0}/.gitignore +3 -1
  2. {message_van-1.2.0 → message_van-2.0.0}/PKG-INFO +1 -1
  3. {message_van-1.2.0 → message_van-2.0.0}/pyproject.toml +2 -2
  4. message_van-2.0.0/src/message_van/__init__.py +10 -0
  5. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/__init__.py +3 -0
  6. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/base/command_handler.py +1 -1
  7. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/base/event_handler.py +1 -1
  8. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/base/message_handler.py +1 -1
  9. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/message_handlers.py +11 -2
  10. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/message_van.py +6 -8
  11. message_van-2.0.0/src/message_van/models/message_van_meta.py +31 -0
  12. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/unit_of_work.py +1 -3
  13. message_van-2.0.0/src/message_van/signatures/__init__.py +13 -0
  14. message_van-2.0.0/src/message_van/signatures/function_lister.py +21 -0
  15. message_van-2.0.0/src/message_van/signatures/module_lister.py +71 -0
  16. message_van-2.0.0/src/message_van/signatures/signature_getter.py +80 -0
  17. message_van-2.0.0/src/message_van/signatures/signature_lister.py +88 -0
  18. message_van-2.0.0/src/message_van/util/__init__.py +6 -0
  19. message_van-2.0.0/src/message_van/util/src_dir.py +13 -0
  20. message_van-1.2.0/src/message_van/__init__.py +0 -19
  21. message_van-1.2.0/src/message_van/adapters/__init__.py +0 -8
  22. message_van-1.2.0/src/message_van/adapters/config_adapter.py +0 -28
  23. message_van-1.2.0/src/message_van/adapters/mappers/__init__.py +0 -6
  24. message_van-1.2.0/src/message_van/adapters/mappers/user_config_mapper.py +0 -5
  25. message_van-1.2.0/src/message_van/adapters/models/__init__.py +0 -6
  26. message_van-1.2.0/src/message_van/adapters/models/user_config.py +0 -9
  27. message_van-1.2.0/src/message_van/adapters/signature_adapter.py +0 -119
  28. message_van-1.2.0/src/message_van/adapters/util/__init__.py +0 -6
  29. message_van-1.2.0/src/message_van/adapters/util/toml_parser.py +0 -20
  30. message_van-1.2.0/src/message_van/bootstrap.py +0 -14
  31. message_van-1.2.0/src/message_van/config.py +0 -3
  32. message_van-1.2.0/src/message_van/domain/__init__.py +0 -7
  33. message_van-1.2.0/src/message_van/domain/entrypoint_decorator.py +0 -25
  34. message_van-1.2.0/src/message_van/service_layer/__init__.py +0 -6
  35. message_van-1.2.0/src/message_van/service_layer/service.py +0 -29
  36. {message_van-1.2.0 → message_van-2.0.0}/.github/workflows/publish.yml +0 -0
  37. {message_van-1.2.0 → message_van-2.0.0}/.python-version +0 -0
  38. {message_van-1.2.0 → message_van-2.0.0}/LICENSE.txt +0 -0
  39. {message_van-1.2.0 → message_van-2.0.0}/README.md +0 -0
  40. {message_van-1.2.0 → message_van-2.0.0}/docs/img/icon.png +0 -0
  41. {message_van-1.2.0 → message_van-2.0.0}/docs/img/logo.png +0 -0
  42. {message_van-1.2.0 → message_van-2.0.0}/requirements-dev.lock +0 -0
  43. {message_van-1.2.0 → message_van-2.0.0}/requirements.lock +0 -0
  44. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/exceptions/__init__.py +0 -0
  45. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/exceptions/already_registered_handler_error.py +0 -0
  46. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/exceptions/unknown_handler_error.py +0 -0
  47. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/base/__init__.py +0 -0
  48. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/base/command.py +0 -0
  49. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/base/event.py +0 -0
  50. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/base/message.py +0 -0
  51. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/handler_signature.py +0 -0
  52. {message_van-1.2.0/src/message_van/domain → message_van-2.0.0/src/message_van}/models/message_handler_type.py +0 -0
@@ -170,4 +170,6 @@ cython_debug/
170
170
  # PyPI configuration file
171
171
  .pypirc
172
172
 
173
- .idea/
173
+ .idea/
174
+
175
+ __*main*__.py
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: message-van
3
- Version: 1.2.0
3
+ Version: 2.0.0
4
4
  Summary: Python Event-Driven Architecture without the Administration.
5
5
  Author-email: Thomas Vanhelden <thomas.vanhelden@gmail.com>
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "message-van"
3
- version = "1.2.0"
3
+ version = "2.0.0"
4
4
  description = "Python Event-Driven Architecture without the Administration."
5
5
  authors = [
6
6
  { name = "Thomas Vanhelden", email = "thomas.vanhelden@gmail.com" }
@@ -37,7 +37,7 @@ packages = ["src/message_van"]
37
37
 
38
38
 
39
39
  [tool.bumpversion]
40
- current_version = "1.2.0"
40
+ current_version = "2.0.0"
41
41
  parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
42
42
  serialize = ["{major}.{minor}.{patch}"]
43
43
  search = "{current_version}"
@@ -0,0 +1,10 @@
1
+ from message_van.models import Command, Event
2
+ from message_van.models import MessageVan, UnitOfWork
3
+
4
+
5
+ __all__ = [
6
+ "Command",
7
+ "Event",
8
+ "MessageVan",
9
+ "UnitOfWork",
10
+ ]
@@ -14,6 +14,8 @@ from .handler_signature import MessageHandlerSignature
14
14
  from .message_handlers import MessageHandlers
15
15
  from .unit_of_work import UnitOfWork
16
16
 
17
+ from .message_van_meta import MessageVanMeta
18
+
17
19
  from .message_van import MessageVan
18
20
 
19
21
 
@@ -28,5 +30,6 @@ __all__ = [
28
30
  "MessageHandlerSignature",
29
31
  "MessageHandlerType",
30
32
  "MessageVan",
33
+ "MessageVanMeta",
31
34
  "UnitOfWork",
32
35
  ]
@@ -4,7 +4,7 @@ from . import Command
4
4
 
5
5
 
6
6
  if TYPE_CHECKING:
7
- from message_van.domain.models import MessageVan
7
+ from message_van.models import MessageVan
8
8
 
9
9
  message_van_type = MessageVan
10
10
  else:
@@ -4,7 +4,7 @@ from . import Event
4
4
 
5
5
 
6
6
  if TYPE_CHECKING:
7
- from message_van.domain.models import MessageVan
7
+ from message_van.models import MessageVan
8
8
 
9
9
  message_van_type = MessageVan
10
10
  else:
@@ -4,7 +4,7 @@ from . import Message
4
4
 
5
5
 
6
6
  if TYPE_CHECKING:
7
- from message_van.domain.models import MessageVan
7
+ from message_van.models import MessageVan
8
8
 
9
9
  message_van_type = MessageVan
10
10
  else:
@@ -1,7 +1,7 @@
1
1
  from collections import defaultdict
2
2
 
3
- from message_van.domain.exceptions import UnknownHandlerError
4
- from message_van.domain.models import (
3
+ from message_van.exceptions import UnknownHandlerError
4
+ from message_van.models import (
5
5
  Command,
6
6
  CommandHandler,
7
7
  Event,
@@ -62,6 +62,15 @@ class MessageHandlers:
62
62
  def _register_event(self, class_name: str, handler: EventHandler) -> None:
63
63
  self._event_handlers[class_name].append(handler)
64
64
 
65
+ @property
66
+ def __bool__(self) -> None:
67
+ if self._command_handlers:
68
+ return True
69
+ if len(self._event_handlers) > 0:
70
+ return True
71
+
72
+ return False
73
+
65
74
 
66
75
  def _get_message_name(message: Message) -> str:
67
76
  message_class = message.__class__
@@ -2,13 +2,13 @@ from asyncio import create_task, gather
2
2
  from types import TracebackType
3
3
  from typing import Any
4
4
 
5
- from message_van.domain.models import CommandHandler, EventHandler
6
- from message_van.domain.models.base import Command, Event, Message
5
+ from message_van.models import CommandHandler, EventHandler
6
+ from message_van.models.base import Command, Event, Message
7
7
 
8
- from . import UnitOfWork, MessageHandlers
8
+ from . import MessageHandlers, MessageVanMeta, UnitOfWork
9
9
 
10
10
 
11
- class MessageVan:
11
+ class MessageVan(metaclass=MessageVanMeta):
12
12
  uow: UnitOfWork
13
13
 
14
14
  _message_handlers: MessageHandlers
@@ -19,6 +19,8 @@ class MessageVan:
19
19
  async def __aenter__(self) -> "MessageVan":
20
20
  self.uow = await self.unit_of_work.__aenter__()
21
21
 
22
+ await type(self).register_handlers()
23
+
22
24
  return self
23
25
 
24
26
  async def __aexit__(
@@ -53,7 +55,3 @@ class MessageVan:
53
55
 
54
56
  def _get_handlers_for_event(self, event: Event) -> list[EventHandler]:
55
57
  return self._message_handlers.get_handlers_for_event(event)
56
-
57
-
58
- def init_handlers(message_handlers: MessageHandlers) -> None:
59
- MessageVan._message_handlers = message_handlers
@@ -0,0 +1,31 @@
1
+ from abc import ABCMeta
2
+ from inspect import getfile
3
+ from pathlib import Path
4
+
5
+ from message_van.models import MessageHandlers
6
+ from message_van.signatures import list_signatures
7
+ from message_van.util import get_src_dir
8
+
9
+
10
+ class MessageVanMeta(ABCMeta):
11
+ _message_handlers: MessageHandlers
12
+
13
+ def __new__(mcs, name, bases, namespace):
14
+ cls = super().__new__(mcs, name, bases, namespace)
15
+
16
+ cls._message_handlers = None
17
+
18
+ return cls
19
+
20
+ async def register_handlers(cls) -> None:
21
+ if cls._message_handlers is None:
22
+ await cls._register_handlers()
23
+
24
+ async def _register_handlers(cls) -> None:
25
+ cls_file = Path(getfile(cls))
26
+ src_dir = get_src_dir(cls_file)
27
+
28
+ cls._message_handlers = MessageHandlers()
29
+
30
+ for signature in list_signatures(src_dir):
31
+ cls._message_handlers.register(signature)
@@ -1,9 +1,7 @@
1
- from abc import ABC, abstractmethod
2
1
  from types import TracebackType
3
2
 
4
3
 
5
- class UnitOfWork(ABC):
6
- @abstractmethod
4
+ class UnitOfWork:
7
5
  async def __aenter__(self) -> "UnitOfWork":
8
6
  """Enter the Unit of Work, and return its value."""
9
7
 
@@ -0,0 +1,13 @@
1
+ from .function_lister import list_public_functions
2
+ from .module_lister import list_modules
3
+ from .signature_getter import get_signature
4
+
5
+ from .signature_lister import list_signatures
6
+
7
+
8
+ __all__ = [
9
+ "get_signature",
10
+ "list_modules",
11
+ "list_public_functions",
12
+ "list_signatures",
13
+ ]
@@ -0,0 +1,21 @@
1
+ import inspect
2
+ from collections.abc import Generator
3
+ from types import ModuleType
4
+ from typing import Callable
5
+
6
+
7
+ def list_public_functions(module: ModuleType) -> Generator[Callable]:
8
+ for function in list_functions(module):
9
+ if _is_public_function(function):
10
+ yield function
11
+
12
+
13
+ def list_functions(module: ModuleType) -> Generator[Callable]:
14
+ for _, func in inspect.getmembers(module, inspect.isfunction):
15
+ yield func
16
+
17
+
18
+ def _is_public_function(function: Callable) -> bool:
19
+ name = function.__name__
20
+
21
+ return not name.startswith("_")
@@ -0,0 +1,71 @@
1
+ import importlib
2
+ import os
3
+ import sys
4
+ from collections.abc import Generator
5
+ from pathlib import Path
6
+ from types import ModuleType
7
+
8
+
9
+ def list_modules(root_package_path: Path) -> Generator[ModuleType]:
10
+ for module_path in list_module_paths(root_package_path):
11
+ yield import_module(root_package_path, module_path)
12
+
13
+
14
+ def list_module_paths(root_package_path: Path) -> Generator[Path]:
15
+ for file_path in list_file_paths(root_package_path):
16
+ if _is_module(file_path):
17
+ yield file_path
18
+
19
+
20
+ def list_file_paths(root_package_path: Path) -> Generator[Path]:
21
+ for directory, _, file_names in os.walk(root_package_path):
22
+ directory_path = Path(directory)
23
+
24
+ for file_name in file_names:
25
+ yield directory_path / Path(file_name)
26
+
27
+
28
+ def _is_module(path: Path) -> bool:
29
+ file_name = path.name
30
+
31
+ return file_name.endswith(".py")
32
+
33
+
34
+ def import_module(root_package_path: Path, module_path: Path) -> ModuleType:
35
+ if not _root_package_in_path(root_package_path):
36
+ _add_root_package_to_path(root_package_path)
37
+
38
+ return _import_module(root_package_path, module_path)
39
+
40
+
41
+ def _import_module(root_package_path: Path, module_path: Path) -> ModuleType:
42
+ module_name = get_module_name(module_path, root_package_path)
43
+
44
+ return importlib.import_module(module_name)
45
+
46
+
47
+ def _root_package_in_path(root_package_path: Path) -> bool:
48
+ return _get_package_parent_string(root_package_path) in sys.path
49
+
50
+
51
+ def _get_package_parent_string(package_path: Path) -> str:
52
+ parent_path = package_path.parent
53
+
54
+ return str(parent_path)
55
+
56
+
57
+ def _add_root_package_to_path(root_package_path: Path):
58
+ parent_path = root_package_path.parent
59
+ parent_path_string = str(parent_path)
60
+
61
+ _add_to_path(parent_path_string)
62
+
63
+
64
+ def _add_to_path(path_string: str) -> None:
65
+ sys.path.insert(0, path_string)
66
+
67
+
68
+ def get_module_name(module_path: Path, package_root_path: Path):
69
+ relative = module_path.relative_to(package_root_path.parent)
70
+
71
+ return ".".join(relative.with_suffix("").parts)
@@ -0,0 +1,80 @@
1
+ import inspect
2
+ from collections.abc import Generator
3
+ from inspect import Parameter
4
+ from typing import Callable
5
+
6
+ from message_van.models import (
7
+ Command,
8
+ Event,
9
+ Message,
10
+ MessageHandlerType,
11
+ MessageHandlerSignature,
12
+ )
13
+
14
+
15
+ def get_signature(function: Callable) -> MessageHandlerSignature:
16
+ if message_parameter := get_message_parameter(function):
17
+ return _get_signature(
18
+ function=function,
19
+ message_parameter=message_parameter,
20
+ )
21
+
22
+
23
+ def get_message_parameter(func):
24
+ for param in list_parameters(func):
25
+ if is_message_param(param):
26
+ return param
27
+
28
+
29
+ def _get_signature(
30
+ message_parameter: Parameter,
31
+ function: Callable,
32
+ ) -> MessageHandlerSignature:
33
+ class_name = _get_class_name(message_parameter)
34
+ handler_type = _get_handler_type(message_parameter)
35
+
36
+ return MessageHandlerSignature(
37
+ message_handler=function,
38
+ message_class_name=class_name,
39
+ type=handler_type,
40
+ )
41
+
42
+
43
+ def list_parameters(function: Callable) -> Generator[Parameter]:
44
+ signature = inspect.signature(function)
45
+
46
+ yield from signature.parameters.values()
47
+
48
+
49
+ def is_message_param(parameter: Parameter):
50
+ return _inherits_from_message(parameter) and _is_not_base_class(parameter)
51
+
52
+
53
+ def _inherits_from_message(parameter: Parameter) -> bool:
54
+ annotation = parameter.annotation
55
+
56
+ try:
57
+ return issubclass(annotation, Message)
58
+ except TypeError:
59
+ return False
60
+
61
+
62
+ def _is_not_base_class(parameter: Parameter):
63
+ annotation = parameter.annotation
64
+
65
+ return annotation not in (Command, Event)
66
+
67
+
68
+ def _get_class_name(param):
69
+ return param.annotation.__name__
70
+
71
+
72
+ def _get_handler_type(param) -> MessageHandlerType:
73
+ annotation = param.annotation
74
+
75
+ if issubclass(annotation, Command):
76
+ return MessageHandlerType.COMMAND
77
+ elif issubclass(annotation, Event):
78
+ return MessageHandlerType.EVENT
79
+
80
+ raise ValueError(f"Unknown message type: {annotation}")
@@ -0,0 +1,88 @@
1
+ import inspect
2
+ from collections.abc import Generator
3
+ from inspect import Parameter
4
+ from pathlib import Path
5
+ from types import ModuleType
6
+ from typing import Callable
7
+
8
+ from message_van.signatures.function_lister import list_public_functions
9
+ from message_van.signatures.module_lister import list_modules
10
+ from message_van.models import (
11
+ Command,
12
+ Event,
13
+ Message,
14
+ MessageHandlerType,
15
+ MessageHandlerSignature,
16
+ )
17
+
18
+
19
+ def list_signatures(root_package_path: Path):
20
+ for module in list_modules(root_package_path):
21
+ yield from _list_signatures(module)
22
+
23
+
24
+ def _list_signatures(module: ModuleType) -> Generator[MessageHandlerSignature]:
25
+ for public_function in list_public_functions(module):
26
+ if signature := get_signature(public_function):
27
+ yield signature
28
+
29
+
30
+ def get_signature(func) -> MessageHandlerSignature:
31
+ if param := get_message_param(func):
32
+ return _param_to_signature(func=func, param=param)
33
+
34
+
35
+ def _param_to_signature(
36
+ param: Parameter,
37
+ func: Callable,
38
+ ) -> MessageHandlerSignature:
39
+ class_name = get_class_name(param)
40
+ handler_type = get_handler_type(param)
41
+
42
+ return MessageHandlerSignature(
43
+ message_handler=func,
44
+ message_class_name=class_name,
45
+ type=handler_type,
46
+ )
47
+
48
+
49
+ def get_message_param(func):
50
+ for param in list_params(func):
51
+ if is_message_param(param):
52
+ return param
53
+
54
+
55
+ def list_params(func) -> Generator:
56
+ signature = inspect.signature(func)
57
+
58
+ yield from signature.parameters.values()
59
+
60
+
61
+ def is_message_param(param):
62
+ return _inherits_from_message(param) and _is_not_base_class(param)
63
+
64
+
65
+ def _inherits_from_message(param):
66
+ try:
67
+ return issubclass(param.annotation, Message)
68
+ except TypeError:
69
+ return False
70
+
71
+
72
+ def _is_not_base_class(param):
73
+ return param.annotation not in (Command, Event)
74
+
75
+
76
+ def get_class_name(param):
77
+ return param.annotation.__name__
78
+
79
+
80
+ def get_handler_type(param) -> MessageHandlerType:
81
+ annotation = param.annotation
82
+
83
+ if issubclass(annotation, Command):
84
+ return MessageHandlerType.COMMAND
85
+ elif issubclass(annotation, Event):
86
+ return MessageHandlerType.EVENT
87
+
88
+ raise ValueError(f"Unknown message type: {annotation}")
@@ -0,0 +1,6 @@
1
+ from .src_dir import get_src_dir
2
+
3
+
4
+ __all__ = [
5
+ "get_src_dir",
6
+ ]
@@ -0,0 +1,13 @@
1
+ from pathlib import Path
2
+
3
+
4
+ def get_src_dir(start_path: Path) -> Path:
5
+ start_path = start_path.resolve()
6
+
7
+ for parent in [start_path, *start_path.parents]:
8
+ if _is_src_dir(parent):
9
+ return parent
10
+
11
+
12
+ def _is_src_dir(path: Path) -> bool:
13
+ return path.name == "src" or path.parent.name == "site-packages"
@@ -1,19 +0,0 @@
1
- from .domain import entrypoint, sync_entrypoint
2
- from .domain.models import Command, Event
3
- from .domain.models import MessageVan, UnitOfWork
4
- from .domain.models.message_van import init_handlers
5
- from .service_layer import load_handlers
6
-
7
-
8
- message_handlers = load_handlers()
9
- init_handlers(message_handlers)
10
-
11
-
12
- __all__ = [
13
- "Command",
14
- "Event",
15
- "MessageVan",
16
- "UnitOfWork",
17
- "entrypoint",
18
- "sync_entrypoint",
19
- ]
@@ -1,8 +0,0 @@
1
- from .config_adapter import ConfigAdapter
2
- from .signature_adapter import SignatureAdapter
3
-
4
-
5
- __all__ = [
6
- "ConfigAdapter",
7
- "SignatureAdapter",
8
- ]
@@ -1,28 +0,0 @@
1
- from pathlib import Path
2
-
3
- from message_van.adapters.mappers import dict_to_user_config
4
- from message_van.adapters.models import UserConfig
5
- from message_van.adapters.util import get_toml_parser
6
- from message_van.config import USER_CONFIG_KEY
7
-
8
-
9
- class ConfigAdapter:
10
- _parser = get_toml_parser()
11
-
12
- def __init__(self, config_path: Path):
13
- self.config_path = config_path
14
-
15
- def get(self) -> UserConfig:
16
- config_dict = self._get()
17
-
18
- return dict_to_user_config(config_dict)
19
-
20
- def _get(self) -> dict:
21
- pyproject_dict = self._get_pyproject_dict()
22
-
23
- return pyproject_dict["tool"][USER_CONFIG_KEY]
24
-
25
- def _get_pyproject_dict(self) -> dict:
26
- contents = self.config_path.read_text()
27
-
28
- return self._parser.loads(contents)
@@ -1,6 +0,0 @@
1
- from .user_config_mapper import dict_to_user_config
2
-
3
-
4
- __all__ = [
5
- "dict_to_user_config",
6
- ]
@@ -1,5 +0,0 @@
1
- from message_van.adapters.models import UserConfig
2
-
3
-
4
- def dict_to_user_config(config_dict: dict) -> UserConfig:
5
- return UserConfig.model_validate(config_dict)
@@ -1,6 +0,0 @@
1
- from .user_config import UserConfig
2
-
3
-
4
- __all__ = [
5
- "UserConfig",
6
- ]
@@ -1,9 +0,0 @@
1
- from pathlib import Path
2
-
3
- from pydantic import BaseModel, Field
4
-
5
- from message_van.config import HANDLERS_PATH_KEY
6
-
7
-
8
- class UserConfig(BaseModel):
9
- handlers_path: Path = Field(alias=HANDLERS_PATH_KEY)
@@ -1,119 +0,0 @@
1
- import os
2
- import importlib.util
3
- import inspect
4
- from collections.abc import Generator
5
- from pathlib import Path
6
- from typing import Any, List, Callable
7
-
8
-
9
- from message_van.domain.models import (
10
- Command,
11
- Message,
12
- MessageHandlerType,
13
- MessageHandlerSignature,
14
- )
15
-
16
-
17
- class SignatureAdapter:
18
- def __init__(self, path: Path):
19
- self.path = path
20
-
21
- def list(self) -> Generator[MessageHandlerSignature]:
22
- module_paths = self.get_module_paths()
23
- modules = self.import_modules(module_paths)
24
-
25
- for func in list_modules(modules):
26
- if signature := get_signature(func):
27
- yield signature
28
-
29
- def get_module_paths(self) -> List[Path]:
30
- return [module_path for module_path in self.list_module_paths()]
31
-
32
- def list_module_paths(self) -> Generator[Path]:
33
- for root_path, file_name in self.list_file_names():
34
- if _is_module(file_name):
35
- yield root_path / Path(file_name)
36
-
37
- def list_file_names(self) -> Generator[tuple]:
38
- for root, _, file_names in os.walk(self.path):
39
- root_path = Path(root)
40
-
41
- for file_name in file_names:
42
- yield root_path, file_name
43
-
44
- def import_modules(self, module_paths: List[Path]) -> Generator:
45
- for file_path in module_paths:
46
- yield self.import_module(file_path)
47
-
48
- def import_module(self, file_path: str) -> Any:
49
- module_name = os.path.relpath(file_path, self.path).replace(
50
- os.sep, "."
51
- )[:-3]
52
- spec = importlib.util.spec_from_file_location(module_name, file_path)
53
- module = importlib.util.module_from_spec(spec)
54
- spec.loader.exec_module(module)
55
-
56
- return module
57
-
58
-
59
- def _is_module(file_name: str) -> bool:
60
- return file_name.endswith(".py")
61
-
62
-
63
- def list_modules(modules: list) -> Generator[Callable]:
64
- for module in modules:
65
- for func in list_public_functions(module):
66
- yield func
67
-
68
-
69
- def list_public_functions(module) -> Generator[Callable]:
70
- for func in _list_functions(module):
71
- if _is_public_function(func):
72
- yield func
73
-
74
-
75
- def _list_functions(module) -> Generator[Callable]:
76
- for _, func in inspect.getmembers(module, inspect.isfunction):
77
- yield func
78
-
79
-
80
- def _is_public_function(func: Callable) -> bool:
81
- name = func.__name__
82
-
83
- return not name.startswith("_")
84
-
85
-
86
- def get_signature(func) -> MessageHandlerSignature:
87
- if param := get_message_param(func):
88
- return MessageHandlerSignature(
89
- message_class_name=get_class_name(param),
90
- message_handler=func,
91
- type=get_handler_type(param),
92
- )
93
-
94
-
95
- def get_message_param(func):
96
- for param in list_params(func):
97
- if is_message_param(param):
98
- return param
99
-
100
-
101
- def list_params(func) -> Generator:
102
- signature = inspect.signature(func)
103
-
104
- yield from signature.parameters.values()
105
-
106
-
107
- def is_message_param(param):
108
- return issubclass(param.annotation, Message)
109
-
110
-
111
- def get_class_name(param):
112
- return param.annotation.__name__
113
-
114
-
115
- def get_handler_type(param) -> MessageHandlerType:
116
- if issubclass(param.annotation, Command):
117
- return MessageHandlerType.COMMAND
118
-
119
- return MessageHandlerType.EVENT
@@ -1,6 +0,0 @@
1
- from .toml_parser import get_toml_parser
2
-
3
-
4
- __all__ = [
5
- "get_toml_parser",
6
- ]
@@ -1,20 +0,0 @@
1
- import sys
2
- from types import ModuleType
3
-
4
-
5
- MIN_PYTHON_VERSION_FOR_TOMLI = (3, 11)
6
-
7
-
8
- def get_toml_parser() -> ModuleType:
9
- if _is_python_version_compatible_with_tomllib():
10
- import tomllib
11
- else:
12
- import tomli as tomllib
13
-
14
- return tomllib
15
-
16
-
17
- def _is_python_version_compatible_with_tomllib() -> bool:
18
- current_python_version = sys.version_info
19
-
20
- return current_python_version >= MIN_PYTHON_VERSION_FOR_TOMLI
@@ -1,14 +0,0 @@
1
- from asyncclick import group
2
-
3
-
4
- def cli_group(*args, **kwargs):
5
- def decorator(func):
6
- async def wrapped(*func_args, **func_kwargs):
7
- from message_van import MessageVan # noqa: F401
8
-
9
- return await func(*func_args, **func_kwargs)
10
-
11
- return group(*args, **kwargs)(wrapped)
12
-
13
- return decorator
14
-
@@ -1,3 +0,0 @@
1
- USER_CONFIG_KEY = "message-van"
2
-
3
- HANDLERS_PATH_KEY = "handlers-path"
@@ -1,7 +0,0 @@
1
- from .entrypoint_decorator import entrypoint, sync_entrypoint
2
-
3
-
4
- __all__ = [
5
- "entrypoint",
6
- "sync_entrypoint",
7
- ]
@@ -1,25 +0,0 @@
1
- from wrapt import decorator
2
-
3
-
4
- @decorator
5
- async def entrypoint(
6
- wrapped=None,
7
- _=None,
8
- args=None,
9
- kwargs=None,
10
- ):
11
- from message_van import MessageVan # noqa F401
12
-
13
- return await wrapped(*args, **kwargs)
14
-
15
-
16
- @decorator
17
- def sync_entrypoint(
18
- wrapped=None,
19
- _=None,
20
- args=None,
21
- kwargs=None,
22
- ):
23
- from message_van import MessageVan # noqa F401
24
-
25
- return wrapped(*args, **kwargs)
@@ -1,6 +0,0 @@
1
- from .service import load_handlers
2
-
3
-
4
- __all__ = [
5
- "load_handlers",
6
- ]
@@ -1,29 +0,0 @@
1
- from pathlib import Path
2
-
3
-
4
- def load_handlers():
5
- from message_van.adapters import SignatureAdapter
6
- from message_van.domain.models import (
7
- MessageHandlers,
8
- )
9
-
10
- message_handlers = MessageHandlers()
11
-
12
- path = _get_handlers_path()
13
- signatures = SignatureAdapter(path)
14
-
15
- for signature in signatures.list():
16
- message_handlers.register(signature)
17
-
18
- return message_handlers
19
-
20
-
21
- def _get_handlers_path() -> Path:
22
- from message_van.adapters import ConfigAdapter
23
-
24
- pyproject_path = Path("pyproject.toml")
25
- config_adapter = ConfigAdapter(pyproject_path)
26
-
27
- config = config_adapter.get()
28
-
29
- return config.handlers_path
File without changes
File without changes
File without changes