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 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>'))
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>)'))
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>'));
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>'))
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>') );
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>)') );
def testStyleMatchingNotGreedy(self): formatter = OutputFormatter(True); self.assertEqual( "(\033[32m>=2.0,<2.3\033[0m)", formatter.format('(<info>>=2.0,<2.3</info>)') );
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>') );
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>'))
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>'))
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>') );
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>') );
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> """ ));
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>'))
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>'))
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>'));
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>' ))
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> """))
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>'))
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.
def testEmptyTag(self): formatter = OutputFormatter(True); self.assertEqual("foo<>bar", formatter.format('foo<>bar'));
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>'))
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>'));
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>'));
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>') );
def testEmptyTag(self): formatter = OutputFormatter(True) self.assertEqual("foo<>bar", formatter.format('foo<>bar'))
def testStyleMatchingNotGreedy(self): formatter = OutputFormatter(True) self.assertEqual("(\033[32m>=2.0,<2.3\033[0m)", formatter.format('(<info>>=2.0,<2.3</info>)'))