-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
128 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
# coding=utf-8 | ||
|
||
from .bean_registry import * | ||
from .definition import * | ||
from .definition_registry import * | ||
from .errors import * | ||
from .factory import * | ||
from .factory_post_processor import * | ||
from .name_generator import * | ||
from .post_processor import * | ||
from .registry import * | ||
|
||
__all__ = (bean_registry.__all__ + | ||
definition.__all__ + | ||
__all__ = (definition.__all__ + | ||
definition_registry.__all__ + | ||
errors.__all__ + | ||
factory.__all__ + | ||
factory_post_processor.__all__ + | ||
name_generator.__all__ + | ||
post_processor.__all__) | ||
post_processor.__all__ + | ||
registry.__all__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
# coding=utf-8 | ||
|
||
from .annotation import * | ||
from .annotation_config_constants import * | ||
from .annotation_config_registry import * | ||
from .annotation_config_utils import * | ||
from .bean_context import * | ||
from .bean_name_generator import * | ||
from .config_constants import * | ||
from .config_post_processor import * | ||
from .event import * | ||
from .event_listener import * | ||
from .event_publisher import * | ||
|
||
__all__ = (annotation.__all__ + | ||
annotation_config_constants.__all__ + | ||
annotation_config_registry.__all__ + | ||
annotation_config_utils.__all__ + | ||
bean_context.__all__ + | ||
bean_name_generator.__all__ + | ||
config_post_processor.__all__) | ||
config_constants.__all__ + | ||
config_post_processor.__all__ + | ||
event.__all__ + | ||
event_listener.__all__ + | ||
event_publisher.__all__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
...sk/context/annotation_config_constants.py → guniflask/context/config_constants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
# coding=utf-8 | ||
|
||
__all__ = ['CONFIGURATION_ANNOTATION_PROCESSOR', | ||
__all__ = ['APPLICATION_EVENT_PUBLISHER', | ||
'CONFIGURATION_ANNOTATION_PROCESSOR', | ||
'CONFIGURATION_BEAN_NAME_GENERATOR'] | ||
|
||
APPLICATION_EVENT_PUBLISHER = '__application_event_publisher' | ||
|
||
CONFIGURATION_ANNOTATION_PROCESSOR = '__configuration_annotation_processor' | ||
|
||
CONFIGURATION_BEAN_NAME_GENERATOR = '__configuration_bean_name_generator' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# coding=utf-8 | ||
|
||
__all__ = ['ApplicationEvent', 'ContextRefreshedEvent'] | ||
|
||
|
||
class ApplicationEvent: | ||
def __init__(self, source): | ||
self.source = source | ||
|
||
|
||
class ContextRefreshedEvent(ApplicationEvent): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# coding=utf-8 | ||
|
||
from abc import ABCMeta, abstractmethod | ||
|
||
from guniflask.context.event import ApplicationEvent | ||
|
||
__all__ = ['ApplicationEventListener'] | ||
|
||
|
||
class ApplicationEventListener(metaclass=ABCMeta): | ||
|
||
@abstractmethod | ||
def on_application_event(self, application_event: ApplicationEvent): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# coding=utf-8 | ||
|
||
import inspect | ||
|
||
from guniflask.beans.factory import BeanFactory | ||
from guniflask.context.event import ApplicationEvent | ||
from guniflask.context.event_listener import ApplicationEventListener | ||
|
||
__all__ = ['ApplicationEventPublisher'] | ||
|
||
|
||
class ApplicationEventPublisher: | ||
def __init__(self, bean_factory: BeanFactory): | ||
self._bean_factory = bean_factory | ||
self._app_listeners = set() | ||
self._app_listener_beans = set() | ||
|
||
def add_application_listener(self, listener: ApplicationEventListener): | ||
self._app_listeners.add(listener) | ||
|
||
def add_application_listener_bean(self, bean_name: str): | ||
self._app_listener_beans.add(bean_name) | ||
|
||
def publish_event(self, event: ApplicationEvent): | ||
for listener in self._get_application_listeners(): | ||
assert isinstance(listener, ApplicationEventListener) | ||
accepted_event_type = self._resolve_accepted_event_type(listener.on_application_event) | ||
if accepted_event_type is None or isinstance(event, accepted_event_type): | ||
listener.on_application_event(event) | ||
|
||
def _get_application_listeners(self): | ||
for listener in self._app_listeners: | ||
yield listener | ||
for bean_name in self._app_listener_beans: | ||
listener = self._bean_factory.get_bean(bean_name, required_type=ApplicationEventListener) | ||
if listener is not None: | ||
yield listener | ||
|
||
def _resolve_accepted_event_type(self, method) -> type: | ||
args_spec = inspect.getfullargspec(method) | ||
args = args_spec.args | ||
event_type_arg = None | ||
if len(args) > 0: | ||
if args[0] in ('self', 'cls'): | ||
if len(args) > 1: | ||
event_type_arg = args[1] | ||
else: | ||
event_type_arg = args[0] | ||
if event_type_arg is not None: | ||
hints = args_spec.annotations | ||
return hints.get(event_type_arg) |