Move utility functions to mopidy.utils
This commit is contained in:
parent
749e3ef281
commit
b4bc81e19e
@ -1,35 +1,11 @@
|
||||
import logging
|
||||
from multiprocessing.reduction import reduce_connection
|
||||
import pickle
|
||||
|
||||
from mopidy import settings as raw_settings
|
||||
|
||||
logger = logging.getLogger('mopidy')
|
||||
|
||||
def get_version():
|
||||
return u'0.1.dev'
|
||||
|
||||
def get_mpd_protocol_version():
|
||||
return u'0.16.0'
|
||||
|
||||
def get_class(name):
|
||||
module_name = name[:name.rindex('.')]
|
||||
class_name = name[name.rindex('.') + 1:]
|
||||
logger.info('Loading: %s from %s', class_name, module_name)
|
||||
module = __import__(module_name, globals(), locals(), [class_name], -1)
|
||||
class_object = getattr(module, class_name)
|
||||
return class_object
|
||||
|
||||
def pickle_connection(connection):
|
||||
return pickle.dumps(reduce_connection(connection))
|
||||
|
||||
def unpickle_connection(pickled_connection):
|
||||
# From http://stackoverflow.com/questions/1446004
|
||||
unpickled = pickle.loads(pickled_connection)
|
||||
func = unpickled[0]
|
||||
args = unpickled[1]
|
||||
return func(*args)
|
||||
|
||||
class SettingsError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@ -7,8 +7,9 @@ import sys
|
||||
sys.path.insert(0,
|
||||
os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
|
||||
|
||||
from mopidy import get_class, settings, SettingsError
|
||||
from mopidy import settings, SettingsError
|
||||
from mopidy.core import CoreProcess
|
||||
from mopidy.utils import get_class
|
||||
|
||||
logger = logging.getLogger('mopidy.main')
|
||||
|
||||
|
||||
@ -3,8 +3,9 @@ import logging
|
||||
import random
|
||||
import time
|
||||
|
||||
from mopidy import get_class, settings
|
||||
from mopidy import settings
|
||||
from mopidy.models import Playlist
|
||||
from mopidy.utils import get_class
|
||||
|
||||
logger = logging.getLogger('mopidy.backends.base')
|
||||
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import logging
|
||||
import multiprocessing
|
||||
|
||||
from mopidy import get_class, settings, unpickle_connection
|
||||
from mopidy import settings
|
||||
from mopidy.utils import get_class, unpickle_connection
|
||||
|
||||
logger = logging.getLogger('mopidy.core')
|
||||
|
||||
|
||||
@ -16,6 +16,7 @@ import re
|
||||
import sys
|
||||
|
||||
from mopidy.mpd import MpdAckError, MpdNotImplemented
|
||||
from mopidy.utils import flatten
|
||||
|
||||
logger = logging.getLogger('mopidy.mpd.frontend')
|
||||
|
||||
@ -49,15 +50,6 @@ def handle_pattern(pattern):
|
||||
return func
|
||||
return decorator
|
||||
|
||||
def flatten(the_list):
|
||||
result = []
|
||||
for element in the_list:
|
||||
if isinstance(element, list):
|
||||
result.extend(flatten(element))
|
||||
else:
|
||||
result.append(element)
|
||||
return result
|
||||
|
||||
class MpdFrontend(object):
|
||||
def __init__(self, backend=None):
|
||||
self.backend = backend
|
||||
|
||||
@ -9,14 +9,16 @@ import multiprocessing
|
||||
import socket
|
||||
import sys
|
||||
|
||||
from mopidy import get_mpd_protocol_version, pickle_connection, settings
|
||||
from mopidy import get_mpd_protocol_version, settings
|
||||
from mopidy.mpd import MpdAckError
|
||||
from mopidy.utils import indent, pickle_connection
|
||||
|
||||
logger = logging.getLogger('mopidy.mpd.server')
|
||||
|
||||
#: All data between the client and the server is encoded in UTF-8.
|
||||
#: The MPD protocol uses UTF-8 for encoding all data.
|
||||
ENCODING = u'utf-8'
|
||||
|
||||
#: The MPD protocol uses ``\n`` as line terminator.
|
||||
LINE_TERMINATOR = u'\n'
|
||||
|
||||
class MpdServer(asyncore.dispatcher):
|
||||
@ -87,13 +89,3 @@ class MpdSession(asynchat.async_chat):
|
||||
output = u'%s%s' % (output, LINE_TERMINATOR)
|
||||
data = output.encode(ENCODING)
|
||||
self.push(data)
|
||||
|
||||
|
||||
def indent(string, places=4, linebreak=LINE_TERMINATOR):
|
||||
lines = string.split(linebreak)
|
||||
if len(lines) == 1:
|
||||
return string
|
||||
result = u''
|
||||
for line in lines:
|
||||
result += linebreak + ' ' * places + line
|
||||
return result
|
||||
|
||||
41
mopidy/utils.py
Normal file
41
mopidy/utils.py
Normal file
@ -0,0 +1,41 @@
|
||||
import logging
|
||||
from multiprocessing.reduction import reduce_connection
|
||||
import pickle
|
||||
|
||||
logger = logging.getLogger('mopidy.utils')
|
||||
|
||||
def flatten(the_list):
|
||||
result = []
|
||||
for element in the_list:
|
||||
if isinstance(element, list):
|
||||
result.extend(flatten(element))
|
||||
else:
|
||||
result.append(element)
|
||||
return result
|
||||
|
||||
def get_class(name):
|
||||
module_name = name[:name.rindex('.')]
|
||||
class_name = name[name.rindex('.') + 1:]
|
||||
logger.info('Loading: %s', name)
|
||||
module = __import__(module_name, globals(), locals(), [class_name], -1)
|
||||
class_object = getattr(module, class_name)
|
||||
return class_object
|
||||
|
||||
def indent(string, places=4, linebreak='\n'):
|
||||
lines = string.split(linebreak)
|
||||
if len(lines) == 1:
|
||||
return string
|
||||
result = u''
|
||||
for line in lines:
|
||||
result += linebreak + ' ' * places + line
|
||||
return result
|
||||
|
||||
def pickle_connection(connection):
|
||||
return pickle.dumps(reduce_connection(connection))
|
||||
|
||||
def unpickle_connection(pickled_connection):
|
||||
# From http://stackoverflow.com/questions/1446004
|
||||
unpickled = pickle.loads(pickled_connection)
|
||||
func = unpickled[0]
|
||||
args = unpickled[1]
|
||||
return func(*args)
|
||||
Loading…
Reference in New Issue
Block a user