Make src/callbacks.py more py3k-friendly.

This commit is contained in:
Valentin Lorentz 2012-08-04 11:38:12 +02:00
parent fa35a07941
commit 139f5b4943
2 changed files with 14 additions and 12 deletions

View File

@ -35,6 +35,7 @@ This module contains the basic callbacks for handling PRIVMSGs.
import supybot import supybot
import re import re
import sys
import copy import copy
import time import time
import shlex import shlex
@ -151,14 +152,16 @@ def canonicalName(command):
Currently, this makes everything lowercase and removes all dashes and Currently, this makes everything lowercase and removes all dashes and
underscores. underscores.
""" """
if isinstance(command, unicode): if sys.version_info[0] < 3 and isinstance(command, unicode):
command = command.encode('utf-8') command = command.encode('utf-8')
elif sys.version_info[0] >= 3 and isinstance(command, bytes):
command = command.decode()
special = '\t -_' special = '\t -_'
reAppend = '' reAppend = ''
while command and command[-1] in special: while command and command[-1] in special:
reAppend = command[-1] + reAppend reAppend = command[-1] + reAppend
command = command[:-1] command = command[:-1]
return command.translate(utils.str.chars, special).lower() + reAppend return ''.join([x for x in command if x not in special]).lower() + reAppend
def reply(msg, s, prefixNick=None, private=None, def reply(msg, s, prefixNick=None, private=None,
notice=None, to=None, action=None, error=False): notice=None, to=None, action=None, error=False):
@ -262,10 +265,10 @@ class Tokenizer(object):
# #
# These are the characters valid in a token. Everything printable except # These are the characters valid in a token. Everything printable except
# double-quote, left-bracket, and right-bracket. # double-quote, left-bracket, and right-bracket.
validChars = utils.str.chars.translate(utils.str.chars, '\x00\r\n \t') separators = '\x00\r\n \t'
def __init__(self, brackets='', pipe=False, quotes='"'): def __init__(self, brackets='', pipe=False, quotes='"'):
if brackets: if brackets:
self.validChars=self.validChars.translate(utils.str.chars, brackets) self.separators += brackets
self.left = brackets[0] self.left = brackets[0]
self.right = brackets[1] self.right = brackets[1]
else: else:
@ -273,15 +276,15 @@ class Tokenizer(object):
self.right = '' self.right = ''
self.pipe = pipe self.pipe = pipe
if self.pipe: if self.pipe:
self.validChars = self.validChars.translate(utils.str.chars, '|') self.separators += '|'
self.quotes = quotes self.quotes = quotes
self.validChars = self.validChars.translate(utils.str.chars, quotes) self.separators += quotes
def _handleToken(self, token): def _handleToken(self, token):
if token[0] == token[-1] and token[0] in self.quotes: if token[0] == token[-1] and token[0] in self.quotes:
token = token[1:-1] token = token[1:-1]
token = token.decode('string_escape') token = token.encode().decode('string_escape')
return token return token
def _insideBrackets(self, lexer): def _insideBrackets(self, lexer):
@ -307,7 +310,7 @@ class Tokenizer(object):
lexer = shlex.shlex(StringIO(s)) lexer = shlex.shlex(StringIO(s))
lexer.commenters = '' lexer.commenters = ''
lexer.quotes = self.quotes lexer.quotes = self.quotes
lexer.wordchars = self.validChars lexer.separators = self.separators
args = [] args = []
ends = [] ends = []
while True: while True:

View File

@ -19,9 +19,8 @@ class shlex:
self.instream = sys.stdin self.instream = sys.stdin
self.infile = None self.infile = None
self.commenters = '#' self.commenters = '#'
self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
self.whitespace = ' \t\r\n' self.whitespace = ' \t\r\n'
self.separators = self.whitespace
self.quotes = '\'"' self.quotes = '\'"'
self.state = ' ' self.state = ' '
self.pushback = [] self.pushback = []
@ -121,7 +120,7 @@ class shlex:
elif nextchar in self.commenters: elif nextchar in self.commenters:
self.instream.readline() self.instream.readline()
self.lineno = self.lineno + 1 self.lineno = self.lineno + 1
elif nextchar in self.wordchars: elif nextchar not in self.separators:
self.token = nextchar self.token = nextchar
self.state = 'a' self.state = 'a'
elif nextchar in self.quotes: elif nextchar in self.quotes:
@ -166,7 +165,7 @@ class shlex:
elif nextchar in self.commenters: elif nextchar in self.commenters:
self.instream.readline() self.instream.readline()
self.lineno = self.lineno + 1 self.lineno = self.lineno + 1
elif nextchar in self.wordchars or nextchar in self.quotes: elif nextchar not in self.separators or nextchar in self.quotes:
self.token = self.token + nextchar self.token = self.token + nextchar
else: else:
self.pushback = [nextchar] + self.pushback self.pushback = [nextchar] + self.pushback