ircutils.strip{Bold,Reverse,Underline,Formatting}

This commit is contained in:
James Vega 2004-04-15 21:15:57 +00:00
parent 38fee5b2dd
commit e87c31aea4
3 changed files with 70 additions and 17 deletions

View File

@ -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.

View File

@ -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

View File

@ -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):