Handle encoding at the borders and only use unicode objects internally

This commit is contained in:
Stein Magnus Jodal 2009-12-23 20:19:10 +01:00
parent 4605a9e8c9
commit ecd5e686f6
4 changed files with 23 additions and 12 deletions

View File

@ -1,2 +1,2 @@
def get_version():
return '0'
return u'0'

View File

@ -5,7 +5,7 @@ import socket
from mopidy import settings
from mopidy.session import MpdSession
logger = logging.getLogger('server')
logger = logging.getLogger(u'server')
class MpdServer(asyncore.dispatcher):
def __init__(self, handler_class=MpdSession):
@ -18,7 +18,7 @@ class MpdServer(asyncore.dispatcher):
def handle_accept(self):
(client_socket, client_address) = self.accept()
logger.info('Connection from: [%s]:%s', *client_address)
logger.info(u'Connection from: [%s]:%s', *client_address)
self.handler_class(client_socket, client_address)
def handle_close(self):

View File

@ -3,21 +3,31 @@ import logging
from mopidy import get_version, settings
logger = logging.getLogger('session')
logger = logging.getLogger(u'session')
class MpdSession(asynchat.async_chat):
def __init__(self, client_socket, client_address):
asynchat.async_chat.__init__(self, sock=client_socket)
self.input_buffer = []
self.set_terminator(settings.LINE_TERMINATOR)
self.respond('OK MPD (mopidy %s)' % get_version())
self.set_terminator(settings.MPD_LINE_TERMINATOR)
self.send_response(u'OK MPD (mopidy %s)' % get_version())
def collect_incoming_data(self, data):
self.input_buffer.append(data)
def found_terminator(self):
logger.debug('Input: %s', ''.join(self.input_buffer))
data = ''.join(self.input_buffer)
self.input_buffer = []
input = data.decode(settings.MPD_LINE_ENCODING)
logger.debug(u'Input: %s', input)
self.handle_request(input)
def handle_request(self, input):
pass # TODO
def send_response(self, output):
logger.debug(u'Output: %s', output)
output = u'%s%s' % (output, settings.MPD_LINE_TERMINATOR)
data = output.encode(settings.MPD_LINE_ENCODING)
self.push(data)
def respond(self, data):
self.push('%s%s' % (data, settings.LINE_TERMINATOR))

View File

@ -1,4 +1,5 @@
CONSOLE_LOG_FORMAT = '%(levelname)-8s %(asctime)s\n %(message)s'
LINE_TERMINATOR = '\n'
MPD_SERVER_HOSTNAME = 'localhost'
CONSOLE_LOG_FORMAT = u'%(levelname)-8s %(asctime)s\n %(message)s'
MPD_LINE_ENCODING = u'utf-8'
MPD_LINE_TERMINATOR = u'\n'
MPD_SERVER_HOSTNAME = u'localhost'
MPD_SERVER_PORT = 6600