pylinting
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/b595f57c Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/b595f57c Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/b595f57c Branch: refs/heads/ARIA-9-API-for-operation-context Commit: b595f57cd2aa8bde2a19667283614a09efd273c5 Parents: 09830f3 Author: mxmrlv <[email protected]> Authored: Sun Nov 13 19:09:18 2016 +0200 Committer: mxmrlv <[email protected]> Committed: Sun Nov 13 19:09:18 2016 +0200 ---------------------------------------------------------------------- aria/context/common.py | 14 ++++++++-- aria/context/operation.py | 44 ++++++++++++++++++++++++++++++-- aria/context/toolbelt.py | 41 ++++++++++++++++++++++++----- aria/decorators.py | 4 +-- aria/storage/models.py | 8 ++---- aria/workflows/core/task.py | 4 +++ aria/workflows/executor/__init__.py | 7 ++++- tests/context/test_operation.py | 11 +++++--- tests/context/test_toolbelt.py | 2 +- tests/workflows/core/test_task.py | 6 ++++- 10 files changed, 117 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/aria/context/common.py ---------------------------------------------------------------------- diff --git a/aria/context/common.py b/aria/context/common.py index 9ba3e41..dcd55c8 100644 --- a/aria/context/common.py +++ b/aria/context/common.py @@ -12,7 +12,9 @@ # 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. - +""" +A common context for both workflow and operation +""" from uuid import uuid4 from .. import logger @@ -21,7 +23,7 @@ from ..tools.lru_cache import lru_cache class BaseContext(logger.LoggerMixin): """ - Context object used during workflow creation and execution + Base context object for workflow and operation """ def __init__( @@ -58,10 +60,18 @@ class BaseContext(logger.LoggerMixin): @property def model(self): + """ + Access to the model storage + :return: + """ return self._model @property def resource(self): + """ + Access to the resource storage + :return: + """ return self._resource @property http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/aria/context/operation.py ---------------------------------------------------------------------- diff --git a/aria/context/operation.py b/aria/context/operation.py index 9ffcab1..84d0352 100644 --- a/aria/context/operation.py +++ b/aria/context/operation.py @@ -48,42 +48,82 @@ class BaseOperationContext(BaseContext): @property def task(self): + """ + The task in the model storage + :return: Task model + """ return self._task_model class NodeOperationContext(BaseOperationContext): - + """ + Context for node based operations. + """ @property def node(self): + """ + the node of the current operation + :return: + """ return self._operation_executor.node @property def node_instance(self): + """ + The node instance of the current operation + :return: + """ return self._operation_executor class RelationshipOperationContext(BaseOperationContext): - + """ + Context for relationship based operations. + """ @property def node(self): + """ + The source node + :return: + """ return self.model.node.get(self.relationship.source_id) @property def node_instance(self): + """ + The source node instance + :return: + """ return self.model.node_instance.get(self.relationship_instance.source_id) @property def target_node(self): + """ + The target node + :return: + """ return self.model.node.get(self.relationship.target_id) @property def target_node_instance(self): + """ + The target node instance + :return: + """ return self.model.node_instance.get(self._operation_executor.target_id) @property def relationship(self): + """ + The relationship of the current operation + :return: + """ return self._operation_executor.relationship @property def relationship_instance(self): + """ + The relationship instance of the current operation + :return: + """ return self._operation_executor http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/aria/context/toolbelt.py ---------------------------------------------------------------------- diff --git a/aria/context/toolbelt.py b/aria/context/toolbelt.py index 0ab1b7a..3bd353a 100644 --- a/aria/context/toolbelt.py +++ b/aria/context/toolbelt.py @@ -12,20 +12,31 @@ # 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. +""" +provides with different tools when working with the operation. +""" + from contextlib import contextmanager from .. import exceptions from . import operation -class _Toolbelt(object): - +class _BaseToolbelt(object): + """ + Base tool belt + """ def __init__(self): self._op_context = None self._workflow_context = None @contextmanager def use(self, operation_context): + """ + Context manager which switches the current toolbelt with the supplied ctx + :param operation_context: + :return: + """ assert isinstance(operation_context, operation.BaseOperationContext) _op_context = self._op_context _workflow_context = self._workflow_context @@ -39,10 +50,16 @@ class _Toolbelt(object): self._workflow_context = _workflow_context -class _OperationToolBelt(_Toolbelt): - +class _NodeToolBelt(_BaseToolbelt): + """ + Node operation related tool belt + """ @property def relationships_to_me(self): + """ + Any relationship which the current node is the target of + :return: + """ assert isinstance(self._op_context, operation.NodeOperationContext) for node_instance in self._workflow_context.node_instances: for relationship_instance in node_instance.relationship_instances: @@ -51,20 +68,32 @@ class _OperationToolBelt(_Toolbelt): @property def host_ip(self): + """ + The host ip of the current node + :return: + """ assert isinstance(self._op_context, operation.NodeOperationContext) host_id = self._op_context._operation_executor.host_id host_instance = self._workflow_context.model.node_instance.get(host_id) return host_instance.runtime_properties.get('ip') -class _RelationshipToolBelt(_Toolbelt): +class _RelationshipToolBelt(_BaseToolbelt): + """ + Relationship operation related tool belt + """ pass -_operation_toolbelt = _OperationToolBelt() +_operation_toolbelt = _NodeToolBelt() _relationship_toolbelt = _RelationshipToolBelt() def toolbelt(operation_context): + """ + Get a toolbelt according to the current operation executor + :param operation_context: + :return: + """ if isinstance(operation_context, operation.NodeOperationContext): return _operation_toolbelt.use(operation_context) elif isinstance(operation_context, operation.RelationshipOperationContext): http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/aria/decorators.py ---------------------------------------------------------------------- diff --git a/aria/decorators.py b/aria/decorators.py index 219f3c4..be5e1e3 100644 --- a/aria/decorators.py +++ b/aria/decorators.py @@ -59,9 +59,9 @@ def operation(func=None, toolbelt=False, suffix_template=''): @wraps(func) def _wrapper(**func_kwargs): - with context.toolbelt(func_kwargs.get('ctx')) as tb: + with context.toolbelt(func_kwargs.get('ctx')) as operation_toolbelt: if toolbelt: - func_kwargs.setdefault('toolbelt', tb) + func_kwargs.setdefault('toolbelt', operation_toolbelt) validate_function_arguments(func, func_kwargs) return func(**func_kwargs) return _wrapper http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/aria/storage/models.py ---------------------------------------------------------------------- diff --git a/aria/storage/models.py b/aria/storage/models.py index 7fba44a..9bcde26 100644 --- a/aria/storage/models.py +++ b/aria/storage/models.py @@ -264,11 +264,7 @@ class Node(Model): # todo: maybe add here Exception if isn't exists (didn't yield one's) -class Instance(Model): - id = Field(type=basestring, default=uuid_generator) - - -class RelationshipInstance(Instance): +class RelationshipInstance(Model): """ A Model which represents a relationship instance """ @@ -281,7 +277,7 @@ class RelationshipInstance(Instance): relationship = PointerField(type=Relationship) -class NodeInstance(Instance): +class NodeInstance(Model): """ A Model which represents a node instance """ http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/aria/workflows/core/task.py ---------------------------------------------------------------------- diff --git a/aria/workflows/core/task.py b/aria/workflows/core/task.py index a87b089..e4121b9 100644 --- a/aria/workflows/core/task.py +++ b/aria/workflows/core/task.py @@ -168,6 +168,10 @@ class OperationTask(BaseTask): @property def context(self): + """ + Contexts for the operation + :return: + """ return self._ctx @property http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/aria/workflows/executor/__init__.py ---------------------------------------------------------------------- diff --git a/aria/workflows/executor/__init__.py b/aria/workflows/executor/__init__.py index 352b33f..09fb12c 100644 --- a/aria/workflows/executor/__init__.py +++ b/aria/workflows/executor/__init__.py @@ -13,4 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import blocking, celery, multiprocess, thread \ No newline at end of file +""" +Executors for task execution +""" + + +from . import blocking, celery, multiprocess, thread http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/tests/context/test_operation.py ---------------------------------------------------------------------- diff --git a/tests/context/test_operation.py b/tests/context/test_operation.py index a1fd2e0..70c564c 100644 --- a/tests/context/test_operation.py +++ b/tests/context/test_operation.py @@ -28,9 +28,12 @@ def test_node_operation_task_execution(workflow_context, executor): workflow_context.model.node.store(node) workflow_context.model.node_instance.store(node_instance) - node_instance = workflow_context.model.node_instance.get(mock.models.DEPENDENCY_NODE_INSTANCE_ID) + node_instance = \ + workflow_context.model.node_instance.get(mock.models.DEPENDENCY_NODE_INSTANCE_ID) name = 'op_name' - operation_details = {'operation': op_path(my_operation, module_path=sys.modules[__name__].__name__)} + operation_details = { + 'operation': op_path(my_operation, module_path=sys.modules[__name__].__name__) + } inputs = {'putput': True} @workflow @@ -84,7 +87,9 @@ def test_relationship_operation_task_execution(workflow_context, executor): workflow_context.model.node_instance.store(dependent_node_instance) name = 'op_name' - operation_details = {'operation': op_path(my_operation, module_path=sys.modules[__name__].__name__)} + operation_details = { + 'operation': op_path(my_operation, module_path=sys.modules[__name__].__name__) + } inputs = {'putput': True} @workflow http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/tests/context/test_toolbelt.py ---------------------------------------------------------------------- diff --git a/tests/context/test_toolbelt.py b/tests/context/test_toolbelt.py index c9340d2..0b2c3c6 100644 --- a/tests/context/test_toolbelt.py +++ b/tests/context/test_toolbelt.py @@ -19,7 +19,7 @@ import pytest from aria.workflows import api from aria import workflow, operation, context, exceptions -from aria.context.toolbelt import _RelationshipToolBelt, _OperationToolBelt +from aria.context.toolbelt import _RelationshipToolBelt, _NodeToolBelt from .. import mock from . import ( http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b595f57c/tests/workflows/core/test_task.py ---------------------------------------------------------------------- diff --git a/tests/workflows/core/test_task.py b/tests/workflows/core/test_task.py index 5691e4c..2945e74 100644 --- a/tests/workflows/core/test_task.py +++ b/tests/workflows/core/test_task.py @@ -12,8 +12,12 @@ # 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. +from datetime import ( + datetime, + timedelta +) + import pytest -from datetime import datetime, timedelta from aria.context import workflow as workflow_context from aria.workflows import (
