示例#1
0
    def __init__(self, verbosity = OutputInterface.VERBOSITY_NORMAL, decorated = None, formatter = None):
        """Constructor.

        @param integer                  verbosity The verbosity level (self.VERBOSITY_QUIET, self.VERBOSITY_NORMAL, self.VERBOSITY_VERBOSE)
        @param Boolean                  decorated Whether to decorate messages or not (None for auto-guessing)
        @param OutputFormatterInterface formatter Output formatter instance

        @api

        """
        if formatter:
            assert isinstance(formatter, OutputFormatterInterface);

        self.__verbosity = None;
        self.__formatter = None;
    
        if None is verbosity:
            self.__verbosity = self.VERBOSITY_NORMAL;
        else:
            self.__verbosity = verbosity;
        if None is formatter:
            self.__formatter = OutputFormatter();
        else:
            self.__formatter = formatter;
        self.__formatter.setDecorated(bool(decorated));
示例#2
0
    def testDeepNestedStyles(self):

        formatter = OutputFormatter(True)

        self.assertEqual(
            "\033[37;41merror\033[0m\033[32minfo\033[0m\033[33mcomment\033[0m\033[37;41merror\033[0m",
            formatter.format(
                '<error>error<info>info<comment>comment</info>error</error>'))
示例#3
0
    def testStyleEscaping(self):

        formatter = OutputFormatter(True)

        self.assertEqual(
            "(\033[32mz>=2.0,<a2.3\033[0m)",
            formatter.format('(<info>' + formatter.escape('z>=2.0,<a2.3') +
                             '</info>)'))
示例#4
0
    def testRedefineStyle(self):

        formatter = OutputFormatter(True);

        style = OutputFormatterStyle('blue', 'white');
        formatter.setStyle('info', style);

        self.assertEqual("\033[34;47msome custom msg\033[0m", formatter.format('<info>some custom msg</info>'));
示例#5
0
    def testNestedStyles(self):

        formatter = OutputFormatter(True)

        self.assertEqual(
            "\033[37;41msome \033[0m\033[32msome info\033[0m\033[37;41m error\033[0m",
            formatter.format(
                '<error>some <info>some info</info> error</error>'))
示例#6
0
    def testDeepNestedStyles(self):

        formatter = OutputFormatter(True);

        self.assertEqual(
            "\033[37;41merror\033[0m\033[32minfo\033[0m\033[33mcomment\033[0m\033[37;41merror\033[0m",
            formatter.format('<error>error<info>info<comment>comment</info>error</error>')
        );
示例#7
0
    def testStyleEscaping(self):

        formatter = OutputFormatter(True);

        self.assertEqual(
            "(\033[32mz>=2.0,<a2.3\033[0m)",
            formatter.format('(<info>'+formatter.escape('z>=2.0,<a2.3')+'</info>)')
        );
示例#8
0
    def testStyleMatchingNotGreedy(self):

        formatter = OutputFormatter(True);

        self.assertEqual(
            "(\033[32m>=2.0,<2.3\033[0m)",
            formatter.format('(<info>>=2.0,<2.3</info>)')
        );
示例#9
0
    def testNestedStyles(self):

        formatter = OutputFormatter(True);

        self.assertEqual(
            "\033[37;41msome \033[0m\033[32msome info\033[0m\033[37;41m error\033[0m",
            formatter.format('<error>some <info>some info</info> error</error>')
        );
示例#10
0
    def testRedefineStyle(self):

        formatter = OutputFormatter(True)

        style = OutputFormatterStyle('blue', 'white')
        formatter.setStyle('info', style)

        self.assertEqual("\033[34;47msome custom msg\033[0m",
                         formatter.format('<info>some custom msg</info>'))
示例#11
0
    def testInlineStyle(self):

        formatter = OutputFormatter(True)

        self.assertEqual("\033[34;41msome text\033[0m",
                         formatter.format('<fg=blue;bg=red>some text</>'))
        self.assertEqual(
            "\033[34;41msome text\033[0m",
            formatter.format('<fg=blue;bg=red>some text</fg=blue;bg=red>'))
示例#12
0
    def testBundledStyles(self):

        formatter = OutputFormatter(True);

        self.assertTrue(formatter.hasStyle('error'));
        self.assertTrue(formatter.hasStyle('info'));
        self.assertTrue(formatter.hasStyle('comment'));
        self.assertTrue(formatter.hasStyle('question'));

        self.assertEqual(
            "\033[37;41msome error\033[0m",
            formatter.format('<error>some error</error>')
        );
        self.assertEqual(
            "\033[32msome info\033[0m",
            formatter.format('<info>some info</info>')
        );
        self.assertEqual(
            "\033[33msome comment\033[0m",
            formatter.format('<comment>some comment</comment>')
        );
        self.assertEqual(
            "\033[30;46msome question\033[0m",
            formatter.format('<question>some question</question>')
        );
示例#13
0
    def testLGCharEscaping(self):

        formatter = OutputFormatter(True);

        self.assertEqual("foo<bar", formatter.format('foo\\<bar'));
        self.assertEqual("<info>some info</info>", formatter.format('\\<info>some info\\</info>'));
        self.assertEqual("\\<info>some info\\</info>", OutputFormatter.escape('<info>some info</info>'));

        self.assertEqual(
            "\033[33mSymfony\\Component\\Console does work very well!\033[0m",
            formatter.format('<comment>Symfony\Component\Console does work very well!</comment>')
        );
示例#14
0
    def testContentWithLineBreaks(self):

        formatter = OutputFormatter(True);

        self.assertEqual("""
\033[32m
some text\033[0m
"""
            , formatter.format("""
<info>
some text</info>
"""
        ));

        self.assertEqual("""
\033[32msome text
\033[0m
"""
            , formatter.format("""
<info>some text
</info>
"""
        ));

        self.assertEqual("""
\033[32m
some text
\033[0m
"""
            , formatter.format("""
<info>
some text
</info>
"""
        ));

        self.assertEqual("""
\033[32m
some text
more text
\033[0m
"""
            , formatter.format("""
<info>
some text
more text
</info>
"""
        ));
示例#15
0
    def testBundledStyles(self):

        formatter = OutputFormatter(True)

        self.assertTrue(formatter.hasStyle('error'))
        self.assertTrue(formatter.hasStyle('info'))
        self.assertTrue(formatter.hasStyle('comment'))
        self.assertTrue(formatter.hasStyle('question'))

        self.assertEqual("\033[37;41msome error\033[0m",
                         formatter.format('<error>some error</error>'))
        self.assertEqual("\033[32msome info\033[0m",
                         formatter.format('<info>some info</info>'))
        self.assertEqual("\033[33msome comment\033[0m",
                         formatter.format('<comment>some comment</comment>'))
        self.assertEqual(
            "\033[30;46msome question\033[0m",
            formatter.format('<question>some question</question>'))
示例#16
0
    def testNewStyle(self):

        formatter = OutputFormatter(True)

        style = OutputFormatterStyle('blue', 'white')
        formatter.setStyle('test', style)

        self.assertEqual(style, formatter.getStyle('test'))
        self.assertNotEqual(style, formatter.getStyle('info'))

        self.assertEqual("\033[34;47msome custom msg\033[0m",
                         formatter.format('<test>some custom msg</test>'))
示例#17
0
    def testNewStyle(self):

        formatter = OutputFormatter(True);

        style = OutputFormatterStyle('blue', 'white');
        formatter.setStyle('test', style);

        self.assertEqual(style, formatter.getStyle('test'));
        self.assertNotEqual(style, formatter.getStyle('info'));

        self.assertEqual("\033[34;47msome custom msg\033[0m", formatter.format('<test>some custom msg</test>'));
示例#18
0
    def testLGCharEscaping(self):

        formatter = OutputFormatter(True)

        self.assertEqual("foo<bar", formatter.format('foo\\<bar'))
        self.assertEqual("<info>some info</info>",
                         formatter.format('\\<info>some info\\</info>'))
        self.assertEqual("\\<info>some info\\</info>",
                         OutputFormatter.escape('<info>some info</info>'))

        self.assertEqual(
            "\033[33mSymfony\\Component\\Console does work very well!\033[0m",
            formatter.format(
                '<comment>Symfony\Component\Console does work very well!</comment>'
            ))
示例#19
0
    def testContentWithLineBreaks(self):

        formatter = OutputFormatter(True)

        self.assertEqual("""
\033[32m
some text\033[0m
""", formatter.format("""
<info>
some text</info>
"""))

        self.assertEqual("""
\033[32msome text
\033[0m
""", formatter.format("""
<info>some text
</info>
"""))

        self.assertEqual("""
\033[32m
some text
\033[0m
""", formatter.format("""
<info>
some text
</info>
"""))

        self.assertEqual(
            """
\033[32m
some text
more text
\033[0m
""", formatter.format("""
<info>
some text
more text
</info>
"""))
示例#20
0
    def testNotDecoratedFormatter(self):

        formatter = OutputFormatter(False)

        self.assertTrue(formatter.hasStyle('error'))
        self.assertTrue(formatter.hasStyle('info'))
        self.assertTrue(formatter.hasStyle('comment'))
        self.assertTrue(formatter.hasStyle('question'))

        self.assertEqual("some error",
                         formatter.format('<error>some error</error>'))
        self.assertEqual("some info",
                         formatter.format('<info>some info</info>'))
        self.assertEqual("some comment",
                         formatter.format('<comment>some comment</comment>'))
        self.assertEqual(
            "some question",
            formatter.format('<question>some question</question>'))

        formatter.setDecorated(True)

        self.assertEqual("\033[37;41msome error\033[0m",
                         formatter.format('<error>some error</error>'))
        self.assertEqual("\033[32msome info\033[0m",
                         formatter.format('<info>some info</info>'))
        self.assertEqual("\033[33msome comment\033[0m",
                         formatter.format('<comment>some comment</comment>'))
        self.assertEqual(
            "\033[30;46msome question\033[0m",
            formatter.format('<question>some question</question>'))
示例#21
0
class Output(OutputInterface):
    """Base class for output classes.

    There are three levels of verbosity:

        normal: no option passed (normal output - information)
        verbose: -v (more output - debug)
        quiet: -q (no output)

    @author Fabien Potencier <*****@*****.**>

    @api

    """


    def __init__(self, verbosity = OutputInterface.VERBOSITY_NORMAL, decorated = None, formatter = None):
        """Constructor.

        @param integer                  verbosity The verbosity level (self.VERBOSITY_QUIET, self.VERBOSITY_NORMAL, self.VERBOSITY_VERBOSE)
        @param Boolean                  decorated Whether to decorate messages or not (None for auto-guessing)
        @param OutputFormatterInterface formatter Output formatter instance

        @api

        """
        if formatter:
            assert isinstance(formatter, OutputFormatterInterface);

        self.__verbosity = None;
        self.__formatter = None;
    
        if None is verbosity:
            self.__verbosity = self.VERBOSITY_NORMAL;
        else:
            self.__verbosity = verbosity;
        if None is formatter:
            self.__formatter = OutputFormatter();
        else:
            self.__formatter = formatter;
        self.__formatter.setDecorated(bool(decorated));


    def setFormatter(self, formatter):
        """Sets output formatter.

        @param OutputFormatterInterface formatter

        @api

        """
        assert isinstance(formatter, OutputFormatterInterface);

        self.__formatter = formatter;


    def getFormatter(self):
        """Returns current output formatter instance.

        @return  OutputFormatterInterface

        @api

        """

        return self.__formatter;


    def setDecorated(self, decorated):
        """Sets the decorated flag.

        @param Boolean decorated Whether to decorate the messages or not

        @api

        """

        self.__formatter.setDecorated(bool(decorated));


    def isDecorated(self):
        """Gets the decorated flag.

        @return Boolean True if the output will decorate messages, False otherwise:

        @api

        """

        return self.__formatter.isDecorated();


    def setVerbosity(self, level):
        """Sets the verbosity of the output.

        @param integer level The level of verbosity

        @api

        """

        self.__verbosity = int(level);


    def getVerbosity(self):
        """Gets the current verbosity of the output.

        @return integer The current level of verbosity

        @api

        """

        return self.__verbosity;


    def writeln(self, messages, outputType = OutputInterface.OUTPUT_NORMAL):
        """Writes a message to the output and adds a newline at the end.

        @param string|list messages    The message as an array of lines or a
                                      single string
        @param integer     outputType  The type of output

        @api

        """

        self.write(messages, True, outputType);


    def write(self, messages, newline = False, outputType = OutputInterface.OUTPUT_NORMAL):
        """Writes a message to the output.

        @param string|list messages The message as a list of lines or a single string
        @param Boolean      newline  Whether to add a newline or not
        @param integer      type     The type of output

        @raise InvalidArgumentException When unknown output type is given

        @api

        """

        if (self.VERBOSITY_QUIET == self.__verbosity) :
            return;

        if not isinstance(messages, list):
            messages = [str(messages)];

        for message in messages:
            if OutputInterface.OUTPUT_NORMAL == outputType:
                message = self.__formatter.format(message);
            elif OutputInterface.OUTPUT_RAW == outputType:
                pass;
            elif OutputInterface.OUTPUT_PLAIN == outputType:
                message = self.__formatter.format(message);
                message = re.sub(r'<[^>]*?>', '', message);
            else:
                raise InvalidArgumentException(
                    'Unknown output type given ({0})'.format(outputType)
                );


            self._doWrite(message, newline);



    @abstract
    def _doWrite(self, message, newline):
        """Writes a message to the output.
示例#22
0
    def testEmptyTag(self):

        formatter = OutputFormatter(True);
        self.assertEqual("foo<>bar", formatter.format('foo<>bar'));
示例#23
0
    def testNonStyleTag(self):

        formatter = OutputFormatter(True)
        self.assertEqual("\033[32msome \033[0m\033[32m<tag> styled\033[0m",
                         formatter.format('<info>some <tag> styled</info>'))
示例#24
0
    def testInlineStyle(self):

        formatter = OutputFormatter(True);

        self.assertEqual("\033[34;41msome text\033[0m", formatter.format('<fg=blue;bg=red>some text</>'));
        self.assertEqual("\033[34;41msome text\033[0m", formatter.format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
示例#25
0
    def testNonStyleTag(self):

        formatter = OutputFormatter(True);
        self.assertEqual("\033[32msome \033[0m\033[32m<tag> styled\033[0m", formatter.format('<info>some <tag> styled</info>'));
示例#26
0
    def testNotDecoratedFormatter(self):

        formatter = OutputFormatter(False);

        self.assertTrue(formatter.hasStyle('error'));
        self.assertTrue(formatter.hasStyle('info'));
        self.assertTrue(formatter.hasStyle('comment'));
        self.assertTrue(formatter.hasStyle('question'));

        self.assertEqual(
            "some error", formatter.format('<error>some error</error>')
        );
        self.assertEqual(
            "some info", formatter.format('<info>some info</info>')
        );
        self.assertEqual(
            "some comment", formatter.format('<comment>some comment</comment>')
        );
        self.assertEqual(
            "some question", formatter.format('<question>some question</question>')
        );

        formatter.setDecorated(True);

        self.assertEqual(
            "\033[37;41msome error\033[0m", formatter.format('<error>some error</error>')
        );
        self.assertEqual(
            "\033[32msome info\033[0m", formatter.format('<info>some info</info>')
        );
        self.assertEqual(
            "\033[33msome comment\033[0m", formatter.format('<comment>some comment</comment>')
        );
        self.assertEqual(
            "\033[30;46msome question\033[0m", formatter.format('<question>some question</question>')
        );
示例#27
0
    def testEmptyTag(self):

        formatter = OutputFormatter(True)
        self.assertEqual("foo<>bar", formatter.format('foo<>bar'))
示例#28
0
    def testStyleMatchingNotGreedy(self):

        formatter = OutputFormatter(True)

        self.assertEqual("(\033[32m>=2.0,<2.3\033[0m)",
                         formatter.format('(<info>>=2.0,<2.3</info>)'))