Convert bdp: pass around with static setter on classes

This commit is contained in:
Bruno Cauet 2015-01-06 11:26:15 +01:00
parent 32673b87e7
commit 74e18afa94

View file

@ -71,7 +71,6 @@ SAFE_COMMANDS = (
ITEM_KEYS_WRITABLE = set(MediaFile.fields()).intersection(Item._fields.keys())
# Loggers.
log = logging.getLogger(__name__)
global_log = logging.getLogger('beets')
@ -554,6 +553,8 @@ class Connection(object):
"""A connection between a client and the server. Handles input and
output from and to the client.
"""
_log = None
def __init__(self, server, sock):
"""Create a new connection for the accepted socket `client`.
"""
@ -561,6 +562,10 @@ class Connection(object):
self.sock = sock
self.authenticated = False
@classmethod
def set_logger(cls, logger):
cls._log = logger
def send(self, lines):
"""Send lines, which which is either a single string or an
iterable consisting of strings, to the client. A newline is
@ -570,7 +575,7 @@ class Connection(object):
if isinstance(lines, basestring):
lines = [lines]
out = NEWLINE.join(lines) + NEWLINE
log.debug(out[:-1]) # Don't log trailing newline.
self._log.debug(out[:-1]) # Don't log trailing newline.
if isinstance(out, unicode):
out = out.encode('utf8')
return self.sock.sendall(out)
@ -601,7 +606,7 @@ class Connection(object):
line = line.strip()
if not line:
break
log.debug(line)
self._log.debug(line)
if clist is not None:
# Command list already opened.
@ -639,6 +644,7 @@ class Command(object):
command_re = re.compile(r'^([^ \t]+)[ \t]*')
arg_re = re.compile(r'"((?:\\"|[^"])+)"|([^ \t"]+)')
_log = None
def __init__(self, s):
"""Creates a new `Command` from the given string, `s`, parsing
@ -660,6 +666,10 @@ class Command(object):
arg = arg.decode('utf8')
self.args.append(arg)
@classmethod
def set_logger(cls, logger):
cls._log = logger
def run(self, conn):
"""A coroutine that executes the command on the given
connection.
@ -696,7 +706,7 @@ class Command(object):
except Exception as e:
# An "unintentional" error. Hide it from the client.
log.error(traceback.format_exc(e))
self._log.error(traceback.format_exc(e))
raise BPDError(ERROR_SYSTEM, u'server error', self.name)
@ -1151,13 +1161,15 @@ class BPDPlugin(BeetsPlugin):
'password': u'',
'volume': VOLUME_MAX,
})
Connection.set_logger(self._log)
Server.set_logger(self._log)
def start_bpd(self, lib, host, port, password, volume, debug):
"""Starts a BPD server."""
if debug:
log.setLevel(logging.DEBUG)
if debug: # FIXME this should be managed by BeetsPlugin
self._log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.WARNING)
self._log.setLevel(logging.WARNING)
try:
server = Server(lib, host, port, password)
server.cmd_setvol(None, volume)