From e87c31aea43c07eab6a60ab1d388e9eee20d0cc5 Mon Sep 17 00:00:00 2001 From: James Vega Date: Thu, 15 Apr 2004 21:15:57 +0000 Subject: [PATCH] ircutils.strip{Bold,Reverse,Underline,Formatting} --- ChangeLog | 4 +++ src/ircutils.py | 24 ++++++++++++++++-- test/test_ircutils.py | 59 ++++++++++++++++++++++++++++++++----------- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index c32a17bc8..ee0789b8e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ + * Added ircutils.strip{Bold,Reverse,Underline,Formatting}, which + will remove the specified formatting or all forms of formatting + in the case of stripFormatting. + * Replaced Sourceforge.{rfe,bug} with Sourceforge.tracker, which can query any tracker type (not just RFEs and bugs) and responds with more information, a la trackerSnarfer. diff --git a/src/ircutils.py b/src/ircutils.py index 4c98b83fd..9a68d2499 100644 --- a/src/ircutils.py +++ b/src/ircutils.py @@ -321,11 +321,31 @@ def canonicalColor(s, bg=False, shift=0): else: return (fg, None) +def stripBold(s): + """Returns the string s, with bold removed.""" + return s.replace('\x02', '') + _unColorRe = re.compile(r'\x03(?:\d{1,2},\d{1,2}|\d{1,2}|,\d{1,2}|)') -def unColor(s): - """Removes the color from a string.""" +def stripColor(s): + """Returns the string s, with color removed.""" return _unColorRe.sub('', s) +def stripReverse(s): + """Returns the string s, with reverse-video removed.""" + return s.replace('\x16', '') + +def stripUnderline(s): + """Returns the string s, with underlining removed.""" + return s.replace('\x1f', '').replace('\x1F', '') + +def stripFormatting(s): + """Returns the string s, with all formatting removed.""" + s = stripBold(s) + s = stripColor(s) + s = stripReverse(s) + s = stripUnderline(s) + return s.replace('\x0f', '').replace('\x0F', '') + def isValidArgument(s): """Returns whether s is strictly a valid argument for an IRC message.""" return '\r' not in s and '\n' not in s and '\x00' not in s diff --git a/test/test_ircutils.py b/test/test_ircutils.py index c44e72ba6..8c243eb7f 100644 --- a/test/test_ircutils.py +++ b/test/test_ircutils.py @@ -93,6 +93,16 @@ class FunctionsTestCase(SupyTestCase): self.assertEqual(s[0], '\x02') self.assertEqual(s[-1], '\x02') + def testUnderline(self): + s = ircutils.underline('foo') + self.assertEqual(s[0], '\x1f') + self.assertEqual(s[-1], '\x1f') + + def testReverse(self): + s = ircutils.reverse('foo') + self.assertEqual(s[0], '\x16') + self.assertEqual(s[-1], '\x16') + def testMircColor(self): # No colors provided should return the same string s = 'foo' @@ -113,6 +123,39 @@ class FunctionsTestCase(SupyTestCase): if k: self.assertEqual(ircutils.mircColors[v], k) + def testStripBold(self): + self.assertEqual(ircutils.stripBold(ircutils.bold('foo')), 'foo') + + def testStripColor(self): + self.assertEqual(ircutils.stripColor('\x02bold\x0302,04foo\x03bar\x0f'), + '\x02boldfoobar\x0f') + self.assertEqual(ircutils.stripColor('\x03foo\x03'), 'foo') + self.assertEqual(ircutils.stripColor('\x03foo\x0F'), 'foo\x0F') + self.assertEqual(ircutils.stripColor('\x0312foo\x03'), 'foo') + self.assertEqual(ircutils.stripColor('\x0312,14foo\x03'), 'foo') + self.assertEqual(ircutils.stripColor('\x03,14foo\x03'), 'foo') + self.assertEqual(ircutils.stripColor('\x03,foo\x03'), ',foo') + self.assertEqual(ircutils.stripColor('\x0312foo\x0F'), 'foo\x0F') + self.assertEqual(ircutils.stripColor('\x0312,14foo\x0F'), 'foo\x0F') + self.assertEqual(ircutils.stripColor('\x03,14foo\x0F'), 'foo\x0F') + self.assertEqual(ircutils.stripColor('\x03,foo\x0F'), ',foo\x0F') + + def testStripReverse(self): + self.assertEqual(ircutils.stripReverse(ircutils.reverse('foo')), 'foo') + + def testStripUnderline(self): + self.assertEqual(ircutils.stripUnderline(ircutils.underline('foo')), + 'foo') + + def testStripFormatting(self): + self.assertEqual(ircutils.stripFormatting(ircutils.bold('foo')), 'foo') + self.assertEqual(ircutils.stripFormatting(ircutils.reverse('foo')), + 'foo') + self.assertEqual(ircutils.stripFormatting(ircutils.underline('foo')), + 'foo') + self.assertEqual(ircutils.stripFormatting('\x02bold\x0302,04foo\x03' + 'bar\x0f'), + 'boldfoobar') def testSafeArgument(self): s = 'I have been running for 9 seconds' @@ -175,20 +218,6 @@ class FunctionsTestCase(SupyTestCase): self.assertEqual(ircutils.joinModes(modes), ['+be-l', plusB[1], plusE[1]]) - def testUnColor(self): - self.assertEqual(ircutils.unColor('\x02bold\x0302,04foo\x03bar\x0f'), - '\x02boldfoobar\x0f') - self.assertEqual(ircutils.unColor('\x03foo\x03'), 'foo') - self.assertEqual(ircutils.unColor('\x03foo\x0F'), 'foo\x0F') - self.assertEqual(ircutils.unColor('\x0312foo\x03'), 'foo') - self.assertEqual(ircutils.unColor('\x0312,14foo\x03'), 'foo') - self.assertEqual(ircutils.unColor('\x03,14foo\x03'), 'foo') - self.assertEqual(ircutils.unColor('\x03,foo\x03'), ',foo') - self.assertEqual(ircutils.unColor('\x0312foo\x0F'), 'foo\x0F') - self.assertEqual(ircutils.unColor('\x0312,14foo\x0F'), 'foo\x0F') - self.assertEqual(ircutils.unColor('\x03,14foo\x0F'), 'foo\x0F') - self.assertEqual(ircutils.unColor('\x03,foo\x0F'), ',foo\x0F') - def testDccIpStuff(self): def randomIP(): def rand(): @@ -249,6 +278,7 @@ class IrcDictTestCase(SupyTestCase): self.failUnless(d == copy.copy(d)) self.failUnless(d == copy.deepcopy(d)) + class IrcSetTestCase(SupyTestCase): def test(self): s = ircutils.IrcSet() @@ -279,7 +309,6 @@ class IrcSetTestCase(SupyTestCase): self.failIf('foo' in s1) self.failIf('FOo' in s1) - class IrcStringTestCase(SupyTestCase): def testEquality(self):