2 # Copyright (C) 2004, 2005, 2007 Richard Kettlewell
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 """Python support for DisOrder
22 Provides disorder.client, a class for accessing a DisOrder server.
26 #! /usr/bin/env python
35 #! /usr/bin/env python
39 for path in sys.argv[1:]:
42 See disorder_protocol(5) for details of the communication protocol.
44 NB that this code only supports servers configured to use SHA1-based
45 authentication. If the server demands another hash then it will not be
46 possible to use this module.
59 _configfile = "pkgconfdir/config"
60 _dbhome = "pkgstatedir"
63 # various regexps we'll use
64 _ws = re.compile(r"^[ \t\n\r]+")
65 _squote = re.compile("'(([^\\\\']|\\\\[\\\\\"'n])+)'")
66 _dquote = re.compile("\"(([^\\\\\"]|\\\\[\\\\\"'n])+)\"")
67 _unquoted = re.compile("[^\"' \\t\\n\\r][^ \t\n\r]*")
69 _response = re.compile("([0-9]{3}) ?(.*)")
73 ########################################################################
76 class Error(Exception):
77 """Base class for DisOrder exceptions."""
79 class _splitError(Error):
81 def __init__(self, value):
84 return str(self.value)
86 class parseError(Error):
87 """Error parsing the configuration file."""
88 def __init__(self, path, line, details):
91 self.details = details
93 return "%s:%d: %s" % (self.path, self.line, self.details)
95 class protocolError(Error):
96 """DisOrder control protocol error.
98 Indicates a mismatch between the client and server's understanding of
101 def __init__(self, who, error):
105 return "%s: %s" % (self.who, str(self.error))
107 class operationError(Error):
108 """DisOrder control protocol error response.
110 Indicates that an operation failed (e.g. an attempt to play a
111 nonexistent track). The connection should still be usable.
113 def __init__(self, res, details, cmd=None):
116 self.details_ = details
118 """Return the complete response string from the server, with the command
121 Excludes the final newline.
123 if self.cmd_ is None:
124 return "%d %s" % (self.res_, self.details_)
126 return "%d %s [%s]" % (self.res_, self.details_, self.cmd_)
128 """Return the response code from the server."""
131 """Returns the detail string from the server."""
134 class communicationError(Error):
135 """DisOrder control protocol communication error.
137 Indicates that communication with the server went wrong, perhaps
138 because the server was restarted. The caller could report an error to
139 the user and wait for further user instructions, or even automatically
142 def __init__(self, who, error):
146 return "%s: %s" % (self.who, str(self.error))
148 ########################################################################
149 # DisOrder-specific text processing
152 # Unescape the contents of a string
156 # s -- string to unescape
158 s = re.sub("\\\\n", "\n", s)
159 s = re.sub("\\\\(.)", "\\1", s)
162 def _split(s, *comments):
163 # Split a string into fields according to the usual Disorder string splitting
168 # s -- string to parse
169 # comments -- if present, parse comments
173 # On success, a list of fields is returned.
175 # On error, disorder.parseError is thrown.
180 if comments and s[0] == '#':
187 # pick of quoted fields of both kinds
192 fields.append(_unescape(m.group(1)))
195 # and unquoted fields
196 m = _unquoted.match(s)
198 fields.append(m.group(0))
201 # anything left must be in error
202 if s[0] == '"' or s[0] == '\'':
203 raise _splitError("invalid quoted string")
205 raise _splitError("syntax error")
209 # Escape the contents of a string
213 # s -- string to escape
215 if re.search("[\\\\\"'\n \t\r]", s) or s == '':
216 s = re.sub(r'[\\"]', r'\\\g<0>', s)
217 s = re.sub("\n", r"\\n", s)
223 # Quote a list of values
224 return ' '.join(map(_escape, list))
227 # Return the value of s in a form suitable for writing to stderr
228 return s.encode(locale.nl_langinfo(locale.CODESET), 'replace')
231 # Convert a list of the form [k1, v1, k2, v2, ..., kN, vN]
232 # to a dictionary {k1:v1, k2:v2, ..., kN:vN}
240 except StopIteration:
245 # parse a queue entry
246 return _list2dict(_split(s))
248 ########################################################################
252 """DisOrder client class.
254 This class provides access to the DisOrder server either on this
255 machine or across the internet.
257 The server to connect to, and the username and password to use, are
258 determined from the configuration files as described in 'man
261 All methods will connect if necessary, as soon as you have a
262 disorder.client object you can start calling operational methods on
265 However if the server is restarted then the next method called on a
266 connection will throw an exception. This may be considered a bug.
268 All methods block until they complete.
270 Operation methods raise communicationError if the connection breaks,
271 protocolError if the response from the server is malformed, or
272 operationError if the response is valid but indicates that the
279 def __init__(self, user=None, password=None):
280 """Constructor for DisOrder client class.
282 The constructor reads the configuration file, but does not connect
285 If the environment variable DISORDER_PYTHON_DEBUG is set then the
286 debug flags are initialised to that value. This can be overridden
287 with the debug() method below.
289 The constructor Raises parseError() if the configuration file is not
292 pw = pwd.getpwuid(os.getuid())
293 self.debugging = int(os.getenv("DISORDER_PYTHON_DEBUG", 0))
294 self.config = { 'collections': [],
295 'username': pw.pw_name,
298 self.password = password
299 home = os.getenv("HOME")
302 privconf = _configfile + "." + pw.pw_name
303 passfile = home + os.sep + ".disorder" + os.sep + "passwd"
304 if os.path.exists(_configfile):
305 self._readfile(_configfile)
306 if os.path.exists(privconf):
307 self._readfile(privconf)
308 if os.path.exists(passfile) and _userconf:
309 self._readfile(passfile)
310 self.state = 'disconnected'
312 def debug(self, bits):
313 """Enable or disable protocol debugging. Debug messages are written
317 bits -- bitmap of operations that should generate debug information
320 debug_proto -- dump control protocol messages (excluding bodies)
321 debug_body -- dump control protocol message bodies
323 self.debugging = bits
325 def _debug(self, bit, s):
327 if self.debugging & bit:
328 sys.stderr.write(_sanitize(s))
329 sys.stderr.write("\n")
332 def connect(self, cookie=None):
333 """c.connect(cookie=None)
335 Connect to the DisOrder server and authenticate.
337 Raises communicationError if connection fails and operationError if
338 authentication fails (in which case disconnection is automatic).
340 May be called more than once to retry connections (e.g. when the
341 server is down). If we are already connected and authenticated,
344 Other operations automatically connect if we're not already
345 connected, so it is not strictly necessary to call this method.
347 If COOKIE is specified then that is used to log in instead of
348 the username/password.
350 if self.state == 'disconnected':
352 self.state = 'connecting'
353 if 'connect' in self.config and len(self.config['connect']) > 0:
354 c = self.config['connect']
355 self.who = repr(c) # temporarily
357 a = socket.getaddrinfo(None, c[0],
363 a = socket.getaddrinfo(c[0], c[1],
369 s = socket.socket(a[0], a[1], a[2]);
371 self.who = "%s" % a[3]
373 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM);
374 self.who = self.config['home'] + os.sep + "socket"
376 self.w = s.makefile("wb")
377 self.r = s.makefile("rb")
378 (res, challenge_and_algo) = self._simple()
379 (algo, challenge) = _split(challenge_and_algo)
381 if self.user is None:
382 user = self.config['username']
385 if self.password is None:
386 password = self.config['password']
388 password = self.password
389 # TODO support algorithms other than SHA-1
392 h.update(binascii.unhexlify(challenge))
393 self._simple("user", user, h.hexdigest())
395 self._simple("cookie", cookie)
396 self.state = 'connected'
397 except socket.error, e:
399 raise communicationError(self.who, e)
404 def _disconnect(self):
405 # disconnect from the server, whatever state we are in
411 self.state = 'disconnected'
413 ########################################################################
416 def play(self, track):
420 track -- the path of the track to play.
422 Returns the ID of the new queue entry.
424 Note that queue IDs are unicode strings (because all track information
425 values are unicode strings).
427 res, details = self._simple("play", track)
428 return unicode(details) # because it's unicode in queue() output
430 def remove(self, track):
431 """Remove a track from the queue.
434 track -- the path or ID of the track to remove.
436 self._simple("remove", track)
439 """Enable playing."""
440 self._simple("enable")
442 def disable(self, *now):
446 now -- if present (with any value), the current track is stopped
450 self._simple("disable", "now")
452 self._simple("disable")
454 def scratch(self, *id):
455 """Scratch the currently playing track.
458 id -- if present, the ID of the track to scratch.
461 self._simple("scratch", id[0])
463 self._simple("scratch")
466 """Shut down the server.
468 Only trusted users can perform this operation.
470 self._simple("shutdown")
472 def reconfigure(self):
473 """Make the server reload its configuration.
475 Only trusted users can perform this operation.
477 self._simple("reconfigure")
479 def rescan(self, pattern):
480 """Rescan one or more collections.
483 pattern -- glob pattern matching collections to rescan.
485 Only trusted users can perform this operation.
487 self._simple("rescan", pattern)
490 """Return the server's version number."""
491 return self._simple("version")[1]
494 """Return the currently playing track.
496 If a track is playing then it is returned as a dictionary. See
497 disorder_protocol(5) for the meanings of the keys. All keys are
498 plain strings but the values will be unicode strings.
500 If no track is playing then None is returned."""
501 res, details = self._simple("playing")
504 return _queueEntry(details)
505 except _splitError, s:
506 raise protocolError(self.who, s.str())
510 def _somequeue(self, command):
511 self._simple(command)
513 return map(lambda s: _queueEntry(s), self._body())
514 except _splitError, s:
515 raise protocolError(self.who, s.str())
518 """Return a list of recently played tracks.
520 The return value is a list of dictionaries corresponding to
521 recently played tracks. The oldest track comes first.
523 See disorder_protocol(5) for the meanings of the keys. All keys are
524 plain strings but the values will be unicode strings."""
525 return self._somequeue("recent")
528 """Return the current queue.
530 The return value is a list of dictionaries corresponding to
531 recently played tracks. The next track to be played comes first.
533 See disorder_protocol(5) for the meanings of the keys. All keys are
534 plain strings but the values will be unicode strings."""
535 return self._somequeue("queue")
537 def _somedir(self, command, dir, re):
539 self._simple(command, dir, re[0])
541 self._simple(command, dir)
544 def directories(self, dir, *re):
545 """List subdirectories of a directory.
548 dir -- directory to list, or '' for the whole root.
549 re -- regexp that results must match. Optional.
551 The return value is a list of the (nonempty) subdirectories of dir.
552 If dir is '' then a list of top-level directories is returned.
554 If a regexp is specified then the basename of each result must
555 match. Matching is case-independent. See pcrepattern(3).
557 return self._somedir("dirs", dir, re)
559 def files(self, dir, *re):
560 """List files within a directory.
563 dir -- directory to list, or '' for the whole root.
564 re -- regexp that results must match. Optional.
566 The return value is a list of playable files in dir. If dir is ''
567 then a list of top-level files is returned.
569 If a regexp is specified then the basename of each result must
570 match. Matching is case-independent. See pcrepattern(3).
572 return self._somedir("files", dir, re)
574 def allfiles(self, dir, *re):
575 """List subdirectories and files within a directory.
578 dir -- directory to list, or '' for the whole root.
579 re -- regexp that results must match. Optional.
581 The return value is a list of all (nonempty) subdirectories and
582 files within dir. If dir is '' then a list of top-level files and
583 directories is returned.
585 If a regexp is specified then the basename of each result must
586 match. Matching is case-independent. See pcrepattern(3).
588 return self._somedir("allfiles", dir, re)
590 def set(self, track, key, value):
591 """Set a preference value.
594 track -- the track to modify
595 key -- the preference name
596 value -- the new preference value
598 self._simple("set", track, key, value)
600 def unset(self, track, key):
601 """Unset a preference value.
604 track -- the track to modify
605 key -- the preference to remove
607 self._simple("set", track, key, value)
609 def get(self, track, key):
610 """Get a preference value.
613 track -- the track to query
614 key -- the preference to remove
616 The return value is the preference.
618 ret, details = self._simple("get", track, key)
624 def prefs(self, track):
625 """Get all the preferences for a track.
628 track -- the track to query
630 The return value is a dictionary of all the track's preferences.
631 Note that even nominally numeric values remain encoded as strings.
633 self._simple("prefs", track)
635 for line in self._body():
638 except _splitError, s:
639 raise protocolError(self.who, s.str())
641 raise protocolError(self.who, "invalid prefs body line")
645 def _boolean(self, s):
648 def exists(self, track):
649 """Return true if a track exists
652 track -- the track to check for"""
653 return self._boolean(self._simple("exists", track))
656 """Return true if playing is enabled"""
657 return self._boolean(self._simple("enabled"))
659 def random_enabled(self):
660 """Return true if random play is enabled"""
661 return self._boolean(self._simple("random-enabled"))
663 def random_enable(self):
664 """Enable random play."""
665 self._simple("random-enable")
667 def random_disable(self):
668 """Disable random play."""
669 self._simple("random-disable")
671 def length(self, track):
672 """Return the length of a track in seconds.
675 track -- the track to query.
677 ret, details = self._simple("length", track)
680 def search(self, words):
681 """Search for tracks.
684 words -- the set of words to search for.
686 The return value is a list of track path names, all of which contain
687 all of the required words (in their path name, trackname
690 self._simple("search", _quote(words))
696 The return value is a list of all tags which apply to at least one
702 """Get server statistics.
704 The return value is list of statistics.
706 self._simple("stats")
710 """Get all preferences.
712 The return value is an encoded dump of the preferences database.
717 def set_volume(self, left, right):
721 left -- volume for the left speaker.
722 right -- volume for the right speaker.
724 self._simple("volume", left, right)
726 def get_volume(self):
729 The return value a tuple consisting of the left and right volumes.
731 ret, details = self._simple("volume")
732 return map(int,string.split(details))
734 def move(self, track, delta):
735 """Move a track in the queue.
738 track -- the name or ID of the track to move
739 delta -- the number of steps towards the head of the queue to move
741 ret, details = self._simple("move", track, str(delta))
744 def moveafter(self, target, tracks):
745 """Move a track in the queue
748 target -- target ID or None
749 tracks -- a list of IDs to move
751 If target is '' or is not in the queue then the tracks are moved to
752 the head of the queue.
754 Otherwise the tracks are moved to just after the target."""
757 self._simple("moveafter", target, *tracks)
759 def log(self, callback):
760 """Read event log entries as they happen.
762 Each event log entry is handled by passing it to callback.
764 The callback takes two arguments, the first is the client and the
765 second the line from the event log.
767 The callback should return True to continue or False to stop (don't
768 forget this, or your program will mysteriously misbehave).
770 It is suggested that you use the disorder.monitor class instead of
771 calling this method directly, but this is not mandatory.
773 See disorder_protocol(5) for the event log syntax.
776 callback -- function to call with log entry
778 ret, details = self._simple("log")
781 self._debug(client.debug_body, "<<< %s" % l)
782 if l != '' and l[0] == '.':
786 if not callback(self, l):
788 # tell the server to stop sending, eat the remains of the body,
790 self._send("version")
795 """Pause the current track."""
796 self._simple("pause")
799 """Resume after a pause."""
800 self._simple("resume")
802 def part(self, track, context, part):
803 """Get a track name part
806 track -- the track to query
807 context -- the context ('sort' or 'display')
808 part -- the desired part (usually 'artist', 'album' or 'title')
810 The return value is the preference
812 ret, details = self._simple("part", track, context, part)
815 def setglobal(self, key, value):
816 """Set a global preference value.
819 key -- the preference name
820 value -- the new preference value
822 self._simple("set-global", key, value)
824 def unsetglobal(self, key):
825 """Unset a global preference value.
828 key -- the preference to remove
830 self._simple("set-global", key, value)
832 def getglobal(self, key):
833 """Get a global preference value.
836 key -- the preference to look up
838 The return value is the preference
840 ret, details = self._simple("get-global", key)
846 def make_cookie(self):
847 """Create a login cookie"""
848 ret, details = self._simple("make-cookie")
849 return _split(details)[0]
852 """Revoke a login cookie"""
853 self._simple("revoke")
855 def adduser(self, user, password):
857 self._simple("adduser", user, password)
859 def deluser(self, user):
861 self._simple("deluser", user)
863 def userinfo(self, user, key):
864 """Get user information"""
865 res, details = self._simple("userinfo", user, key)
868 return _split(details)[0]
870 def edituser(self, user, key, value):
871 """Set user information"""
872 self._simple("edituser", user, key, value)
877 The return value is a list of all users."""
878 self._simple("users")
881 ########################################################################
885 # read one response line and return as some suitable string object
887 # If an I/O error occurs, disconnect from the server.
889 # XXX does readline() DTRT regarding character encodings?
891 l = self.r.readline()
892 if not re.search("\n", l):
893 raise communicationError(self.who, "peer disconnected")
898 return unicode(l, "UTF-8")
901 # read a response as a (code, details) tuple
903 self._debug(client.debug_proto, "<== %s" % l)
904 m = _response.match(l)
906 return int(m.group(1)), m.group(2)
908 raise protocolError(self.who, "invalid response %s")
910 def _send(self, *command):
911 # Quote and send a command
913 # Returns the encoded command.
914 quoted = _quote(command)
915 self._debug(client.debug_proto, "==> %s" % quoted)
916 encoded = quoted.encode("UTF-8")
918 self.w.write(encoded)
925 raise communicationError(self.who, e)
930 def _simple(self, *command):
931 # Issue a simple command, throw an exception on error
933 # If an I/O error occurs, disconnect from the server.
935 # On success or 'normal' errors returns response as a (code, details) tuple
937 # On error raise operationError
938 if self.state == 'disconnected':
941 cmd = self._send(*command)
944 res, details = self._response()
945 if res / 100 == 2 or res == 555:
947 raise operationError(res, details, cmd)
950 # Fetch a dot-stuffed body
954 self._debug(client.debug_body, "<<< %s" % l)
955 if l != '' and l[0] == '.':
961 ########################################################################
962 # Configuration file parsing
964 def _readfile(self, path):
965 # Read a configuration file
969 # path -- path of file to read
971 # handlers for various commands
972 def _collection(self, command, args):
974 return "'%s' takes three args" % command
975 self.config["collections"].append(args)
977 def _unary(self, command, args):
979 return "'%s' takes only one arg" % command
980 self.config[command] = args[0]
982 def _include(self, command, args):
984 return "'%s' takes only one arg" % command
985 self._readfile(args[0])
987 def _any(self, command, args):
988 self.config[command] = args
990 # mapping of options to handlers
991 _options = { "collection": _collection,
996 "include": _include }
999 for lno, line in enumerate(file(path, "r")):
1001 fields = _split(line, 'comments')
1002 except _splitError, s:
1003 raise parseError(path, lno + 1, str(s))
1006 # we just ignore options we don't know about, so as to cope gracefully
1007 # with version skew (and nothing to do with implementor laziness)
1008 if command in _options:
1009 e = _options[command](self, command, fields[1:])
1011 self._parseError(path, lno + 1, e)
1013 def _parseError(self, path, lno, s):
1014 raise parseError(path, lno, s)
1016 ########################################################################
1020 """DisOrder event log monitor class
1022 Intended to be subclassed with methods corresponding to event log messages
1023 the implementor cares about over-ridden."""
1025 def __init__(self, c=None):
1026 """Constructor for the monitor class
1028 Can be passed a client to use. If none is specified then one
1029 will be created specially for the purpose.
1038 """Start monitoring logs. Continues monitoring until one of the
1039 message-specific methods returns False. Can be called more than once
1040 (but not recursively!)"""
1041 self.c.log(self._callback)
1044 """Return the timestamp of the current (or most recent) event log entry"""
1045 return self.timestamp
1047 def _callback(self, c, line):
1051 return self.invalid(line)
1053 return self.invalid(line)
1054 self.timestamp = int(bits[0], 16)
1057 if keyword == 'completed':
1059 return self.completed(bits[0])
1060 elif keyword == 'failed':
1062 return self.failed(bits[0], bits[1])
1063 elif keyword == 'moved':
1068 return self.invalid(line)
1069 return self.moved(bits[0], n, bits[2])
1070 elif keyword == 'playing':
1072 return self.playing(bits[0], None)
1073 elif len(bits) == 2:
1074 return self.playing(bits[0], bits[1])
1075 elif keyword == 'queue' or keyword == 'recent-added':
1077 q = _list2dict(bits)
1079 return self.invalid(line)
1080 if keyword == 'queue':
1081 return self.queue(q)
1082 if keyword == 'recent-added':
1083 return self.recent_added(q)
1084 elif keyword == 'recent-removed':
1086 return self.recent_removed(bits[0])
1087 elif keyword == 'removed':
1089 return self.removed(bits[0], None)
1090 elif len(bits) == 2:
1091 return self.removed(bits[0], bits[1])
1092 elif keyword == 'scratched':
1094 return self.scratched(bits[0], bits[1])
1095 return self.invalid(line)
1097 def completed(self, track):
1098 """Called when a track completes.
1101 track -- track that completed"""
1104 def failed(self, track, error):
1105 """Called when a player suffers an error.
1108 track -- track that failed
1109 error -- error indicator"""
1112 def moved(self, id, offset, user):
1113 """Called when a track is moved in the queue.
1116 id -- queue entry ID
1117 offset -- distance moved
1118 user -- user responsible"""
1121 def playing(self, track, user):
1122 """Called when a track starts playing.
1125 track -- track that has started
1126 user -- user that submitted track, or None"""
1130 """Called when a track is added to the queue.
1133 q -- dictionary of new queue entry"""
1136 def recent_added(self, q):
1137 """Called when a track is added to the recently played list
1140 q -- dictionary of new queue entry"""
1143 def recent_removed(self, id):
1144 """Called when a track is removed from the recently played list
1147 id -- ID of removed entry (always the oldest)"""
1150 def removed(self, id, user):
1151 """Called when a track is removed from the queue, either manually
1152 or in order to play it.
1155 id -- ID of removed entry
1156 user -- user responsible (or None if we're playing this track)"""
1159 def scratched(self, track, user):
1160 """Called when a track is scratched
1163 track -- track that was scratched
1164 user -- user responsible"""
1167 def invalid(self, line):
1168 """Called when an event log line cannot be interpreted
1171 line -- line that could not be understood"""
1176 # py-indent-offset:2