| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from mistral_lib.actions import base |
| 14 |
|
| 15 |
|
| 16 |
class CompositeActionProvider(base.ActionProvider): |
| 17 |
def __init__(self, name, delegates): |
| 18 |
super().__init__(name) |
| 19 |
|
| 20 |
self._delegates = delegates |
| 21 |
|
| 22 |
def find(self, action_name, namespace=None): |
| 23 |
for d in self._delegates: |
| 24 |
action_desc = d.find(action_name, namespace) |
| 25 |
|
| 26 |
if action_desc is not None: |
| 27 |
return action_desc |
| 28 |
|
| 29 |
return None |
| 30 |
|
| 31 |
def find_all(self, namespace=None, limit=None, sort_fields=None, |
| 32 |
sort_dirs=None, **filters): |
| 33 |
# TODO(rakhmerov): Implement the algorithm that takes ordering/sorting |
| 34 |
# parameters into account correctly. For now, they are just passed to |
| 35 |
# delegates. |
| 36 |
res = [] |
| 37 |
|
| 38 |
for d in self._delegates: |
| 39 |
action_descriptors = d.find_all( |
| 40 |
namespace=namespace, |
| 41 |
limit=limit, |
| 42 |
sort_fields=sort_fields, |
| 43 |
sort_dirs=sort_dirs, |
| 44 |
**filters |
| 45 |
) |
| 46 |
|
| 47 |
if action_descriptors is not None: |
| 48 |
res.extend(action_descriptors) |
| 49 |
|
| 50 |
return res |
| 51 |
|
| 52 |
def add_action_provider(self, action_provider): |
| 53 |
self._delegates.append(action_provider) |