reykit 1.0.0__py3-none-any.whl → 1.0.1__py3-none-any.whl

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.
@@ -0,0 +1,65 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2024-07-17 10:49:26
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : Exception methods.
9
+ """
10
+
11
+
12
+ from reykit.rexception import RError, RActiveError
13
+
14
+
15
+ __all__ = (
16
+ 'RWeChatError',
17
+ 'RWeChatExecuteError',
18
+ 'RWeChatExecuteContinueError',
19
+ 'RWeChatExecuteBreakError',
20
+ 'RWeChatExecuteReplyError',
21
+ 'RWeChatExecuteNoRuleReplyError',
22
+ 'RWeChatExecuteTriggerReplyError'
23
+ )
24
+
25
+
26
+ class RWeChatError(RError):
27
+ """
28
+ Rey's `WeChat error` type.
29
+ """
30
+
31
+
32
+ class RWeChatExecuteError(RWeChatError):
33
+ """
34
+ Rey's `WeChat execute error` type.
35
+ """
36
+
37
+
38
+ class RWeChatExecuteContinueError(RActiveError, RWeChatExecuteError):
39
+ """
40
+ Rey's `WeChat execute continue error` type.
41
+ """
42
+
43
+
44
+ class RWeChatExecuteBreakError(RActiveError, RWeChatExecuteError):
45
+ """
46
+ Rey's `WeChat execute break error` type.
47
+ """
48
+
49
+
50
+ class RWeChatExecuteReplyError(RWeChatExecuteError):
51
+ """
52
+ Rey's `WeChat execute reply error` type.
53
+ """
54
+
55
+
56
+ class RWeChatExecuteNoRuleReplyError(RWeChatExecuteReplyError):
57
+ """
58
+ Rey's `WeChat execute no rule reply error` type.
59
+ """
60
+
61
+
62
+ class RWeChatExecuteTriggerReplyError(RWeChatExecuteReplyError):
63
+ """
64
+ Rey's `WeChat execute trigger function reply error` type.
65
+ """
reywechat/rexecute.py ADDED
@@ -0,0 +1,201 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2024-07-16 16:20:34
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : Execute methods.
9
+ """
10
+
11
+
12
+ from typing import Any, TypedDict, Literal, NoReturn
13
+ from collections.abc import Callable
14
+ from reykit.rexception import catch_exc
15
+
16
+ from .rexception import RWeChatExecuteContinueError, RWeChatExecuteBreakError
17
+ from .rreceive import RMessage, RReceive
18
+
19
+
20
+ __all__ = (
21
+ 'RExecute',
22
+ )
23
+
24
+
25
+ Rule = TypedDict('Rule', {'mode': Literal['trigger', 'reply'], 'executer': Callable[[RMessage], None], 'level': float})
26
+
27
+
28
+ class RExecute(object):
29
+ """
30
+ Rey's `execute` type.
31
+ """
32
+
33
+
34
+ def __init__(
35
+ self,
36
+ rreceive: RReceive
37
+ ) -> None:
38
+ """
39
+ Build `execute` attributes.
40
+
41
+ Parameters
42
+ ----------
43
+ rreceive : `RReceive` instance.
44
+ """
45
+
46
+ # Set attribute.
47
+ self.rreceive = rreceive
48
+ self.rules: list[Rule] = []
49
+
50
+ # Add handler.
51
+ self.handler = self._execute_by_rule()
52
+
53
+ # Add executer.
54
+ self._add_execute_valid()
55
+
56
+
57
+ def _execute_by_rule(self) -> Callable[[RMessage], None]:
58
+ """
59
+ Add handler, execute message by rules.
60
+
61
+ Returns
62
+ -------
63
+ Handler.
64
+ """
65
+
66
+
67
+ # Define.
68
+ def handler_execute_by_rule(rmessage: RMessage) -> None:
69
+ """
70
+ Execute message by rules.
71
+
72
+ Parameters
73
+ ----------
74
+ rmessage : `RMessage` instance.
75
+ """
76
+
77
+ # Loop.
78
+ for rule in self.rules:
79
+ rmessage.ruling = rule
80
+
81
+ # Break.
82
+ if (
83
+ rule['mode'] == 'reply'
84
+ and rmessage.replied
85
+ ):
86
+ break
87
+
88
+ # Execute.
89
+ try:
90
+ rule['executer'](rmessage)
91
+
92
+ # Continue.
93
+ except RWeChatExecuteContinueError:
94
+ continue
95
+
96
+ # Break.
97
+ except RWeChatExecuteBreakError:
98
+ break
99
+
100
+ # Exception.
101
+ except:
102
+
103
+ ## Catch exception.
104
+ exc_report, *_ = catch_exc()
105
+
106
+ ## Save.
107
+ rmessage.exc_reports.append(exc_report)
108
+
109
+ finally:
110
+ rmessage.ruling = None
111
+
112
+
113
+ # Add handler.
114
+ self.rreceive.add_handler(handler_execute_by_rule)
115
+
116
+ return handler_execute_by_rule
117
+
118
+
119
+ def add_rule(
120
+ self,
121
+ mode: Literal['trigger', 'reply'],
122
+ executer: Callable[[RMessage], Any],
123
+ level: float = 0
124
+ ) -> None:
125
+ """
126
+ Add execute rule.
127
+
128
+ Parameters
129
+ ----------
130
+ mode : Execute mode.
131
+ executer : Function of execute. The parameter is the `RMessage` instance.
132
+ When throw `RWeChatExecuteContinueError` type exception, then continue next execution.
133
+ When throw `RWeChatExecuteBreakError` type exception, then stop executes.
134
+ level : Priority level, sort from large to small.
135
+ """
136
+
137
+ # Get parameter.
138
+ rule = {
139
+ 'mode': mode,
140
+ 'executer': executer,
141
+ 'level': level
142
+ }
143
+
144
+ # Add.
145
+ self.rules.append(rule)
146
+
147
+ # Sort.
148
+ fund_sort = lambda rule: rule['level']
149
+ self.rules.sort(
150
+ key=fund_sort,
151
+ reverse=True
152
+ )
153
+
154
+
155
+ def continue_(self) -> NoReturn:
156
+ """
157
+ Continue execute by throwing `RWeChatExecuteContinueError` type exception.
158
+ """
159
+
160
+ # Raise.
161
+ raise RWeChatExecuteContinueError
162
+
163
+
164
+ def break_(self) -> NoReturn:
165
+ """
166
+ Break execute by throwing `RWeChatExecuteBreakError` type exception.
167
+ """
168
+
169
+ # Raise.
170
+ raise RWeChatExecuteBreakError
171
+
172
+
173
+ def _add_execute_valid(self) -> None:
174
+ """
175
+ Add executer, execute rule judge valid.
176
+
177
+ Returns
178
+ -------
179
+ Handler.
180
+ """
181
+
182
+
183
+ # Define.
184
+ def execute_valid(rmessage: RMessage) -> None:
185
+ """
186
+ Execute rule judge valid.
187
+
188
+ Parameters
189
+ ----------
190
+ rmessage : `RMessage` instance.
191
+ """
192
+
193
+ # Judge.
194
+ if not rmessage.valid:
195
+
196
+ # Break.
197
+ rmessage.execute_break()
198
+
199
+
200
+ # Add.
201
+ self.add_rule('trigger', execute_valid, float('inf'))
reywechat/rlog.py ADDED
@@ -0,0 +1,198 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2023-10-19 11:33:45
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : Log methods.
9
+ """
10
+
11
+
12
+ from os.path import join as os_join
13
+ from reykit.rlog import RLog as RRLog
14
+
15
+ from .rreceive import RMessage
16
+ from .rsend import RSendParam
17
+ from .rwechat import RWeChat
18
+
19
+
20
+ __all__ = (
21
+ 'RLog',
22
+ )
23
+
24
+
25
+ class RLog(object):
26
+ """
27
+ Rey's `log` type.
28
+ """
29
+
30
+
31
+ def __init__(
32
+ self,
33
+ rwechat: RWeChat
34
+ ) -> None:
35
+ """
36
+ Build `log` attributes.
37
+
38
+ Parameters
39
+ ----------
40
+ rwechat : `RClient` instance.
41
+ """
42
+
43
+ # Set attribute.
44
+ self.rwechat = rwechat
45
+
46
+ # Logger.
47
+ self.rrlog = RRLog('WeChat')
48
+ self.rrlog_print = RRLog('WeChat.WeChatPrint')
49
+ self.rrlog_file = RRLog('WeChat.WeChatFile')
50
+
51
+ # Add handler.
52
+ self._add_handler()
53
+
54
+
55
+ def _add_handler(self) -> None:
56
+ """
57
+ Add log handler.
58
+ """
59
+
60
+ # Set parameter.
61
+ format_ = (
62
+ '%(format_time)s | '
63
+ '%(format_levelname)s | '
64
+ '%(format_message_)s'
65
+ )
66
+
67
+ # Add.
68
+
69
+ ## Reset.
70
+ self.rrlog_print.clear_handler()
71
+
72
+ ## Add handler print.
73
+ self.rrlog_print.add_print(format_=format_)
74
+
75
+ ## Add handler file.
76
+ file_path = os_join(self.rwechat.dir_log, 'WeChat')
77
+ self.rrlog_file.add_file(
78
+ file_path,
79
+ time='m',
80
+ format_=format_
81
+ )
82
+
83
+
84
+ @property
85
+ def print_colour(self) -> bool:
86
+ """
87
+ Whether print colour.
88
+
89
+ Returns
90
+ -------
91
+ Result.
92
+ """
93
+
94
+ # Get parameter.
95
+ result = self.rrlog.print_colour
96
+
97
+ return result
98
+
99
+
100
+ @print_colour.setter
101
+ def print_colour(self, value: bool) -> None:
102
+ """
103
+ Set whether print colour.
104
+
105
+ Parameters
106
+ ----------
107
+ value : Set value.
108
+ """
109
+
110
+ # Set.
111
+ self.rrlog.print_colour = value
112
+ self.rrlog_print.print_colour = value
113
+ self.rrlog_file.print_colour = value
114
+
115
+
116
+ def log_receive(
117
+ self,
118
+ rmessage: RMessage
119
+ ) -> None:
120
+ """
121
+ Log receive message.
122
+
123
+ Parameters
124
+ ----------
125
+ rmessage : `RMessage` instance.
126
+ """
127
+
128
+ # Generate record.
129
+ if rmessage.room is None:
130
+ message_object = rmessage.user
131
+ else:
132
+ message_object = rmessage.room
133
+ content_print = 'RECEIVE | %-20s' % message_object
134
+ content_file = 'RECEIVE | %s' % rmessage.params
135
+ if rmessage.exc_reports == []:
136
+ level = self.rrlog.INFO
137
+ else:
138
+ level = self.rrlog.ERROR
139
+ exc_report = '\n'.join(rmessage.exc_reports)
140
+ content_print = '%s\n%s' % (content_print, exc_report)
141
+ content_file = '%s\n%s' % (content_file, exc_report)
142
+
143
+ ## Add color.
144
+ if self.rrlog.print_colour:
145
+ color_code = self.rrlog.get_level_color_ansi(level)
146
+ content_print = f'{color_code}{content_print}\033[0m'
147
+
148
+ # Log.
149
+ self.rrlog_print.log(
150
+ format_message_=content_print,
151
+ level=level
152
+ )
153
+ self.rrlog_file.log(
154
+ format_message_=content_file,
155
+ level=level
156
+ )
157
+
158
+
159
+ def log_send(
160
+ self,
161
+ rsparam: RSendParam
162
+ ) -> None:
163
+ """
164
+ Log send message.
165
+
166
+ Parameters
167
+ ----------
168
+ rsparam : `RSendParams` instance.
169
+ """
170
+
171
+ # Generate record.
172
+ content_print = 'SEND | %-20s' % rsparam.receive_id
173
+ content_file = 'SEND | %s' % {
174
+ 'receive_id': rsparam.receive_id,
175
+ **rsparam.params
176
+ }
177
+ if rsparam.exc_reports == []:
178
+ level = self.rrlog.INFO
179
+ else:
180
+ level = self.rrlog.ERROR
181
+ exc_report = '\n'.join(rsparam.exc_reports)
182
+ content_print = '%s\n%s' % (content_print, exc_report)
183
+ content_file = '%s\n%s' % (content_file, exc_report)
184
+
185
+ ## Add color.
186
+ if self.rrlog.print_colour:
187
+ color_code = self.rrlog.get_level_color_ansi(level)
188
+ content_print = f'{color_code}{content_print}\033[0m'
189
+
190
+ # Log.
191
+ self.rrlog_print.log(
192
+ format_message_=content_print,
193
+ level=level
194
+ )
195
+ self.rrlog_file.log(
196
+ format_message_=content_file,
197
+ level=level
198
+ )