Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from ._action import (
TaskLevel,
WrittenAction,
ACTION_STATUS_FIELD,
STARTED_STATUS,
ACTION_TYPE_FIELD,
)
class Task(PClass):
"""
A tree of actions with the same task UUID.
"""
_nodes = pmap_field(TaskLevel, (WrittenAction, WrittenMessage))
_completed = pset_field(TaskLevel)
_root_level = TaskLevel(level=[])
def root(self):
"""
@return: The root L{WrittenAction}.
"""
return self._nodes[self._root_level]
def is_complete(self):
"""
@return bool: True only if all messages in the task tree have been
added to it.
"""
return self._root_level in self._completed
def _insert_action(self, node):
def task_level(self):
"""
The L{TaskLevel} of this message appears within the task.
"""
return TaskLevel(level=self._logged_dict[TASK_LEVEL_FIELD])
TaskLevel,
WrittenAction,
ACTION_STATUS_FIELD,
STARTED_STATUS,
ACTION_TYPE_FIELD,
)
class Task(PClass):
"""
A tree of actions with the same task UUID.
"""
_nodes = pmap_field(TaskLevel, (WrittenAction, WrittenMessage))
_completed = pset_field(TaskLevel)
_root_level = TaskLevel(level=[])
def root(self):
"""
@return: The root L{WrittenAction}.
"""
return self._nodes[self._root_level]
def is_complete(self):
"""
@return bool: True only if all messages in the task tree have been
added to it.
"""
return self._root_level in self._completed
def _insert_action(self, node):
"""
@param logger: The L{eliot.ILogger} to which to write messages, or
C{None} to use the default one.
@param action_type: The type of this action,
e.g. C{"yourapp:subsystem:dosomething"}.
@param _serializers: Either a L{eliot._validation._ActionSerializers}
instance or C{None}. In the latter case no validation or serialization
will be done for messages generated by the L{Action}.
@param fields: Additional fields to add to the start message.
@return: A new L{Action}.
"""
action = Action(logger, unicode(uuid4()), TaskLevel(level=[]), action_type,
_serializers)
action._start(fields)
return action
def child(self):
"""
Return a child of this L{TaskLevel}.
@return: L{TaskLevel} which is the first child of this one.
"""
return TaskLevel(level=self.level.append(1))
def next_sibling(self):
"""
Return the next L{TaskLevel}, that is a task at the same level as this
one, but one after.
@return: L{TaskLevel} which follows this one.
"""
return TaskLevel(level=self.level.set(-1, self.level[-1] + 1))
from ._message import WrittenMessage, TASK_UUID_FIELD
from ._action import (
TaskLevel,
WrittenAction,
ACTION_STATUS_FIELD,
STARTED_STATUS,
ACTION_TYPE_FIELD,
)
class Task(PClass):
"""
A tree of actions with the same task UUID.
"""
_nodes = pmap_field(TaskLevel, (WrittenAction, WrittenMessage))
_completed = pset_field(TaskLevel)
_root_level = TaskLevel(level=[])
def root(self):
"""
@return: The root L{WrittenAction}.
"""
return self._nodes[self._root_level]
def is_complete(self):
"""
@return bool: True only if all messages in the task tree have been
added to it.
"""
return self._root_level in self._completed
@ivar TaskLevel task_level: The action's task level, e.g. if start
message has level C{[2, 3, 1]} it will be
C{TaskLevel(level=[2, 3])}.
@ivar UUID task_uuid: The UUID of the task to which this action belongs.
@ivar _children: A L{pmap} from L{TaskLevel} to the L{WrittenAction} and
L{WrittenMessage} objects that make up this action.
"""
start_message = field(type=optional(WrittenMessage), mandatory=True,
initial=None)
end_message = field(type=optional(WrittenMessage), mandatory=True,
initial=None)
task_level = field(type=TaskLevel, mandatory=True)
task_uuid = field(type=unicode, mandatory=True, factory=unicode)
# Pyrsistent doesn't support pmap_field with recursive types.
_children = pmap_field(TaskLevel, object)
@classmethod
def from_messages(cls, start_message=None, children=pvector(),
end_message=None):
"""
Create a C{WrittenAction} from C{WrittenMessage}s and other
C{WrittenAction}s.
@param WrittenMessage start_message: A message that has
C{ACTION_STATUS_FIELD}, C{ACTION_TYPE_FIELD}, and a C{task_level}
that ends in C{1}, or C{None} if unavailable.
@param children: An iterable of C{WrittenMessage} and C{WrittenAction}
@param WrittenMessage end_message: A message that has the same
C{action_type} as this action.
@raise WrongTask: If C{end_message} has a C{task_uuid} that differs
@ivar WrittenMessage end_message: An end message hose task UUID and
level match this action. Can be C{None} if the action is
unfinished.
@ivar TaskLevel task_level: The action's task level, e.g. if start
message has level C{[2, 3, 1]} it will be
C{TaskLevel(level=[2, 3])}.
@ivar UUID task_uuid: The UUID of the task to which this action belongs.
@ivar _children: A L{pmap} from L{TaskLevel} to the L{WrittenAction} and
L{WrittenMessage} objects that make up this action.
"""
start_message = field(type=optional(WrittenMessage), mandatory=True,
initial=None)
end_message = field(type=optional(WrittenMessage), mandatory=True,
initial=None)
task_level = field(type=TaskLevel, mandatory=True)
task_uuid = field(type=unicode, mandatory=True, factory=unicode)
# Pyrsistent doesn't support pmap_field with recursive types.
_children = pmap_field(TaskLevel, object)
@classmethod
def from_messages(cls, start_message=None, children=pvector(),
end_message=None):
"""
Create a C{WrittenAction} from C{WrittenMessage}s and other
C{WrittenAction}s.
@param WrittenMessage start_message: A message that has
C{ACTION_STATUS_FIELD}, C{ACTION_TYPE_FIELD}, and a C{task_level}
that ends in C{1}, or C{None} if unavailable.
@param children: An iterable of C{WrittenMessage} and C{WrittenAction}
@param WrittenMessage end_message: A message that has the same
@param action_type: The type of the action,
e.g. C{"yourapp:subsystem:dosomething"}.
@param serializers: Either a L{eliot._validation._ActionSerializers}
instance or C{None}. In the latter case no validation or
serialization will be done for messages generated by the
L{Action}.
"""
self._numberOfMessages = iter(count())
self._successFields = {}
self._logger = logger
if isinstance(task_level, unicode):
warn("Action should be initialized with a TaskLevel",
DeprecationWarning, stacklevel=2)
task_level = TaskLevel.fromString(task_level)
self._task_level = task_level
self._last_child = None
self._identification = {TASK_UUID_FIELD: task_uuid,
ACTION_TYPE_FIELD: action_type,
}
self._serializers = serializers
self._finished = False