29
29
from otopi import util
30
30
from otopimdp import errors
31
31
from otopimdp import constants as c
32
+ from otopimdp import utils
32
33
33
34
34
35
@util .export
@@ -85,7 +86,11 @@ def next_event(self):
85
86
"""
86
87
Returns instance of Event
87
88
"""
88
- line = self .next_line ()
89
+ return self ._next_event ()
90
+
91
+ def _next_event (self , line = None ):
92
+ if not line :
93
+ line = self .next_line ()
89
94
for event_type in c .TRANSLATION :
90
95
match = event_type [c .REGEX_KEY ].match (line )
91
96
if not match :
@@ -96,15 +101,16 @@ def next_event(self):
96
101
(c .ATTRIBUTES_KEY , match .groupdict ()),
97
102
)
98
103
)
99
- self ._process_event (
100
- event_type [c .TYPE_KEY ], event [c .ATTRIBUTES_KEY ],
101
- )
104
+ self ._process_event (event )
102
105
self .logger .debug ("Next event: %s" , event )
103
106
return event
104
107
# W/A for hosted-engine deploy job
105
108
self .logger .warning ("This line doesn't match no event: %s" , line )
106
109
107
- def _process_event (self , event_type , attributes ):
110
+ def _process_event (self , event ):
111
+ event_type = event [c .TYPE_KEY ]
112
+ attributes = event [c .ATTRIBUTES_KEY ]
113
+
108
114
if event_type == c .DISPLAY_VALUE_EVENT :
109
115
type_ = attributes ['type' ].lower ()
110
116
if type_ == 'none' :
@@ -133,6 +139,66 @@ def _process_event(self, event_type, attributes):
133
139
break
134
140
attributes ['value' ] = lines
135
141
142
+ if event_type == c .QUERY_FRAME_EVENT :
143
+ self ._process_frame_event (event )
144
+
145
+ def _process_frame_event (self , event ):
146
+ attributes = event [c .ATTRIBUTES_KEY ]
147
+ framed_event = None
148
+ while True :
149
+ line = self .next_line ()
150
+ m = c .QUERY_FRAME_PATTERNS [c .QUERY_FRAME_PART_END ].match (line )
151
+ if m :
152
+ if m .group (c .FRAME_NAME_KEY ) != attributes [c .FRAME_NAME_KEY ]:
153
+ self .logger .warning (
154
+ "The QStart:NAME != QEnd:NAME (%s != %s)" ,
155
+ m .group (c .FRAME_NAME_KEY ),
156
+ attributes [c .FRAME_NAME_KEY ],
157
+ )
158
+ break
159
+ m = c .QUERY_FRAME_PATTERNS [c .QUERY_FRAME_PART_DEFAULT ].match (line )
160
+ if m :
161
+ attributes [c .DEFAULT_KEY ] = m .group (c .DEFAULT_KEY )
162
+ continue
163
+ m = c .QUERY_FRAME_PATTERNS [c .QUERY_FRAME_PART_HIDDEN ].match (line )
164
+ if m :
165
+ if m .group (c .HIDDEN_KEY ) == "TRUE" :
166
+ attributes [c .HIDDEN_KEY ] = True
167
+ elif m .group (c .HIDDEN_KEY ) == "FALSE" :
168
+ attributes [c .HIDDEN_KEY ] = False
169
+ else :
170
+ self .logger .warning (
171
+ "The QHidden(%s) has invalid option: "
172
+ "%s not in (TRUE, FALSE)" ,
173
+ attributes [c .FRAME_NAME_KEY ],
174
+ m .group (c .HIDDEN_KEY ),
175
+ )
176
+ attributes [c .HIDDEN_KEY ] = False
177
+ continue
178
+ m = c .QUERY_FRAME_PATTERNS [
179
+ c .QUERY_FRAME_PART_VALID_VALUES
180
+ ].match (line )
181
+ if m :
182
+ attributes [c .VALID_VALUES_KEY ] = utils .split_valid_options (
183
+ m .group ('valid' )
184
+ )
185
+ continue
186
+
187
+ framed_event = self ._next_event (line )
188
+ if framed_event is None :
189
+ raise errors .UnexpectedInputError (
190
+ c .QUERY_FRAME_EVENT , attributes , line ,
191
+ )
192
+
193
+ event [c .ATTRIBUTES_KEY ].update (framed_event [c .ATTRIBUTES_KEY ])
194
+ event [c .TYPE_KEY ] = framed_event [c .TYPE_KEY ]
195
+
196
+ if framed_event is None :
197
+ raise errors .IncompleteQueryFrameError (
198
+ "The frame %s doesn't contain query." ,
199
+ event ,
200
+ )
201
+
136
202
def send_response (self , event ):
137
203
"""
138
204
Sends response for replyable events.
@@ -241,7 +307,7 @@ def cli_download_log(self):
241
307
"""
242
308
self ._write ('log' )
243
309
event = self .next_event ()
244
- if event [c .TYPE_KEY ] == c . DISPLAY_MULTI_STRING_EVENT :
310
+ if event [c .TYPE_KEY ] == c .DISPLAY_MULTI_STRING_EVENT :
245
311
return '\n ' .join (event [c .ATTRIBUTES_KEY ]['value' ]) + '\n '
246
312
raise errors .UnexpectedEventError (event )
247
313
0 commit comments