1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import logging as stdlib_logging |
17
|
|
|
|
18
|
|
|
from st2common import log as logging |
19
|
|
|
|
20
|
|
|
__all__ = [ |
21
|
|
|
'get_logger_for_python_runner_action', |
22
|
|
|
'get_action_class_instance' |
23
|
|
|
] |
24
|
|
|
|
25
|
|
|
LOG = logging.getLogger(__name__) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def get_logger_for_python_runner_action(action_name): |
29
|
|
|
""" |
30
|
|
|
Set up a logger which logs all the messages with level DEBUG and above to stderr. |
31
|
|
|
""" |
32
|
|
|
logger_name = 'actions.python.%s' % (action_name) |
33
|
|
|
logger = logging.getLogger(logger_name) |
34
|
|
|
|
35
|
|
|
console = stdlib_logging.StreamHandler() |
36
|
|
|
console.setLevel(stdlib_logging.DEBUG) |
37
|
|
|
|
38
|
|
|
formatter = stdlib_logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') |
39
|
|
|
console.setFormatter(formatter) |
40
|
|
|
logger.addHandler(console) |
41
|
|
|
logger.setLevel(stdlib_logging.DEBUG) |
42
|
|
|
|
43
|
|
|
return logger |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def get_action_class_instance(action_cls, config=None, action_service=None): |
47
|
|
|
""" |
48
|
|
|
Instantiate and return Action class instance. |
49
|
|
|
|
50
|
|
|
:param action_cls: Action class to instantiate. |
51
|
|
|
:type action_cls: ``class`` |
52
|
|
|
|
53
|
|
|
:param config: Config to pass to the action class. |
54
|
|
|
:type config: ``dict`` |
55
|
|
|
|
56
|
|
|
:param action_service: ActionService instance to pass to the class. |
57
|
|
|
:type action_service: :class:`ActionService` |
58
|
|
|
""" |
59
|
|
|
kwargs = {} |
60
|
|
|
kwargs['config'] = config |
61
|
|
|
kwargs['action_service'] = action_service |
62
|
|
|
|
63
|
|
|
# Note: This is done for backward compatibility reasons. We first try to pass |
64
|
|
|
# "action_service" argument to the action class constructor, but if that doesn't work (e.g. old |
65
|
|
|
# action which hasn't been updated yet), we resort to late assignment post class instantiation. |
66
|
|
|
# TODO: Remove in next major version once all the affected actions have been updated. |
67
|
|
|
try: |
68
|
|
|
action_instance = action_cls(**kwargs) |
69
|
|
|
except TypeError as e: |
70
|
|
|
if 'unexpected keyword argument \'action_service\'' not in str(e): |
71
|
|
|
raise e |
72
|
|
|
|
73
|
|
|
LOG.debug('Action class (%s) constructor doesn\'t take "action_service" argument, ' |
74
|
|
|
'falling back to late assignment...' % (action_cls.__class__.__name__)) |
75
|
|
|
|
76
|
|
|
action_service = kwargs.pop('action_service', None) |
77
|
|
|
action_instance = action_cls(**kwargs) |
78
|
|
|
action_instance.action_service = action_service |
79
|
|
|
|
80
|
|
|
return action_instance |
81
|
|
|
|