forked from GoogleCloudPlatform/dfcx-scrapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builders_common.py
351 lines (290 loc) · 11.1 KB
/
builders_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""A set of builder methods to create CX proto resource objects"""
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import List, Union
from google.cloud.dialogflowcx_v3beta1.types import TransitionRoute
from google.cloud.dialogflowcx_v3beta1.types import EventHandler
# logging config
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
class BuildersCommon:
"""Base class for other Builder classes"""
_proto_type = None
_proto_type_str = "None"
def __init__(self, obj=None):
self.proto_obj = None
if obj:
self.load_proto_obj(obj)
def _check_proto_obj_attr_exist(self):
"""Check if the proto_obj exists otherwise raise an error."""
if self.proto_obj is None:
raise ValueError(
"There is no proto_obj!"
"\nUse `create_new_proto_obj` or `load_proto_obj` to continue."
)
elif not isinstance(self.proto_obj, self._proto_type): # pylint: disable=W1116
raise ValueError(
f"proto_obj is not {self._proto_type_str} type."
"\nPlease create or load the correct type to continue."
)
def load_proto_obj(self, obj, overwrite: bool = False):
"""Load an existing object to proto_obj for further uses.
Args:
obj (proto object):
An existing proto object.
overwrite (bool)
Overwrite the new proto_obj if proto_obj already
contains some object.
Returns:
An object stored in proto_obj
"""
if not isinstance(obj, self._proto_type): # pylint: disable=W1116
raise ValueError(
"The object you're trying to load"
f" is not {self._proto_type_str}!"
)
if self.proto_obj and not overwrite:
raise UserWarning(
f"proto_obj already contains {self._proto_type_str}."
" If you wish to overwrite it, pass overwrite as True."
)
if overwrite or not self.proto_obj:
self.proto_obj = obj
return self.proto_obj
def _is_type_or_list_of_types(self, obj, type_, var_name: str = None):
"""Check whether the `obj` type is `type_` or
is a list with elements of `type_` otherwise raise an error.
Args:
obj:
The object to check
type_:
Type of `obj`
var_name (str):
The variable name to show in the error message.
Raises:
ValueError: If the `obj` type is not `type_` or a list of `type_`s.
"""
default_error_msg = "Incorrect type!!"
error_msg_map = {
str: (
f"`{var_name}` should be either a string or a list of strings."
),
EventHandler: (
f"`{var_name}` should be either a EventHandler"
" or a list of EventHandlers."
),
TransitionRoute: (
f"`{var_name}` should be either a TransitionRoute"
" or a list of TransitionRoutes."
),
}
if not(
isinstance(obj, type_) or
(isinstance(obj, list) and
all(isinstance(item, type_) for item in obj))
):
msg = error_msg_map.get(obj, default_error_msg)
raise ValueError(msg)
def _match_transition_route(
self,
transition_route: TransitionRoute,
target_route: TransitionRoute = None,
intent: str = None,
condition: str = None
) -> bool:
"""Check if transition_route's intent and condition
matches with the input.
At least one of the `target_route`, `intent`, or `condition` should
be specfied.
Args:
transition_route (TransitionRoute):
The TransitionRoute that input should match with.
taget_route (TransitionRoute):
The target TransitionRoute that we want to match.
intent (str):
TransitionRoute's intent that we want to match.
condition (str):
TransitionRoute's condition that we want to match.
Returns:
True if the `transition_route` matched with the input.
"""
# Type/Error checking
if not isinstance(transition_route, TransitionRoute):
raise ValueError(
"`transition_route` should have the type TransitionRoute."
)
if not(target_route or intent or condition):
raise ValueError(
"At least one of `target_route`, `intent`, or `condition`"
" must be specified."
)
# Check if the transition route matches
is_match = False
if target_route:
is_match = self._check_transition_route_with_target_route(
transition_route, target_route
)
if intent and condition:
is_match = self._check_transition_route_with_intent_and_condition(
transition_route, intent, condition
)
elif intent and not condition:
is_match = self._check_transition_route_with_intent(
transition_route, intent
)
elif not intent and condition:
is_match = self._check_transition_route_with_condition(
transition_route, condition
)
return is_match
def _check_transition_route_with_target_route(
self,
transition_route: TransitionRoute,
target_route: TransitionRoute
) -> bool:
"""Check if transition_route's intent and condition
matches with the target_route's intent and condition.
Args:
transition_route (TransitionRoute):
The TransitionRoute that input should match with.
taget_route (TransitionRoute):
The target TransitionRoute that we want to match.
Returns:
True if the `transition_route` matched with the input.
"""
# Type/Error checking
if not isinstance(target_route, TransitionRoute):
raise ValueError("`target_route` should be a TransitionRoute.")
if (
transition_route.condition == target_route.condition and
transition_route.intent == target_route.intent
):
return True
return False
def _check_transition_route_with_intent_and_condition(
self,
transition_route: TransitionRoute,
intent: str,
condition: str
) -> bool:
"""Check if transition_route's intent and condition
matches with the input.
Args:
transition_route (TransitionRoute):
The TransitionRoute that input should match with.
intent (str):
TransitionRoute's intent that we want to match.
condition (str):
TransitionRoute's condition that we want to match.
Returns:
True if the `transition_route` matched with the input.
"""
# Type/Error checking
if not(isinstance(intent, str) and isinstance(condition, str)):
raise ValueError("`intent` and `condition` should be a string.")
if (
transition_route.condition == condition and
transition_route.intent == intent
):
return True
return False
def _check_transition_route_with_intent(
self,
transition_route: TransitionRoute,
intent: str
) -> bool:
"""Check if transition_route's intent matches with the input.
Args:
transition_route (TransitionRoute):
The TransitionRoute that input should match with.
intent (str):
TransitionRoute's intent that we want to match.
Returns:
True if the `transition_route` matched with the input.
"""
# Type/Error checking
if not isinstance(intent, str):
raise ValueError("`intent` should be a string.")
if transition_route.intent == intent:
return True
return False
def _check_transition_route_with_condition(
self,
transition_route: TransitionRoute,
condition: str
) -> bool:
"""Check if transition_route's condition matches with the input.
Args:
transition_route (TransitionRoute):
The TransitionRoute that input should match with.
condition (str):
TransitionRoute's condition that we want to match.
Returns:
True if the `transition_route` matched with the input.
"""
# Type/Error checking
if not isinstance(condition, str):
raise ValueError("`condition` should be a string.")
if transition_route.condition == condition:
return True
return False
def _find_unmatched_event_handlers(
self, event_handlers: Union[EventHandler, List[EventHandler]]
) -> List[EventHandler]:
"""Find the EventHandlers of proto_obj which is not present
in the `event_handlers`
Args:
event_handlers (EventHandler | List[EventHandler]):
A single or list of EventHandler to remove
from the existing EventHandlers in proto_obj.
Returns:
A list of EventHandlers
"""
# Type error checking
self._is_type_or_list_of_types(
event_handlers, EventHandler, "event_handlers"
)
if not isinstance(event_handlers, list):
event_handlers = [event_handlers]
return [
eh
for eh in self.proto_obj.event_handlers
if eh not in event_handlers
]
def _find_unmatched_event_handlers_by_name(
self, event_names: Union[str, List[str]]
) -> List[EventHandler]:
"""Find the EventHandlers of proto_obj which their event names
is not present in the `event_names`
Args:
event_names (str | List[str]):
A single or list of EventHandler's event names corresponding
to the EventHandler to remove from the existing
EventHandlers in proto_obj.
Returns:
A list of EventHandlers
"""
# Type error checking
self._is_type_or_list_of_types(event_names, str, "event_names")
if not isinstance(event_names, list):
event_names = [event_names]
return [
eh
for eh in self.proto_obj.event_handlers
if eh.event not in event_names
]