Skip to content

Commit d38c92d

Browse files
committed
feat: refactor response module and add new response handling capabilities
1 parent 028229e commit d38c92d

File tree

4 files changed

+82
-48
lines changed

4 files changed

+82
-48
lines changed

reply_module/abstract_reply.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

reply_module/reply_factory.py

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from abc import ABC, abstractmethod
2+
3+
class AbstractResponse(ABC):
4+
@abstractmethod
5+
def __init__(self, config):
6+
self.config = config
7+
8+
9+
class AbstractResponseMessage(AbstractResponse):
10+
@abstractmethod
11+
def __init__(self, config):
12+
super().__init__(config)
13+
14+
@abstractmethod
15+
def send(self, message):
16+
pass
17+
18+
19+
class AbstractResponseOther(AbstractResponse):
20+
@abstractmethod
21+
def __init__(self, config):
22+
super().__init__(config)
23+
24+
@abstractmethod
25+
def set_state(self, *args, **kwargs):
26+
pass
27+
28+
@abstractmethod
29+
def send(self, *args, **kwargs):
30+
pass
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from response_module.abstract_response import AbstractResponse, AbstractResponseMessage
2+
from response_module.response_target.msg_response.dingtalk_response import DingtalkResponse
3+
from response_module.response_target.msg_response.gitlab_response import GitlabResponse
4+
5+
6+
class ResponseFactory:
7+
_registry_msg = {}
8+
_registry_other = {}
9+
10+
@classmethod
11+
def register_target(cls, target, target_class):
12+
# 检测是否实现了AbstractResponseMessage接口
13+
if not issubclass(target_class, AbstractResponse):
14+
raise TypeError(f'{target_class} does not implement AbstractResponse')
15+
if not issubclass(target_class, AbstractResponseMessage):
16+
cls._registry_other[target] = target_class
17+
cls._registry_msg[target] = target_class
18+
19+
@classmethod
20+
def get_message_instance(cls, target, config):
21+
if target not in cls._registry_msg:
22+
return None
23+
return cls._registry_msg[target](config)
24+
25+
@classmethod
26+
def get_other_instance(cls, target, config):
27+
if target not in cls._registry_other:
28+
return None
29+
return cls._registry_other[target](config)
30+
31+
@classmethod
32+
def get_all_message_instance(cls, config):
33+
return [target_class(config) for target_class in cls._registry_msg.values()]
34+
35+
@classmethod
36+
def get_all_other_instance(cls, *args, **kwargs):
37+
return [target_class(*args, **kwargs) for target_class in cls._registry_other.values()]
38+
39+
@classmethod
40+
def get_all_message_targets(cls):
41+
return list(cls._registry_msg.keys())
42+
43+
@classmethod
44+
def get_all_other_targets(cls):
45+
return list(cls._registry_other.keys())
46+
47+
48+
ResponseFactory.register_target('gitlab', GitlabResponse)
49+
ResponseFactory.register_target('dingtalk', DingtalkResponse)
50+
# ResponseFactory.register_target('temp', TemplateResponse)
51+
if __name__ == '__main__':
52+
print(ResponseFactory.get_all_message_targets())

0 commit comments

Comments
 (0)