1
1
# coding: utf-8
2
- import asyncio
3
2
import random
4
- from typing import Union , Dict , List , Any , Optional
3
+ from typing import Union , Dict , List , Any , Optional , AsyncGenerator
5
4
6
5
import core .logging .logger_constants as log_const
7
6
from core .basic_models .actions .command import Command
@@ -33,8 +32,9 @@ def __init__(self, items: Optional[Dict[str, Any]] = None, id: Optional[str] = N
33
32
self .version = items .get ("version" , - 1 )
34
33
35
34
async def run (self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
36
- params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> Optional [ List [ Command ] ]:
35
+ params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> AsyncGenerator [ Command , None ]:
37
36
raise NotImplementedError
37
+ yield
38
38
39
39
def on_run_error (self , text_preprocessing_result : BaseTextPreprocessingResult , user : BaseUser ):
40
40
log ("exc_handler: Action failed to run. Return None. MESSAGE: %(masked_message)s." , user ,
@@ -72,11 +72,8 @@ def __init__(self, items: Dict[str, Any], id: Optional[str] = None):
72
72
self .nodes = items .get ("nodes" ) or {}
73
73
74
74
async def run (self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
75
- params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> List [Command ]:
76
- commands = []
77
- commands .append (Command (self .command , self .nodes , self .id , request_type = self .request_type ,
78
- request_data = self .request_data ))
79
- return commands
75
+ params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> AsyncGenerator [Command , None ]:
76
+ yield Command (self .command , self .nodes , self .id , request_type = self .request_type , request_data = self .request_data )
80
77
81
78
82
79
class RequirementAction (Action ):
@@ -105,11 +102,10 @@ def build_internal_item(self) -> str:
105
102
return self ._item
106
103
107
104
async def run (self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
108
- params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> List [Command ]:
109
- commands = []
105
+ params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> AsyncGenerator [Command , None ]:
110
106
if self .requirement .check (text_preprocessing_result , user , params ):
111
- commands . extend ( await self .internal_item .run (user , text_preprocessing_result , params ) or [])
112
- return commands
107
+ async for command in self .internal_item .run (user , text_preprocessing_result , params ):
108
+ yield command
113
109
114
110
115
111
class ChoiceAction (Action ):
@@ -141,18 +137,18 @@ def build_else_item(self) -> Optional[str]:
141
137
return self ._else_item
142
138
143
139
async def run (self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
144
- params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> List [Command ]:
145
- commands = []
140
+ params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> AsyncGenerator [Command , None ]:
146
141
choice_is_made = False
147
142
for item in self .items :
148
143
checked = item .requirement .check (text_preprocessing_result , user , params )
149
144
if checked :
150
- commands .extend (await item .internal_item .run (user , text_preprocessing_result , params ) or [])
145
+ async for command in item .internal_item .run (user , text_preprocessing_result , params ):
146
+ yield command
151
147
choice_is_made = True
152
148
break
153
149
if not choice_is_made and self ._else_item :
154
- commands . extend ( await self .else_item .run (user , text_preprocessing_result , params ) or [])
155
- return commands
150
+ async for command in self .else_item .run (user , text_preprocessing_result , params ):
151
+ yield command
156
152
157
153
158
154
class ElseAction (Action ):
@@ -189,14 +185,16 @@ def build_item(self) -> str:
189
185
def build_else_item (self ) -> Optional [str ]:
190
186
return self ._else_item
191
187
192
- async def run (self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
193
- params : Optional [Optional [Dict [str , Union [str , float , int ]]]] = None ) -> List [Command ]:
194
- commands = []
188
+ async def run (
189
+ self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
190
+ params : Optional [Optional [Dict [str , Union [str , float , int ]]]] = None
191
+ ) -> AsyncGenerator [Command , None ]:
195
192
if self .requirement .check (text_preprocessing_result , user , params ):
196
- commands .extend (await self .item .run (user , text_preprocessing_result , params ) or [])
193
+ async for command in self .item .run (user , text_preprocessing_result , params ):
194
+ yield command
197
195
elif self ._else_item :
198
- commands . extend ( await self .else_item .run (user , text_preprocessing_result , params ) or [])
199
- return commands
196
+ async for command in self .else_item .run (user , text_preprocessing_result , params ):
197
+ yield command
200
198
201
199
202
200
class ActionOfActions (Action ):
@@ -215,11 +213,10 @@ def build_actions(self) -> List[Action]:
215
213
216
214
class CompositeAction (ActionOfActions ):
217
215
async def run (self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
218
- params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> List [Command ]:
219
- commands = []
216
+ params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> AsyncGenerator [Command , None ]:
220
217
for action in self .actions :
221
- commands . extend ( await action .run (user , text_preprocessing_result , params ) or [])
222
- return commands
218
+ async for command in action .run (user , text_preprocessing_result , params ):
219
+ yield command
223
220
224
221
225
222
class NonRepeatingAction (ActionOfActions ):
@@ -231,8 +228,7 @@ def __init__(self, items: Dict[str, Any], id: Optional[str] = None):
231
228
self ._last_action_ids_storage = items ["last_action_ids_storage" ]
232
229
233
230
async def run (self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
234
- params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> List [Command ]:
235
- commands = []
231
+ params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> AsyncGenerator [Command , None ]:
236
232
last_ids = user .last_action_ids [self ._last_action_ids_storage ]
237
233
all_indexes = list (range (self ._actions_count ))
238
234
max_last_ids_count = self ._actions_count - 1
@@ -242,8 +238,8 @@ async def run(self, user: BaseUser, text_preprocessing_result: BaseTextPreproces
242
238
action_index = random .choice (available_indexes )
243
239
action = self .actions [action_index ]
244
240
last_ids .add (action_index )
245
- commands . extend ( await action .run (user , text_preprocessing_result , params ) or [])
246
- return commands
241
+ async for command in action .run (user , text_preprocessing_result , params ):
242
+ yield command
247
243
248
244
249
245
class RandomAction (Action ):
@@ -259,9 +255,8 @@ def build_actions(self) -> List[Action]:
259
255
return self ._raw_actions
260
256
261
257
async def run (self , user : BaseUser , text_preprocessing_result : BaseTextPreprocessingResult ,
262
- params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> List [Command ]:
263
- commands = []
258
+ params : Optional [Dict [str , Union [str , float , int ]]] = None ) -> AsyncGenerator [Command , None ]:
264
259
pos = random .randint (0 , len (self ._raw_actions ) - 1 )
265
260
action = self .actions [pos ]
266
- commands . extend ( await action .run (user , text_preprocessing_result , params = params ) or [])
267
- return commands
261
+ async for command in action .run (user , text_preprocessing_result , params = params ):
262
+ yield command
0 commit comments