| 1 |
# Copyright 2020 Nokia Software. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from mistral_lib.actions.providers import base |
| 16 |
from mistral_lib.utils import inspect_utils as i_utils |
| 17 |
|
| 18 |
|
| 19 |
class PythonActionDescriptor(base.ActionDescriptorBase): |
| 20 |
def __init__(self, name, action_cls, action_cls_attrs=None, namespace=None, |
| 21 |
project_id=None, scope=None): |
| 22 |
super(PythonActionDescriptor, self).__init__( |
| 23 |
name, |
| 24 |
i_utils.get_docstring(action_cls), |
| 25 |
i_utils.get_arg_list_as_str(action_cls.__init__), |
| 26 |
namespace, |
| 27 |
project_id, |
| 28 |
scope |
| 29 |
) |
| 30 |
|
| 31 |
self._action_cls = action_cls |
| 32 |
self._action_cls_attrs = action_cls_attrs |
| 33 |
|
| 34 |
def __repr__(self): |
| 35 |
return 'Python action [name=%s, cls=%s]' % ( |
| 36 |
self.name, |
| 37 |
self._action_cls |
| 38 |
) |
| 39 |
|
| 40 |
def instantiate(self, params, wf_ctx): |
| 41 |
if not self._action_cls_attrs: |
| 42 |
# No need to create new dynamic type. |
| 43 |
return self._action_cls(**params) |
| 44 |
|
| 45 |
dynamic_cls = type( |
| 46 |
self._action_cls.__name__, |
| 47 |
(self._action_cls,), |
| 48 |
self._action_cls_attrs |
| 49 |
) |
| 50 |
|
| 51 |
return dynamic_cls(**params) |
| 52 |
|
| 53 |
@property |
| 54 |
def action_class(self): |
| 55 |
return self._action_cls |
| 56 |
|
| 57 |
@property |
| 58 |
def action_class_name(self): |
| 59 |
return "{}.{}".format( |
| 60 |
self._action_cls.__module__, |
| 61 |
self._action_cls.__name__ |
| 62 |
) |
| 63 |
|
| 64 |
@property |
| 65 |
def action_class_attributes(self): |
| 66 |
return self._action_cls_attrs |