-
-
Notifications
You must be signed in to change notification settings - Fork 782
Python runner actions datastore access changes, action unit testing fixes and lint cleanup #2511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
032e107
9e16b3e
c76b4fb
ec18a7e
631ec96
c7e1630
b2a5758
0ec9d1f
503ea08
6b3c185
0d420b2
42b5369
60e2d21
e80249a
0af4db1
315ab90
5a9aa59
935ae4a
15ab507
44b36e6
454427e
2078ba3
d1e9107
99452ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from st2tests.base import BaseActionTestCase | ||
|
|
||
| from pythonactions.isprime import PrimeCheckerAction | ||
|
|
||
|
|
||
| class PrimeCheckerActionTestCase(BaseActionTestCase): | ||
| action_cls = PrimeCheckerAction | ||
|
|
||
| def test_run(self): | ||
| action = self.get_action_instance() | ||
| result = action.run(value=1) | ||
| self.assertFalse(result) | ||
|
|
||
| result = action.run(value=3) | ||
| self.assertTrue(result) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,24 +16,58 @@ | |
| import sys | ||
| import json | ||
| import argparse | ||
| import logging as stdlib_logging | ||
|
|
||
| from st2common import log as logging | ||
| from st2actions import config | ||
| from st2actions.runners.pythonrunner import Action | ||
| from st2actions.runners.utils import get_logger_for_python_runner_action | ||
| from st2actions.runners.utils import get_action_class_instance | ||
| from st2common.util import loader as action_loader | ||
| from st2common.util.config_parser import ContentPackConfigParser | ||
| from st2common.constants.action import ACTION_OUTPUT_RESULT_DELIMITER | ||
| from st2common.service_setup import db_setup | ||
| from st2common.services.datastore import DatastoreService | ||
|
|
||
| __all__ = [ | ||
| 'PythonActionWrapper' | ||
| 'PythonActionWrapper', | ||
| 'ActionService' | ||
| ] | ||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ActionService(object): | ||
| """ | ||
| Instance of this class is passed to the action instance and exposes "public" | ||
| methods which can be called by the action. | ||
| """ | ||
|
|
||
| def __init__(self, action_wrapper): | ||
| logger = get_logger_for_python_runner_action(action_name=action_wrapper._class_name) | ||
|
|
||
| self._action_wrapper = action_wrapper | ||
| self._datastore_service = DatastoreService(logger=logger, | ||
| pack_name=self._action_wrapper._pack, | ||
| class_name=self._action_wrapper._class_name, | ||
| api_username='action_service') | ||
|
|
||
| ################################## | ||
| # Methods for datastore management | ||
| ################################## | ||
|
|
||
| def list_values(self, local=True, prefix=None): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just let datastore_service be a pass through and expect actions to access self.datastore_service directly?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way it's consistent with existing sensor service interface.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, not a fan of the pattern but consistency is better in this regard. |
||
| return self._datastore_service.list_values(local, prefix) | ||
|
|
||
| def get_value(self, name, local=True): | ||
| return self._datastore_service.get_value(name, local) | ||
|
|
||
| def set_value(self, name, value, ttl=None, local=True): | ||
| return self._datastore_service.set_value(name, value, ttl, local) | ||
|
|
||
| def delete_value(self, name, local=True): | ||
| return self._datastore_service.delete_value(name, local) | ||
|
|
||
|
|
||
| class PythonActionWrapper(object): | ||
| def __init__(self, pack, file_path, parameters=None, parent_args=None): | ||
| """ | ||
|
|
@@ -92,39 +126,17 @@ def _get_action_instance(self): | |
| if config: | ||
| LOG.info('Using config "%s" for action "%s"' % (config.file_path, | ||
| self._file_path)) | ||
|
|
||
| action_instance = action_cls(config=config.config) | ||
| config = config.config | ||
| else: | ||
| LOG.info('No config found for action "%s"' % (self._file_path)) | ||
| action_instance = action_cls(config={}) | ||
|
|
||
| # Setup action_instance proeprties | ||
| action_instance.logger = self._set_up_logger(action_cls.__name__) | ||
| action_instance.datastore = DatastoreService(logger=action_instance.logger, | ||
| pack_name=self._pack, | ||
| class_name=action_cls.__name__, | ||
| api_username="action_service") | ||
| config = None | ||
|
|
||
| action_service = ActionService(action_wrapper=self) | ||
| action_instance = get_action_class_instance(action_cls=action_cls, | ||
| config=config, | ||
| action_service=action_service) | ||
| return action_instance | ||
|
|
||
| def _set_up_logger(self, action_name): | ||
| """ | ||
| Set up a logger which logs all the messages with level DEBUG | ||
| and above to stderr. | ||
| """ | ||
| logger_name = 'actions.python.%s' % (action_name) | ||
| logger = logging.getLogger(logger_name) | ||
|
|
||
| console = stdlib_logging.StreamHandler() | ||
| console.setLevel(stdlib_logging.DEBUG) | ||
|
|
||
| formatter = stdlib_logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') | ||
| console.setFormatter(formatter) | ||
| logger.addHandler(console) | ||
| logger.setLevel(stdlib_logging.DEBUG) | ||
|
|
||
| return logger | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser(description='Python action runner process wrapper') | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed we added some new Python files there but we didn't lint files there before so some lint violations managed to slip in.