Use utils.str.MultipleReplacer instead of str.maketrans.

This commit is contained in:
Valentin Lorentz 2012-08-04 14:04:15 +02:00
parent b17228d683
commit df2d976818
5 changed files with 15 additions and 11 deletions

View File

@ -649,7 +649,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
change = wrap(change, ['channel', 'something',
'factoidId', 'regexpReplacer'])
_sqlTrans = string.maketrans('*?', '%_')
_sqlTrans = utils.str.MultipleReplacer({'*': '%', '?': '_'})
@internationalizeDocstring
def search(self, irc, msg, args, channel, optlist, globs):
"""[<channel>] [--values] [--{regexp} <value>] [<glob> ...]
@ -681,7 +681,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
predicateName += 'p'
for glob in globs:
criteria.append('TARGET LIKE ?')
formats.append(glob.translate(self._sqlTrans))
formats.append(self._sqlTrans(glob))
cursor = db.cursor()
sql = """SELECT keys.key FROM %s WHERE %s""" % \
(', '.join(tables), ' AND '.join(criteria))

View File

@ -232,7 +232,8 @@ class Filter(callbacks.Plugin):
irc.reply(text)
lithp = wrap(lithp, ['text'])
_leettrans = string.maketrans('oOaAeElBTiIts', '004433187!1+5')
_leettrans = utils.str.MultipleReplacer(dict(zip('oOaAeElBTiIts',
'004433187!1+5')))
_leetres = [(re.compile(r'\b(?:(?:[yY][o0O][oO0uU])|u)\b'), 'j00'),
(re.compile(r'fear'), 'ph33r'),
(re.compile(r'[aA][tT][eE]'), '8'),
@ -247,7 +248,7 @@ class Filter(callbacks.Plugin):
"""
for (r, sub) in self._leetres:
text = re.sub(r, sub, text)
text = text.translate(self._leettrans)
text = self._leettrans(text)
irc.reply(text)
leet = wrap(leet, ['text'])
@ -648,7 +649,7 @@ class Filter(callbacks.Plugin):
irc.reply(text)
shrink = wrap(shrink, ['text'])
_azn_trans = string.maketrans('rlRL', 'lrLR')
_azn_trans = utils.str.MultipleReplacer(dict(zip('rlRL', 'lrLR')))
@internationalizeDocstring
def azn(self, irc, msg, args, text):
"""<text>

View File

@ -95,7 +95,7 @@ class Format(callbacks.Plugin):
if len(bad) != len(good):
irc.error(_('<chars to translate> must be the same length as '
'<chars to replace those with>.'), Raise=True)
irc.reply(text.translate(string.maketrans(bad, good)))
irc.reply(utils.str.MultipleReplacer(dict(zip(bad, good)))(text))
translate = wrap(translate, ['something', 'something', 'text'])
@internationalizeDocstring

View File

@ -51,6 +51,7 @@ import re
import math
import string
import supybot.utils as utils
import supybot.callbacks as callbacks
from supybot.commands import wrap, additional
from supybot.i18n import PluginInternationalization, internationalizeDocstring
@ -116,11 +117,12 @@ class Nickometer(callbacks.Plugin):
('\\[rkx]0', 1000),
('\\0[rkx]', 1000)]
letterNumberTranslator = string.maketrans('023457+8', 'ozeasttb')
letterNumberTranslator = utils.str.MultipleReplacer(dict(zip(
'023457+8', 'ozeasttb')))
for special in specialCost:
tempNick = nick
if special[0][0] != '\\':
tempNick = tempNick.translate(letterNumberTranslator)
tempNick = letterNumberTranslator(tempNick)
if tempNick and re.search(special[0], tempNick, re.IGNORECASE):
score += self.punish(special[1], 'matched special case /%s/' %

View File

@ -91,13 +91,14 @@ def joinHostmask(nick, ident, host):
assert nick and ident and host
return intern('%s!%s@%s' % (nick, ident, host))
_rfc1459trans = string.maketrans(string.ascii_uppercase + r'\[]~',
string.ascii_lowercase + r'|{}^')
_rfc1459trans = utils.str.MultipleReplacer(dict(zip(
string.ascii_uppercase + r'\[]~',
string.ascii_lowercase + r'|{}^')))
def toLower(s, casemapping=None):
"""s => s
Returns the string s lowered according to IRC case rules."""
if casemapping is None or casemapping == 'rfc1459':
return s.translate(_rfc1459trans)
return _rfc1459trans(s)
elif casemapping == 'ascii': # freenode
return s.lower()
else: