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

View File

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

View File

@ -95,7 +95,7 @@ class Format(callbacks.Plugin):
if len(bad) != len(good): if len(bad) != len(good):
irc.error(_('<chars to translate> must be the same length as ' irc.error(_('<chars to translate> must be the same length as '
'<chars to replace those with>.'), Raise=True) '<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']) translate = wrap(translate, ['something', 'something', 'text'])
@internationalizeDocstring @internationalizeDocstring

View File

@ -51,6 +51,7 @@ import re
import math import math
import string import string
import supybot.utils as utils
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
from supybot.commands import wrap, additional from supybot.commands import wrap, additional
from supybot.i18n import PluginInternationalization, internationalizeDocstring from supybot.i18n import PluginInternationalization, internationalizeDocstring
@ -116,11 +117,12 @@ class Nickometer(callbacks.Plugin):
('\\[rkx]0', 1000), ('\\[rkx]0', 1000),
('\\0[rkx]', 1000)] ('\\0[rkx]', 1000)]
letterNumberTranslator = string.maketrans('023457+8', 'ozeasttb') letterNumberTranslator = utils.str.MultipleReplacer(dict(zip(
'023457+8', 'ozeasttb')))
for special in specialCost: for special in specialCost:
tempNick = nick tempNick = nick
if special[0][0] != '\\': if special[0][0] != '\\':
tempNick = tempNick.translate(letterNumberTranslator) tempNick = letterNumberTranslator(tempNick)
if tempNick and re.search(special[0], tempNick, re.IGNORECASE): if tempNick and re.search(special[0], tempNick, re.IGNORECASE):
score += self.punish(special[1], 'matched special case /%s/' % 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 assert nick and ident and host
return intern('%s!%s@%s' % (nick, ident, host)) return intern('%s!%s@%s' % (nick, ident, host))
_rfc1459trans = string.maketrans(string.ascii_uppercase + r'\[]~', _rfc1459trans = utils.str.MultipleReplacer(dict(zip(
string.ascii_lowercase + r'|{}^') string.ascii_uppercase + r'\[]~',
string.ascii_lowercase + r'|{}^')))
def toLower(s, casemapping=None): def toLower(s, casemapping=None):
"""s => s """s => s
Returns the string s lowered according to IRC case rules.""" Returns the string s lowered according to IRC case rules."""
if casemapping is None or casemapping == 'rfc1459': if casemapping is None or casemapping == 'rfc1459':
return s.translate(_rfc1459trans) return _rfc1459trans(s)
elif casemapping == 'ascii': # freenode elif casemapping == 'ascii': # freenode
return s.lower() return s.lower()
else: else: