Beispiel #1
0
def write(var, output_format="str", no_colour=True):
    """
    Writes ``var`` to stdout. If output_format is set to "json" or "yaml",
    write ``var`` as a JSON or YAML string.

    :param var: The object to print
    :type var: obj
    :param output_format: The format to print the output as. Allowed values: \
    "str", "json", "yaml"
    :type output_format: str
    :param no_colour: Whether to colour stack statuses
    :type no_colour: bool
    """
    stream = var

    if output_format == "json":
        encoder = CustomJsonEncoder(indent=4)
        stream = encoder.encode(var)
    if output_format == "yaml":
        stream = yaml.safe_dump(var, default_flow_style=False)
    if output_format == "str":
        stream = var
    if not no_colour:
        stack_status_colourer = StackStatusColourer()
        stream = stack_status_colourer.colour(stream)

    click.echo(stream)
Beispiel #2
0
def write(var, output_format="text", no_colour=True):
    """
    Writes ``var`` to stdout. If output_format is set to "json" or "yaml",
    write ``var`` as a JSON or YAML string.

    :param var: The object to print
    :type var: obj
    :param output_format: The format to print the output as. Allowed values: \
    "text", "json", "yaml"
    :type output_format: str
    :param no_colour: Whether to colour stack statuses
    :type no_colour: bool
    """
    output = var

    if output_format == "json":
        output = _generate_json(var)
    if output_format == "yaml":
        output = _generate_yaml(var)
    if output_format == "text":
        output = var
    if not no_colour:
        stack_status_colourer = StackStatusColourer()
        output = stack_status_colourer.colour(str(output))

    click.echo(output)
class TestStackStatusColourer(object):
    def setup_method(self, test_method):
        init()
        self.stack_status_colourer = StackStatusColourer()
        self.statuses = {
            "CREATE_COMPLETE": Fore.GREEN,
            "CREATE_FAILED": Fore.RED,
            "CREATE_IN_PROGRESS": Fore.YELLOW,
            "DELETE_COMPLETE": Fore.GREEN,
            "DELETE_FAILED": Fore.RED,
            "DELETE_IN_PROGRESS": Fore.YELLOW,
            "ROLLBACK_COMPLETE": Fore.RED,
            "ROLLBACK_FAILED": Fore.RED,
            "ROLLBACK_IN_PROGRESS": Fore.YELLOW,
            "UPDATE_COMPLETE": Fore.GREEN,
            "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS": Fore.YELLOW,
            "UPDATE_FAILED": Fore.RED,
            "UPDATE_IN_PROGRESS": Fore.YELLOW,
            "UPDATE_ROLLBACK_COMPLETE": Fore.GREEN,
            "UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS": Fore.YELLOW,
            "UPDATE_ROLLBACK_FAILED": Fore.RED,
            "UPDATE_ROLLBACK_IN_PROGRESS": Fore.YELLOW
        }

    def test_colour_with_string_with_no_stack_statuses(self):
        response = self.stack_status_colourer.colour("string with no statuses")
        assert response == "string with no statuses"

    def test_colour_with_string_with_single_stack_status(self):
        strings = [
            "string string {0} string".format(status)
            for status in sorted(self.statuses.keys())
        ]

        responses = [
            self.stack_status_colourer.colour(string) for string in strings
        ]

        assert responses == [
            "string string {0}{1}{2} string".format(self.statuses[status],
                                                    status, Style.RESET_ALL)
            for status in sorted(self.statuses.keys())
        ]

    def test_colour_with_string_with_multiple_stack_statuses(self):
        response = self.stack_status_colourer.colour(" ".join(
            sorted(self.statuses.keys())))
        assert response == " ".join([
            "{0}{1}{2}".format(self.statuses[status], status, Style.RESET_ALL)
            for status in sorted(self.statuses.keys())
        ])
 def setup_method(self, test_method):
     init()
     self.stack_status_colourer = StackStatusColourer()
     self.statuses = {
         "CREATE_COMPLETE": Fore.GREEN,
         "CREATE_FAILED": Fore.RED,
         "CREATE_IN_PROGRESS": Fore.YELLOW,
         "DELETE_COMPLETE": Fore.GREEN,
         "DELETE_FAILED": Fore.RED,
         "DELETE_IN_PROGRESS": Fore.YELLOW,
         "ROLLBACK_COMPLETE": Fore.RED,
         "ROLLBACK_FAILED": Fore.RED,
         "ROLLBACK_IN_PROGRESS": Fore.YELLOW,
         "UPDATE_COMPLETE": Fore.GREEN,
         "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS": Fore.YELLOW,
         "UPDATE_FAILED": Fore.RED,
         "UPDATE_IN_PROGRESS": Fore.YELLOW,
         "UPDATE_ROLLBACK_COMPLETE": Fore.GREEN,
         "UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS": Fore.YELLOW,
         "UPDATE_ROLLBACK_FAILED": Fore.RED,
         "UPDATE_ROLLBACK_IN_PROGRESS": Fore.YELLOW
     }
Beispiel #5
0
class ColouredFormatter(logging.Formatter):
    """
    ColouredFormatter add colours to all stack statuses that appear in log
    messages.
    """

    stack_status_colourer = StackStatusColourer()

    def format(self, record):
        """
        Colours and returns all stack statuses in ``record``.

        :param record: The log item to format.
        :type record: str
        :returns: str
        """
        response = super(ColouredFormatter, self).format(record)
        coloured_response = self.stack_status_colourer.colour(response)
        return coloured_response
 def setup_method(self, test_method):
     init()
     self.stack_status_colourer = StackStatusColourer()
     self.statuses = {
         "CREATE_COMPLETE": Fore.GREEN,
         "CREATE_FAILED": Fore.RED,
         "CREATE_IN_PROGRESS": Fore.YELLOW,
         "DELETE_COMPLETE": Fore.GREEN,
         "DELETE_FAILED": Fore.RED,
         "DELETE_IN_PROGRESS": Fore.YELLOW,
         "ROLLBACK_COMPLETE": Fore.RED,
         "ROLLBACK_FAILED": Fore.RED,
         "ROLLBACK_IN_PROGRESS": Fore.YELLOW,
         "UPDATE_COMPLETE": Fore.GREEN,
         "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS": Fore.YELLOW,
         "UPDATE_FAILED": Fore.RED,
         "UPDATE_IN_PROGRESS": Fore.YELLOW,
         "UPDATE_ROLLBACK_COMPLETE": Fore.GREEN,
         "UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS": Fore.YELLOW,
         "UPDATE_ROLLBACK_FAILED": Fore.RED,
         "UPDATE_ROLLBACK_IN_PROGRESS": Fore.YELLOW
     }
class TestStackStatusColourer(object):

    def setup_method(self, test_method):
        init()
        self.stack_status_colourer = StackStatusColourer()
        self.statuses = {
            "CREATE_COMPLETE": Fore.GREEN,
            "CREATE_FAILED": Fore.RED,
            "CREATE_IN_PROGRESS": Fore.YELLOW,
            "DELETE_COMPLETE": Fore.GREEN,
            "DELETE_FAILED": Fore.RED,
            "DELETE_IN_PROGRESS": Fore.YELLOW,
            "ROLLBACK_COMPLETE": Fore.RED,
            "ROLLBACK_FAILED": Fore.RED,
            "ROLLBACK_IN_PROGRESS": Fore.YELLOW,
            "UPDATE_COMPLETE": Fore.GREEN,
            "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS": Fore.YELLOW,
            "UPDATE_FAILED": Fore.RED,
            "UPDATE_IN_PROGRESS": Fore.YELLOW,
            "UPDATE_ROLLBACK_COMPLETE": Fore.GREEN,
            "UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS": Fore.YELLOW,
            "UPDATE_ROLLBACK_FAILED": Fore.RED,
            "UPDATE_ROLLBACK_IN_PROGRESS": Fore.YELLOW
        }

    def test_colour_with_string_with_no_stack_statuses(self):
        response = self.stack_status_colourer.colour("string with no statuses")
        assert response == "string with no statuses"

    def test_colour_with_string_with_single_stack_status(self):
        strings = [
            "string string {0} string".format(status)
            for status in sorted(self.statuses.keys())
        ]

        responses = [
            self.stack_status_colourer.colour(string)
            for string in strings
        ]

        assert responses == [
            "string string {0}{1}{2} string".format(
                self.statuses[status],
                status,
                Style.RESET_ALL
            )
            for status in sorted(self.statuses.keys())
        ]

    def test_colour_with_string_with_multiple_stack_statuses(self):
        response = self.stack_status_colourer.colour(
            " ".join(sorted(self.statuses.keys()))
        )
        assert response == " ".join([
            "{0}{1}{2}".format(
                self.statuses[status],
                status,
                Style.RESET_ALL
            )
            for status in sorted(self.statuses.keys())
        ])