From 139f5b4943e5fdfc7afc8f94730c2067c6c65c9f Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 4 Aug 2012 11:38:12 +0200 Subject: [PATCH] Make src/callbacks.py more py3k-friendly. --- src/callbacks.py | 19 +++++++++++-------- src/shlex.py | 7 +++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index 2db9c9874..d34867525 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -35,6 +35,7 @@ This module contains the basic callbacks for handling PRIVMSGs. import supybot import re +import sys import copy import time import shlex @@ -151,14 +152,16 @@ def canonicalName(command): Currently, this makes everything lowercase and removes all dashes and underscores. """ - if isinstance(command, unicode): + if sys.version_info[0] < 3 and isinstance(command, unicode): command = command.encode('utf-8') + elif sys.version_info[0] >= 3 and isinstance(command, bytes): + command = command.decode() special = '\t -_' reAppend = '' while command and command[-1] in special: reAppend = command[-1] + reAppend 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, 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 # 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='"'): if brackets: - self.validChars=self.validChars.translate(utils.str.chars, brackets) + self.separators += brackets self.left = brackets[0] self.right = brackets[1] else: @@ -273,15 +276,15 @@ class Tokenizer(object): self.right = '' self.pipe = pipe if self.pipe: - self.validChars = self.validChars.translate(utils.str.chars, '|') + self.separators += '|' self.quotes = quotes - self.validChars = self.validChars.translate(utils.str.chars, quotes) + self.separators += quotes def _handleToken(self, token): if token[0] == token[-1] and token[0] in self.quotes: token = token[1:-1] - token = token.decode('string_escape') + token = token.encode().decode('string_escape') return token def _insideBrackets(self, lexer): @@ -307,7 +310,7 @@ class Tokenizer(object): lexer = shlex.shlex(StringIO(s)) lexer.commenters = '' lexer.quotes = self.quotes - lexer.wordchars = self.validChars + lexer.separators = self.separators args = [] ends = [] while True: diff --git a/src/shlex.py b/src/shlex.py index 34a809cf0..eb447a725 100644 --- a/src/shlex.py +++ b/src/shlex.py @@ -19,9 +19,8 @@ class shlex: self.instream = sys.stdin self.infile = None self.commenters = '#' - self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' - 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') self.whitespace = ' \t\r\n' + self.separators = self.whitespace self.quotes = '\'"' self.state = ' ' self.pushback = [] @@ -121,7 +120,7 @@ class shlex: elif nextchar in self.commenters: self.instream.readline() self.lineno = self.lineno + 1 - elif nextchar in self.wordchars: + elif nextchar not in self.separators: self.token = nextchar self.state = 'a' elif nextchar in self.quotes: @@ -166,7 +165,7 @@ class shlex: elif nextchar in self.commenters: self.instream.readline() 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 else: self.pushback = [nextchar] + self.pushback