コード例 #1
0
ファイル: __init__.py プロジェクト: emou/duck
 def initialize(self):
     self._connect()
     self._changes = None
     self.idle_request = Event()
     self.idle_request.clear()
     self.idle_thread = IdleThread(self)
     self.idle_thread.start()
コード例 #2
0
ファイル: __init__.py プロジェクト: emou/duck
class Backend(BaseBackend):
    """
    A backend that talks to an MPD server.
    """

    default_options = {
        'host': 'localhost',
        'port': 6600,
    }

    def __init__(self, *args, **kwargs):
        options = kwargs.pop('options', None)
        BaseBackend.__init__(self, *args, **kwargs)
        self.options = Backend.default_options.copy()
        self._last_status = None
        self.status = None
        if options:
            self.options.update(options)

    def initialize(self):
        self._connect()
        self._changes = None
        self.idle_request = Event()
        self.idle_request.clear()
        self.idle_thread = IdleThread(self)
        self.idle_thread.start()

    def _connect(self):
        self.client = mpd.MPDClient()
        try:
            self.client.connect(self.options['host'], self.options['port'])
        except (mpd.MPDError, socket.error) as e:
            msg = [
                'Could not connect to MPD Server at %(host)s:%(port)s.' % self.options,
                '%s.' % e,
            ]
            raise BackendInitializeError('\n'.join(msg))

    def __enter__(self):
        try:
            self._changes = self._noidle()
        except mpd.MPDConnectionError, e:
            raise BackendConnectionError("MPDConnection error: %s" % e)
        return self