Exemplo n.º 1
0
 def test_html_conversion(self):
     """Check the conversion from ANSI escape sequences to HTML."""
     # Check conversion of colored text.
     for color_name, ansi_code in ANSI_COLOR_CODES.items():
         ansi_encoded_text = 'plain text followed by %s text' % ansi_wrap(color_name, color=color_name)
         expected_html = format(
             '<code>plain text followed by <span style="color:{css}">{name}</span> text</code>',
             css=EIGHT_COLOR_PALETTE[ansi_code], name=color_name,
         )
         self.assertEqual(expected_html, convert(ansi_encoded_text))
     # Check conversion of bright colored text.
     expected_html = '<code><span style="color:#FF0">bright yellow</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('bright yellow', color='yellow', bright=True)))
     # Check conversion of text with a background color.
     expected_html = '<code><span style="background-color:#DE382B">red background</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('red background', background='red')))
     # Check conversion of text with a bright background color.
     expected_html = '<code><span style="background-color:#F00">bright red background</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('bright red background', background='red', bright=True)))
     # Check conversion of text that uses the 256 color mode palette as a foreground color.
     expected_html = '<code><span style="color:#FFAF00">256 color mode foreground</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('256 color mode foreground', color=214)))
     # Check conversion of text that uses the 256 color mode palette as a background color.
     expected_html = '<code><span style="background-color:#AF0000">256 color mode background</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('256 color mode background', background=124)))
     # Check that invalid 256 color mode indexes don't raise exceptions.
     expected_html = '<code>plain text expected</code>'
     self.assertEqual(expected_html, convert('\x1b[38;5;256mplain text expected\x1b[0m'))
     # Check conversion of bold text.
     expected_html = '<code><span style="font-weight:bold">bold text</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('bold text', bold=True)))
     # Check conversion of underlined text.
     expected_html = '<code><span style="text-decoration:underline">underlined text</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('underlined text', underline=True)))
     # Check conversion of strike-through text.
     expected_html = '<code><span style="text-decoration:line-through">strike-through text</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('strike-through text', strike_through=True)))
     # Check conversion of inverse text.
     expected_html = '<code><span style="background-color:#FFC706;color:#000">inverse</span></code>'
     self.assertEqual(expected_html, convert(ansi_wrap('inverse', color='yellow', inverse=True)))
     # Check conversion of URLs.
     for sample_text in 'www.python.org', 'http://coloredlogs.rtfd.org', 'https://coloredlogs.rtfd.org':
         sample_url = sample_text if '://' in sample_text else ('http://' + sample_text)
         expected_html = '<code><a href="%s" style="color:inherit">%s</a></code>' % (sample_url, sample_text)
         self.assertEqual(expected_html, convert(sample_text))
     # Check that the capture pattern for URLs doesn't match ANSI escape
     # sequences and also check that the short hand for the 0 reset code is
     # supported. These are tests for regressions of bugs found in
     # coloredlogs <= 8.0.
     reset_short_hand = '\x1b[0m'
     blue_underlined = ansi_style(color='blue', underline=True)
     ansi_encoded_text = '<%shttps://coloredlogs.readthedocs.io%s>' % (blue_underlined, reset_short_hand)
     expected_html = (
         '<code>&lt;<span style="color:#006FB8;text-decoration:underline">'
         '<a href="https://coloredlogs.readthedocs.io" style="color:inherit">'
         'https://coloredlogs.readthedocs.io'
         '</a></span>&gt;</code>'
     )
     self.assertEqual(expected_html, convert(ansi_encoded_text))
Exemplo n.º 2
0
 def test_html_conversion(self):
     """Check the conversion from ANSI escape sequences to HTML."""
     # Check conversion of colored text.
     for color_name, ansi_code in ANSI_COLOR_CODES.items():
         ansi_encoded_text = 'plain text followed by %s text' % ansi_wrap(color_name, color=color_name)
         expected_html = format(
             '<code>plain text followed by <span style="color:{css}">{name}</span> text</code>',
             css=EIGHT_COLOR_PALETTE[ansi_code], name=color_name,
         )
         self.assertEquals(expected_html, convert(ansi_encoded_text))
     # Check conversion of bright colored text.
     expected_html = '<code><span style="color:#FF0">bright yellow</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('bright yellow', color='yellow', bright=True)))
     # Check conversion of text with a background color.
     expected_html = '<code><span style="background-color:#DE382B">red background</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('red background', background='red')))
     # Check conversion of text with a bright background color.
     expected_html = '<code><span style="background-color:#F00">bright red background</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('bright red background', background='red', bright=True)))
     # Check conversion of text that uses the 256 color mode palette as a foreground color.
     expected_html = '<code><span style="color:#FFAF00">256 color mode foreground</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('256 color mode foreground', color=214)))
     # Check conversion of text that uses the 256 color mode palette as a background color.
     expected_html = '<code><span style="background-color:#AF0000">256 color mode background</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('256 color mode background', background=124)))
     # Check that invalid 256 color mode indexes don't raise exceptions.
     expected_html = '<code>plain text expected</code>'
     self.assertEquals(expected_html, convert('\x1b[38;5;256mplain text expected\x1b[0m'))
     # Check conversion of bold text.
     expected_html = '<code><span style="font-weight:bold">bold text</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('bold text', bold=True)))
     # Check conversion of underlined text.
     expected_html = '<code><span style="text-decoration:underline">underlined text</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('underlined text', underline=True)))
     # Check conversion of strike-through text.
     expected_html = '<code><span style="text-decoration:line-through">strike-through text</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('strike-through text', strike_through=True)))
     # Check conversion of inverse text.
     expected_html = '<code><span style="background-color:#FFC706;color:#000">inverse</span></code>'
     self.assertEquals(expected_html, convert(ansi_wrap('inverse', color='yellow', inverse=True)))
     # Check conversion of URLs.
     for sample_text in 'www.python.org', 'http://coloredlogs.rtfd.org', 'https://coloredlogs.rtfd.org':
         sample_url = sample_text if '://' in sample_text else ('http://' + sample_text)
         expected_html = '<code><a href="%s" style="color:inherit">%s</a></code>' % (sample_url, sample_text)
         self.assertEquals(expected_html, convert(sample_text))
     # Check that the capture pattern for URLs doesn't match ANSI escape
     # sequences and also check that the short hand for the 0 reset code is
     # supported. These are tests for regressions of bugs found in
     # coloredlogs <= 8.0.
     reset_short_hand = '\x1b[0m'
     blue_underlined = ansi_style(color='blue', underline=True)
     ansi_encoded_text = '<%shttps://coloredlogs.readthedocs.io%s>' % (blue_underlined, reset_short_hand)
     expected_html = (
         '<code>&lt;<span style="color:#006FB8;text-decoration:underline">'
         '<a href="https://coloredlogs.readthedocs.io" style="color:inherit">'
         'https://coloredlogs.readthedocs.io'
         '</a></span>&gt;</code>'
     )
     self.assertEquals(expected_html, convert(ansi_encoded_text))
Exemplo n.º 3
0
 def test_ansi_style(self):
     assert ansi_style(bold=True) == '%s1%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(faint=True) == '%s2%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(underline=True) == '%s4%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(inverse=True) == '%s7%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(strike_through=True) == '%s9%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(color='blue') == '%s34%s' % (ANSI_CSI, ANSI_SGR)
     self.assertRaises(ValueError, ansi_style, color='unknown')
Exemplo n.º 4
0
    def emit_style(self, style=None):
        """
        Emit an ANSI escape sequence for the given or current style to the output stream.

        :param style: A dictionary with arguments for :func:`.ansi_style()` or
                      :data:`None`, in which case the style at the top of the
                      stack is emitted.
        """
        # Clear the current text styles.
        self.output.write(ANSI_RESET)
        # Apply a new text style?
        style = self.current_style if style is None else style
        if style:
            self.output.write(ansi_style(**style))
Exemplo n.º 5
0
 def test_ansi_style(self):
     """Test :func:`humanfriendly.terminal.ansi_style()`."""
     assert ansi_style(bold=True) == '%s1%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(faint=True) == '%s2%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(underline=True) == '%s4%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(inverse=True) == '%s7%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(strike_through=True) == '%s9%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(color='blue') == '%s34%s' % (ANSI_CSI, ANSI_SGR)
     self.assertRaises(ValueError, ansi_style, color='unknown')
Exemplo n.º 6
0
 def test_ansi_style(self):
     """Test :func:`humanfriendly.terminal.ansi_style()`."""
     assert ansi_style(bold=True) == '%s1%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(faint=True) == '%s2%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(underline=True) == '%s4%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(inverse=True) == '%s7%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(strike_through=True) == '%s9%s' % (ANSI_CSI, ANSI_SGR)
     assert ansi_style(color='blue') == '%s34%s' % (ANSI_CSI, ANSI_SGR)
     self.assertRaises(ValueError, ansi_style, color='unknown')
    def __init__(self, colorstyle='red', *args, **kwargs):
        if colorstyle == 'console':
            self.colorstyle = LogStyle.records
        elif colorstyle == 'qt':
            self.colorstyle = LogStyle.qtRecords
        else:
            raise ValueError(f"Invalid color style: {colorstyle}")

        self.colors = dict(
            startbyte=ansi_style(**self.colorstyle['warning']),
            address=ansi_style(**self.colorstyle['info']),
            header=ansi_style(**self.colorstyle['notice']),
            ack=ansi_style(**self.colorstyle['success']),
            data=ansi_style(**self.colorstyle['verbose']),
            lrc=ansi_style(**self.colorstyle['error']),
            zero=ansi_style(**self.colorstyle['spam']),
            rfc=ansi_style(**self.colorstyle['critical']),
            header_rfc=ansi_style(**self.colorstyle['critical']),
            reset=ANSI_RESET,
        )

        super().__init__(*args, **kwargs)
class PacketFormatter(ColoredFormatter):

    # Defaults
    colors = dict(
        startbyte=ansi_style(color='yellow'),
        address=ansi_style(color='blue'),
        header=ansi_style(color='white'),
        ack=ansi_style(color='green'),
        data=ansi_style(color='white'),
        lrc=ansi_style(color='cyan'),
        zero=ansi_style(color='magenta'),
        rfc=ansi_style(color='red'),
        header_rfc=ansi_style(color='red'),
        reset=ANSI_RESET,
    )

    def __init__(self, colorstyle='red', *args, **kwargs):
        if colorstyle == 'console':
            self.colorstyle = LogStyle.records
        elif colorstyle == 'qt':
            self.colorstyle = LogStyle.qtRecords
        else:
            raise ValueError(f"Invalid color style: {colorstyle}")

        self.colors = dict(
            startbyte=ansi_style(**self.colorstyle['warning']),
            address=ansi_style(**self.colorstyle['info']),
            header=ansi_style(**self.colorstyle['notice']),
            ack=ansi_style(**self.colorstyle['success']),
            data=ansi_style(**self.colorstyle['verbose']),
            lrc=ansi_style(**self.colorstyle['error']),
            zero=ansi_style(**self.colorstyle['spam']),
            rfc=ansi_style(**self.colorstyle['critical']),
            header_rfc=ansi_style(**self.colorstyle['critical']),
            reset=ANSI_RESET,
        )

        super().__init__(*args, **kwargs)

    def format(self, record):
        msg = record.msg
        msg_start = msg.find('5A')
        prefix = msg[:msg_start]
        packet = msg[msg_start:].split(' ')

        is_reply = packet[1] == '00'
        is_zero = (int(packet[3], base=16) & 1 << 7) >> 7
        core_range = (6, 7) if is_reply else (6, 8)
        data_range = (core_range[1], -3 - is_zero)

        parts = dict(
            startbyte=packet[0],
            address=packet[1],
            header=' '.join(packet[2:4]),
            header_rfc=' '.join(packet[4:6]),
            ack=' '.join(packet[slice(*core_range)]),
            data=' '.join(packet[slice(*data_range)]),
            lrc=packet[data_range[1]],
            zero=packet[-3] if is_zero else '',
            rfc=' '.join(packet[-2:]),
        )

        hex_string = ' '.join(self.colors[name] + value
                              for name, value in parts.items() if value)
        record.msg = prefix + hex_string + self.colors['reset']
        return super().format(record)
"""
import logging
import traceback
import six
import sys
from collections import deque

import humanfriendly.terminal as hft
import humanfriendly.terminal.spinners as spinners

from mapactionpy_controller.steps import Step
from mapactionpy_controller.task_renderer import TaskReferralBase

logger = logging.getLogger(__name__)

bright_white = hft.ansi_style(color='white', bright=True)
bright_green = hft.ansi_style(color='green', bright=True)
bright_red = hft.ansi_style(color='red', bright=True)
bright_yellow = hft.ansi_style(color='yellow', bright=True)
normal_white = hft.ansi_style(color='white', bright=False)

terminal_checkboxs = {
    logging.INFO:
    '{}[{}pass{}]{}'.format(normal_white, bright_green, normal_white,
                            bright_white),
    logging.ERROR:
    '{}[{}fail{}]{}'.format(normal_white, bright_red, normal_white,
                            bright_white),
    logging.WARNING:
    '{}[{}warn{}]{}'.format(normal_white, bright_yellow, normal_white,
                            bright_white)