Example #1
0
def format_node(format_value, theme, node):
    """
    Format a node for display purposes.

    Different representations exist for the various types of node:
        - `eliot.parse.Task`: A task UUID.
        - `eliot.parse.WrittenAction`: An action's type, level and status.
        - `eliot.parse.WrittenMessage`: A message's type and level.
        - ``tuple``: A field name and value.
    """
    if isinstance(node, Task):
        return u'{}'.format(
            theme.root(format.escape_control_characters(
                node.root().task_uuid)))
    elif isinstance(node, WrittenAction):
        return message_name(theme, format_value, node.start_message,
                            node.end_message)
    elif isinstance(node, WrittenMessage):
        return message_name(theme, format_value, node)
    elif isinstance(node, tuple):
        key, value = node
        if isinstance(value, (dict, list)):
            value = u''
        else:
            value = format_value(value, key)
        if is_namespace(key):
            key = format_namespace(key)
        return u'{}: {}'.format(
            theme.prop_key(format.escape_control_characters(key)),
            theme.prop_value(text_type(value)))
    raise NotImplementedError()
Example #2
0
 def get_name(task):
     if isinstance(task, text_type):
         return escape_control_characters(task)
     elif isinstance(task, tuple):
         key = task[0]
         if is_namespace(key):
             key = key.name
         name = escape_control_characters(key)
         if isinstance(task[1], dict):
             return name
         elif isinstance(task[1], text_type):
             return u'{}: {}'.format(
                 colors.prop(name),
                 # No need to escape this because we assume the value
                 # formatter did that already.
                 task[1])
         else:
             return colors.root(name)
     else:
         name = escape_control_characters(task.name)
         if task.success is True:
             return colors.success(name)
         elif task.success is False:
             return colors.failure(name)
         return name
Example #3
0
def message_name(theme, format_value, message, end_message=None):
    """
    Derive the name for a message.

    If the message is an action type then the ``action_type`` field is used in
    conjunction with ``task_level`` and ``action_status``. If the message is a
    message type then the ``message_type`` and ``task_level`` fields are used,
    otherwise no name will be derived.
    """
    if message is not None:
        timestamp = theme.timestamp(
            format_value(
                message.timestamp, field_name=eliot_ns('timestamp')))
        if u'action_type' in message.contents:
            action_type = format.escape_control_characters(
                message.contents.action_type)
            duration = u''
            if end_message:
                duration_seconds = end_message.timestamp - message.timestamp
                duration = u' {} {}'.format(
                    HOURGLASS,
                    theme.duration(
                        format_value(
                            duration_seconds,
                            field_name=eliot_ns('duration'))))
                action_status = end_message.contents.action_status
            else:
                action_status = message.contents.action_status
            status_color = identity
            if action_status == u'succeeded':
                status_color = theme.status_success
            elif action_status == u'failed':
                status_color = theme.status_failure
            return u'{}{} {} {} {}{}'.format(
                theme.parent(action_type),
                theme.task_level(message.task_level.to_string()),
                RIGHT_DOUBLE_ARROW,
                status_color(message.contents.action_status),
                timestamp,
                duration)
        elif u'message_type' in message.contents:
            message_type = format.escape_control_characters(
                message.contents.message_type)
            return u'{}{} {}'.format(
                theme.parent(message_type),
                theme.task_level(message.task_level.to_string()),
                timestamp)
    return u'<unnamed>'