chiark / gitweb /
Quieten compiler
[disorder] / python / disorder.py.in
CommitLineData
460b9539 1#
d8055dc4 2# Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
460b9539 3#
e7eb3a27 4# This program is free software: you can redistribute it and/or modify
460b9539 5# it under the terms of the GNU General Public License as published by
e7eb3a27 6# the Free Software Foundation, either version 3 of the License, or
460b9539 7# (at your option) any later version.
8#
e7eb3a27
RK
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
460b9539 14# You should have received a copy of the GNU General Public License
e7eb3a27 15# along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 16#
17
18"""Python support for DisOrder
19
20Provides disorder.client, a class for accessing a DisOrder server.
21
22Example 1:
23
24 #! /usr/bin/env python
25 import disorder
26 d = disorder.client()
27 p = d.playing()
28 if p:
29 print p['track']
30
31Example 2:
32
33 #! /usr/bin/env python
34 import disorder
35 import sys
36 d = disorder.client()
37 for path in sys.argv[1:]:
38 d.play(path)
39
79cbb91d
RK
40See disorder_protocol(5) for details of the communication protocol.
41
42NB that this code only supports servers configured to use SHA1-based
43authentication. If the server demands another hash then it will not be
44possible to use this module.
460b9539 45"""
46
47import re
48import string
49import os
50import pwd
51import socket
52import binascii
53import sha
54import sys
55import locale
56
57_configfile = "pkgconfdir/config"
58_dbhome = "pkgstatedir"
eee9d4b3 59_userconf = True
460b9539 60
61# various regexps we'll use
62_ws = re.compile(r"^[ \t\n\r]+")
0b96f403
RK
63_squote = re.compile("'(([^\\\\']|\\\\[\\\\\"'n])*)'")
64_dquote = re.compile("\"(([^\\\\\"]|\\\\[\\\\\"'n])*)\"")
460b9539 65_unquoted = re.compile("[^\"' \\t\\n\\r][^ \t\n\r]*")
66
67_response = re.compile("([0-9]{3}) ?(.*)")
68
69version = "_version_"
70
71########################################################################
72# exception classes
73
74class Error(Exception):
75 """Base class for DisOrder exceptions."""
76
77class _splitError(Error):
78 # _split failed
79 def __init__(self, value):
80 self.value = value
81 def __str__(self):
82 return str(self.value)
83
84class parseError(Error):
85 """Error parsing the configuration file."""
86 def __init__(self, path, line, details):
87 self.path = path
88 self.line = line
89 self.details = details
90 def __str__(self):
91 return "%s:%d: %s" % (self.path, self.line, self.details)
92
93class protocolError(Error):
94 """DisOrder control protocol error.
95
96 Indicates a mismatch between the client and server's understanding of
97 the control protocol.
98 """
99 def __init__(self, who, error):
100 self.who = who
101 self.error = error
102 def __str__(self):
103 return "%s: %s" % (self.who, str(self.error))
104
105class operationError(Error):
106 """DisOrder control protocol error response.
107
108 Indicates that an operation failed (e.g. an attempt to play a
109 nonexistent track). The connection should still be usable.
110 """
f383b2f1 111 def __init__(self, res, details, cmd=None):
460b9539 112 self.res_ = int(res)
f383b2f1 113 self.cmd_ = cmd
460b9539 114 self.details_ = details
115 def __str__(self):
f383b2f1
RK
116 """Return the complete response string from the server, with the command
117 if available.
460b9539 118
119 Excludes the final newline.
120 """
f383b2f1
RK
121 if self.cmd_ is None:
122 return "%d %s" % (self.res_, self.details_)
123 else:
124 return "%d %s [%s]" % (self.res_, self.details_, self.cmd_)
460b9539 125 def response(self):
126 """Return the response code from the server."""
127 return self.res_
128 def details(self):
129 """Returns the detail string from the server."""
130 return self.details_
131
132class communicationError(Error):
133 """DisOrder control protocol communication error.
134
135 Indicates that communication with the server went wrong, perhaps
136 because the server was restarted. The caller could report an error to
137 the user and wait for further user instructions, or even automatically
138 retry the operation.
139 """
140 def __init__(self, who, error):
141 self.who = who
142 self.error = error
143 def __str__(self):
144 return "%s: %s" % (self.who, str(self.error))
145
146########################################################################
147# DisOrder-specific text processing
148
149def _unescape(s):
150 # Unescape the contents of a string
151 #
152 # Arguments:
153 #
154 # s -- string to unescape
155 #
156 s = re.sub("\\\\n", "\n", s)
157 s = re.sub("\\\\(.)", "\\1", s)
158 return s
159
160def _split(s, *comments):
161 # Split a string into fields according to the usual Disorder string splitting
162 # conventions.
163 #
164 # Arguments:
165 #
166 # s -- string to parse
167 # comments -- if present, parse comments
168 #
169 # Return values:
170 #
171 # On success, a list of fields is returned.
172 #
173 # On error, disorder.parseError is thrown.
174 #
175 fields = []
176 while s != "":
177 # discard comments
178 if comments and s[0] == '#':
179 break
180 # strip spaces
181 m = _ws.match(s)
182 if m:
183 s = s[m.end():]
184 continue
185 # pick of quoted fields of both kinds
186 m = _squote.match(s)
187 if not m:
188 m = _dquote.match(s)
189 if m:
190 fields.append(_unescape(m.group(1)))
191 s = s[m.end():]
192 continue
193 # and unquoted fields
194 m = _unquoted.match(s)
195 if m:
196 fields.append(m.group(0))
197 s = s[m.end():]
198 continue
199 # anything left must be in error
200 if s[0] == '"' or s[0] == '\'':
201 raise _splitError("invalid quoted string")
202 else:
203 raise _splitError("syntax error")
204 return fields
205
206def _escape(s):
207 # Escape the contents of a string
208 #
209 # Arguments:
210 #
211 # s -- string to escape
212 #
213 if re.search("[\\\\\"'\n \t\r]", s) or s == '':
214 s = re.sub(r'[\\"]', r'\\\g<0>', s)
215 s = re.sub("\n", r"\\n", s)
216 return '"' + s + '"'
217 else:
218 return s
219
220def _quote(list):
221 # Quote a list of values
222 return ' '.join(map(_escape, list))
223
224def _sanitize(s):
225 # Return the value of s in a form suitable for writing to stderr
226 return s.encode(locale.nl_langinfo(locale.CODESET), 'replace')
227
228def _list2dict(l):
229 # Convert a list of the form [k1, v1, k2, v2, ..., kN, vN]
230 # to a dictionary {k1:v1, k2:v2, ..., kN:vN}
231 d = {}
232 i = iter(l)
233 try:
234 while True:
235 k = i.next()
236 v = i.next()
f5eb2aff 237 d[str(k)] = v
460b9539 238 except StopIteration:
239 pass
240 return d
241
242def _queueEntry(s):
243 # parse a queue entry
244 return _list2dict(_split(s))
245
246########################################################################
247# The client class
248
249class client:
250 """DisOrder client class.
251
252 This class provides access to the DisOrder server either on this
253 machine or across the internet.
254
255 The server to connect to, and the username and password to use, are
256 determined from the configuration files as described in 'man
257 disorder_config'.
258
259 All methods will connect if necessary, as soon as you have a
260 disorder.client object you can start calling operational methods on
261 it.
262
263 However if the server is restarted then the next method called on a
264 connection will throw an exception. This may be considered a bug.
265
266 All methods block until they complete.
267
268 Operation methods raise communicationError if the connection breaks,
269 protocolError if the response from the server is malformed, or
270 operationError if the response is valid but indicates that the
271 operation failed.
272 """
273
274 debug_proto = 0x0001
275 debug_body = 0x0002
276
f0feb22e 277 def __init__(self, user=None, password=None):
460b9539 278 """Constructor for DisOrder client class.
279
280 The constructor reads the configuration file, but does not connect
281 to the server.
282
283 If the environment variable DISORDER_PYTHON_DEBUG is set then the
284 debug flags are initialised to that value. This can be overridden
285 with the debug() method below.
286
287 The constructor Raises parseError() if the configuration file is not
288 valid.
289 """
290 pw = pwd.getpwuid(os.getuid())
291 self.debugging = int(os.getenv("DISORDER_PYTHON_DEBUG", 0))
292 self.config = { 'collections': [],
293 'username': pw.pw_name,
294 'home': _dbhome }
f0feb22e
RK
295 self.user = user
296 self.password = password
460b9539 297 home = os.getenv("HOME")
298 if not home:
299 home = pw.pw_dir
300 privconf = _configfile + "." + pw.pw_name
301 passfile = home + os.sep + ".disorder" + os.sep + "passwd"
f5eb2aff
RK
302 if os.path.exists(_configfile):
303 self._readfile(_configfile)
460b9539 304 if os.path.exists(privconf):
305 self._readfile(privconf)
eee9d4b3 306 if os.path.exists(passfile) and _userconf:
460b9539 307 self._readfile(passfile)
308 self.state = 'disconnected'
309
310 def debug(self, bits):
311 """Enable or disable protocol debugging. Debug messages are written
312 to sys.stderr.
313
314 Arguments:
315 bits -- bitmap of operations that should generate debug information
316
317 Bitmap values:
318 debug_proto -- dump control protocol messages (excluding bodies)
319 debug_body -- dump control protocol message bodies
320 """
321 self.debugging = bits
322
323 def _debug(self, bit, s):
324 # debug output
325 if self.debugging & bit:
326 sys.stderr.write(_sanitize(s))
327 sys.stderr.write("\n")
328 sys.stderr.flush()
329
b12be54a
RK
330 def connect(self, cookie=None):
331 """c.connect(cookie=None)
332
333 Connect to the DisOrder server and authenticate.
460b9539 334
335 Raises communicationError if connection fails and operationError if
336 authentication fails (in which case disconnection is automatic).
337
338 May be called more than once to retry connections (e.g. when the
339 server is down). If we are already connected and authenticated,
340 this is a no-op.
341
342 Other operations automatically connect if we're not already
343 connected, so it is not strictly necessary to call this method.
b12be54a
RK
344
345 If COOKIE is specified then that is used to log in instead of
346 the username/password.
460b9539 347 """
348 if self.state == 'disconnected':
349 try:
350 self.state = 'connecting'
351 if 'connect' in self.config and len(self.config['connect']) > 0:
352 c = self.config['connect']
353 self.who = repr(c) # temporarily
354 if len(c) == 1:
355 a = socket.getaddrinfo(None, c[0],
356 socket.AF_INET,
357 socket.SOCK_STREAM,
358 0,
359 0)
360 else:
361 a = socket.getaddrinfo(c[0], c[1],
362 socket.AF_INET,
363 socket.SOCK_STREAM,
364 0,
365 0)
366 a = a[0]
367 s = socket.socket(a[0], a[1], a[2]);
368 s.connect(a[4])
369 self.who = "%s" % a[3]
370 else:
371 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM);
372 self.who = self.config['home'] + os.sep + "socket"
373 s.connect(self.who)
374 self.w = s.makefile("wb")
375 self.r = s.makefile("rb")
7b32e917
RK
376 (res, details) = self._simple()
377 (protocol, algo, challenge) = _split(details)
378 if protocol != '2':
379 raise communicationError(self.who,
380 "unknown protocol version %s" % protocol)
b12be54a 381 if cookie is None:
f0feb22e
RK
382 if self.user is None:
383 user = self.config['username']
384 else:
385 user = self.user
386 if self.password is None:
387 password = self.config['password']
388 else:
389 password = self.password
b3141726 390 # TODO support algorithms other than SHA-1
b12be54a 391 h = sha.sha()
f0feb22e 392 h.update(password)
b12be54a 393 h.update(binascii.unhexlify(challenge))
f0feb22e 394 self._simple("user", user, h.hexdigest())
b12be54a
RK
395 else:
396 self._simple("cookie", cookie)
460b9539 397 self.state = 'connected'
398 except socket.error, e:
399 self._disconnect()
400 raise communicationError(self.who, e)
401 except:
402 self._disconnect()
403 raise
404
405 def _disconnect(self):
406 # disconnect from the server, whatever state we are in
407 try:
408 del self.w
409 del self.r
410 except:
411 pass
412 self.state = 'disconnected'
413
414 ########################################################################
415 # Operations
416
460b9539 417 def play(self, track):
418 """Play a track.
419
420 Arguments:
421 track -- the path of the track to play.
81e440ce
RK
422
423 Returns the ID of the new queue entry.
79cbb91d
RK
424
425 Note that queue IDs are unicode strings (because all track information
426 values are unicode strings).
460b9539 427 """
81e440ce
RK
428 res, details = self._simple("play", track)
429 return unicode(details) # because it's unicode in queue() output
460b9539 430
431 def remove(self, track):
432 """Remove a track from the queue.
433
434 Arguments:
435 track -- the path or ID of the track to remove.
436 """
437 self._simple("remove", track)
438
439 def enable(self):
440 """Enable playing."""
441 self._simple("enable")
442
443 def disable(self, *now):
444 """Disable playing.
445
446 Arguments:
447 now -- if present (with any value), the current track is stopped
448 too.
449 """
450 if now:
451 self._simple("disable", "now")
452 else:
453 self._simple("disable")
454
455 def scratch(self, *id):
456 """Scratch the currently playing track.
457
458 Arguments:
459 id -- if present, the ID of the track to scratch.
460 """
461 if id:
462 self._simple("scratch", id[0])
463 else:
464 self._simple("scratch")
465
466 def shutdown(self):
467 """Shut down the server.
468
469 Only trusted users can perform this operation.
470 """
471 self._simple("shutdown")
472
473 def reconfigure(self):
474 """Make the server reload its configuration.
475
476 Only trusted users can perform this operation.
477 """
478 self._simple("reconfigure")
479
dd9af5cb 480 def rescan(self, *flags):
460b9539 481 """Rescan one or more collections.
482
460b9539 483 Only trusted users can perform this operation.
484 """
dd9af5cb 485 self._simple("rescan", *flags)
460b9539 486
487 def version(self):
488 """Return the server's version number."""
7b32e917 489 return _split(self._simple("version")[1])[0]
460b9539 490
491 def playing(self):
492 """Return the currently playing track.
493
79cbb91d
RK
494 If a track is playing then it is returned as a dictionary. See
495 disorder_protocol(5) for the meanings of the keys. All keys are
496 plain strings but the values will be unicode strings.
497
460b9539 498 If no track is playing then None is returned."""
499 res, details = self._simple("playing")
500 if res % 10 != 9:
501 try:
502 return _queueEntry(details)
503 except _splitError, s:
504 raise protocolError(self.who, s.str())
505 else:
506 return None
507
508 def _somequeue(self, command):
509 self._simple(command)
510 try:
511 return map(lambda s: _queueEntry(s), self._body())
512 except _splitError, s:
513 raise protocolError(self.who, s.str())
514
515 def recent(self):
516 """Return a list of recently played tracks.
517
518 The return value is a list of dictionaries corresponding to
79cbb91d
RK
519 recently played tracks. The oldest track comes first.
520
521 See disorder_protocol(5) for the meanings of the keys. All keys are
522 plain strings but the values will be unicode strings."""
460b9539 523 return self._somequeue("recent")
524
525 def queue(self):
526 """Return the current queue.
527
528 The return value is a list of dictionaries corresponding to
79cbb91d
RK
529 recently played tracks. The next track to be played comes first.
530
531 See disorder_protocol(5) for the meanings of the keys. All keys are
532 plain strings but the values will be unicode strings."""
460b9539 533 return self._somequeue("queue")
534
535 def _somedir(self, command, dir, re):
536 if re:
537 self._simple(command, dir, re[0])
538 else:
539 self._simple(command, dir)
540 return self._body()
541
542 def directories(self, dir, *re):
543 """List subdirectories of a directory.
544
545 Arguments:
546 dir -- directory to list, or '' for the whole root.
547 re -- regexp that results must match. Optional.
548
549 The return value is a list of the (nonempty) subdirectories of dir.
550 If dir is '' then a list of top-level directories is returned.
551
552 If a regexp is specified then the basename of each result must
553 match. Matching is case-independent. See pcrepattern(3).
554 """
555 return self._somedir("dirs", dir, re)
556
557 def files(self, dir, *re):
558 """List files within a directory.
559
560 Arguments:
561 dir -- directory to list, or '' for the whole root.
562 re -- regexp that results must match. Optional.
563
564 The return value is a list of playable files in dir. If dir is ''
565 then a list of top-level files is returned.
566
567 If a regexp is specified then the basename of each result must
568 match. Matching is case-independent. See pcrepattern(3).
569 """
570 return self._somedir("files", dir, re)
571
572 def allfiles(self, dir, *re):
573 """List subdirectories and files within a directory.
574
575 Arguments:
576 dir -- directory to list, or '' for the whole root.
577 re -- regexp that results must match. Optional.
578
579 The return value is a list of all (nonempty) subdirectories and
580 files within dir. If dir is '' then a list of top-level files and
581 directories is returned.
582
583 If a regexp is specified then the basename of each result must
584 match. Matching is case-independent. See pcrepattern(3).
585 """
586 return self._somedir("allfiles", dir, re)
587
588 def set(self, track, key, value):
589 """Set a preference value.
590
591 Arguments:
592 track -- the track to modify
593 key -- the preference name
594 value -- the new preference value
595 """
596 self._simple("set", track, key, value)
597
598 def unset(self, track, key):
599 """Unset a preference value.
600
601 Arguments:
602 track -- the track to modify
603 key -- the preference to remove
604 """
605 self._simple("set", track, key, value)
606
607 def get(self, track, key):
608 """Get a preference value.
609
610 Arguments:
611 track -- the track to query
612 key -- the preference to remove
613
79cbb91d 614 The return value is the preference.
460b9539 615 """
616 ret, details = self._simple("get", track, key)
fb1bc1f5
RK
617 if ret == 555:
618 return None
619 else:
7b32e917 620 return _split(details)[0]
460b9539 621
622 def prefs(self, track):
623 """Get all the preferences for a track.
624
625 Arguments:
626 track -- the track to query
627
628 The return value is a dictionary of all the track's preferences.
629 Note that even nominally numeric values remain encoded as strings.
630 """
631 self._simple("prefs", track)
632 r = {}
633 for line in self._body():
634 try:
635 kv = _split(line)
636 except _splitError, s:
637 raise protocolError(self.who, s.str())
638 if len(kv) != 2:
639 raise protocolError(self.who, "invalid prefs body line")
640 r[kv[0]] = kv[1]
641 return r
642
643 def _boolean(self, s):
644 return s[1] == 'yes'
645
646 def exists(self, track):
647 """Return true if a track exists
648
649 Arguments:
650 track -- the track to check for"""
651 return self._boolean(self._simple("exists", track))
652
653 def enabled(self):
654 """Return true if playing is enabled"""
655 return self._boolean(self._simple("enabled"))
656
657 def random_enabled(self):
658 """Return true if random play is enabled"""
659 return self._boolean(self._simple("random-enabled"))
660
661 def random_enable(self):
662 """Enable random play."""
663 self._simple("random-enable")
664
665 def random_disable(self):
666 """Disable random play."""
667 self._simple("random-disable")
668
669 def length(self, track):
670 """Return the length of a track in seconds.
671
672 Arguments:
673 track -- the track to query.
674 """
675 ret, details = self._simple("length", track)
676 return int(details)
677
678 def search(self, words):
679 """Search for tracks.
680
681 Arguments:
682 words -- the set of words to search for.
683
684 The return value is a list of track path names, all of which contain
685 all of the required words (in their path name, trackname
686 preferences, etc.)
687 """
f383b2f1 688 self._simple("search", _quote(words))
460b9539 689 return self._body()
690
31773020
RK
691 def tags(self):
692 """List all tags
693
694 The return value is a list of all tags which apply to at least one
695 track."""
696 self._simple("tags")
697 return self._body()
698
460b9539 699 def stats(self):
700 """Get server statistics.
701
702 The return value is list of statistics.
703 """
704 self._simple("stats")
705 return self._body()
706
707 def dump(self):
708 """Get all preferences.
709
710 The return value is an encoded dump of the preferences database.
711 """
712 self._simple("dump")
713 return self._body()
714
715 def set_volume(self, left, right):
716 """Set volume.
717
718 Arguments:
719 left -- volume for the left speaker.
720 right -- volume for the right speaker.
721 """
722 self._simple("volume", left, right)
723
724 def get_volume(self):
725 """Get volume.
726
727 The return value a tuple consisting of the left and right volumes.
728 """
729 ret, details = self._simple("volume")
730 return map(int,string.split(details))
731
732 def move(self, track, delta):
733 """Move a track in the queue.
734
735 Arguments:
736 track -- the name or ID of the track to move
737 delta -- the number of steps towards the head of the queue to move
738 """
739 ret, details = self._simple("move", track, str(delta))
740 return int(details)
741
81e440ce
RK
742 def moveafter(self, target, tracks):
743 """Move a track in the queue
744
745 Arguments:
746 target -- target ID or None
747 tracks -- a list of IDs to move
748
749 If target is '' or is not in the queue then the tracks are moved to
750 the head of the queue.
751
752 Otherwise the tracks are moved to just after the target."""
753 if target is None:
754 target = ''
755 self._simple("moveafter", target, *tracks)
756
460b9539 757 def log(self, callback):
758 """Read event log entries as they happen.
759
760 Each event log entry is handled by passing it to callback.
761
762 The callback takes two arguments, the first is the client and the
763 second the line from the event log.
764
765 The callback should return True to continue or False to stop (don't
dbeb3844
RK
766 forget this, or your program will mysteriously misbehave). Once you
767 stop reading the log the connection is useless and should be deleted.
460b9539 768
769 It is suggested that you use the disorder.monitor class instead of
770 calling this method directly, but this is not mandatory.
771
772 See disorder_protocol(5) for the event log syntax.
773
774 Arguments:
775 callback -- function to call with log entry
776 """
777 ret, details = self._simple("log")
778 while True:
779 l = self._line()
780 self._debug(client.debug_body, "<<< %s" % l)
781 if l != '' and l[0] == '.':
782 if l == '.':
783 return
784 l = l[1:]
785 if not callback(self, l):
786 break
460b9539 787
788 def pause(self):
789 """Pause the current track."""
790 self._simple("pause")
791
792 def resume(self):
793 """Resume after a pause."""
794 self._simple("resume")
795
796 def part(self, track, context, part):
797 """Get a track name part
798
799 Arguments:
800 track -- the track to query
801 context -- the context ('sort' or 'display')
802 part -- the desired part (usually 'artist', 'album' or 'title')
803
804 The return value is the preference
805 """
806 ret, details = self._simple("part", track, context, part)
7b32e917 807 return _split(details)[0]
460b9539 808
f35e5800
RK
809 def setglobal(self, key, value):
810 """Set a global preference value.
811
812 Arguments:
813 key -- the preference name
814 value -- the new preference value
815 """
816 self._simple("set-global", key, value)
817
818 def unsetglobal(self, key):
819 """Unset a global preference value.
820
821 Arguments:
822 key -- the preference to remove
823 """
824 self._simple("set-global", key, value)
825
826 def getglobal(self, key):
827 """Get a global preference value.
828
829 Arguments:
830 key -- the preference to look up
831
832 The return value is the preference
833 """
834 ret, details = self._simple("get-global", key)
fb1bc1f5
RK
835 if ret == 555:
836 return None
837 else:
7b32e917 838 return _split(details)[0]
f35e5800 839
b12be54a
RK
840 def make_cookie(self):
841 """Create a login cookie"""
842 ret, details = self._simple("make-cookie")
eb5dc014 843 return _split(details)[0]
b12be54a
RK
844
845 def revoke(self):
846 """Revoke a login cookie"""
847 self._simple("revoke")
848
f0feb22e
RK
849 def adduser(self, user, password):
850 """Create a user"""
851 self._simple("adduser", user, password)
852
853 def deluser(self, user):
854 """Delete a user"""
855 self._simple("deluser", user)
856
5df73aeb
RK
857 def userinfo(self, user, key):
858 """Get user information"""
859 res, details = self._simple("userinfo", user, key)
860 if res == 555:
861 return None
862 return _split(details)[0]
863
864 def edituser(self, user, key, value):
865 """Set user information"""
866 self._simple("edituser", user, key, value)
867
c3be4f19
RK
868 def users(self):
869 """List all users
870
871 The return value is a list of all users."""
872 self._simple("users")
873 return self._body()
874
ba39faf6
RK
875 def register(self, username, password, email):
876 """Register a user"""
877 res, details = self._simple("register", username, password, email)
878 return _split(details)[0]
879
880 def confirm(self, confirmation):
881 """Confirm a user registration"""
882 res, details = self._simple("confirm", confirmation)
883
2b33c4cf
RK
884 def schedule_list(self):
885 """Get a list of scheduled events """
886 self._simple("schedule-list")
887 return self._body()
888
889 def schedule_del(self, event):
890 """Delete a scheduled event"""
891 self._simple("schedule-del", event)
892
893 def schedule_get(self, event):
894 """Get the details for an event as a dict (returns None if event not found)"""
895 res, details = self._simple("schedule-get", event)
896 if res == 555:
897 return None
898 d = {}
899 for line in self._body():
900 bits = _split(line)
901 d[bits[0]] = bits[1]
902 return d
903
904 def schedule_add(self, when, priority, action, *rest):
905 """Add a scheduled event"""
17360d2f 906 self._simple("schedule-add", str(when), priority, action, *rest)
2b33c4cf 907
460b9539 908 ########################################################################
909 # I/O infrastructure
910
911 def _line(self):
912 # read one response line and return as some suitable string object
913 #
914 # If an I/O error occurs, disconnect from the server.
915 #
916 # XXX does readline() DTRT regarding character encodings?
917 try:
918 l = self.r.readline()
919 if not re.search("\n", l):
920 raise communicationError(self.who, "peer disconnected")
921 l = l[:-1]
922 except:
923 self._disconnect()
924 raise
925 return unicode(l, "UTF-8")
926
927 def _response(self):
928 # read a response as a (code, details) tuple
929 l = self._line()
930 self._debug(client.debug_proto, "<== %s" % l)
931 m = _response.match(l)
932 if m:
933 return int(m.group(1)), m.group(2)
934 else:
935 raise protocolError(self.who, "invalid response %s")
936
937 def _send(self, *command):
f383b2f1
RK
938 # Quote and send a command
939 #
940 # Returns the encoded command.
460b9539 941 quoted = _quote(command)
942 self._debug(client.debug_proto, "==> %s" % quoted)
943 encoded = quoted.encode("UTF-8")
944 try:
945 self.w.write(encoded)
946 self.w.write("\n")
947 self.w.flush()
f383b2f1 948 return encoded
460b9539 949 except IOError, e:
950 # e.g. EPIPE
951 self._disconnect()
952 raise communicationError(self.who, e)
953 except:
954 self._disconnect()
955 raise
956
957 def _simple(self, *command):
958 # Issue a simple command, throw an exception on error
959 #
960 # If an I/O error occurs, disconnect from the server.
961 #
fb1bc1f5 962 # On success or 'normal' errors returns response as a (code, details) tuple
460b9539 963 #
964 # On error raise operationError
965 if self.state == 'disconnected':
966 self.connect()
967 if command:
f383b2f1
RK
968 cmd = self._send(*command)
969 else:
970 cmd = None
460b9539 971 res, details = self._response()
fb1bc1f5 972 if res / 100 == 2 or res == 555:
460b9539 973 return res, details
f383b2f1 974 raise operationError(res, details, cmd)
460b9539 975
976 def _body(self):
977 # Fetch a dot-stuffed body
978 result = []
979 while True:
980 l = self._line()
981 self._debug(client.debug_body, "<<< %s" % l)
982 if l != '' and l[0] == '.':
983 if l == '.':
984 return result
985 l = l[1:]
986 result.append(l)
987
988 ########################################################################
989 # Configuration file parsing
990
991 def _readfile(self, path):
992 # Read a configuration file
993 #
994 # Arguments:
995 #
996 # path -- path of file to read
997
998 # handlers for various commands
999 def _collection(self, command, args):
1000 if len(args) != 3:
1001 return "'%s' takes three args" % command
1002 self.config["collections"].append(args)
1003
1004 def _unary(self, command, args):
1005 if len(args) != 1:
1006 return "'%s' takes only one arg" % command
1007 self.config[command] = args[0]
1008
1009 def _include(self, command, args):
1010 if len(args) != 1:
1011 return "'%s' takes only one arg" % command
1012 self._readfile(args[0])
1013
1014 def _any(self, command, args):
1015 self.config[command] = args
1016
1017 # mapping of options to handlers
1018 _options = { "collection": _collection,
1019 "username": _unary,
1020 "password": _unary,
1021 "home": _unary,
1022 "connect": _any,
1023 "include": _include }
1024
1025 # the parser
1026 for lno, line in enumerate(file(path, "r")):
1027 try:
1028 fields = _split(line, 'comments')
1029 except _splitError, s:
1030 raise parseError(path, lno + 1, str(s))
1031 if fields:
1032 command = fields[0]
1033 # we just ignore options we don't know about, so as to cope gracefully
1034 # with version skew (and nothing to do with implementor laziness)
1035 if command in _options:
1036 e = _options[command](self, command, fields[1:])
1037 if e:
1038 self._parseError(path, lno + 1, e)
1039
1040 def _parseError(self, path, lno, s):
1041 raise parseError(path, lno, s)
1042
1043########################################################################
1044# monitor class
1045
1046class monitor:
1047 """DisOrder event log monitor class
1048
1049 Intended to be subclassed with methods corresponding to event log messages
1050 the implementor cares about over-ridden."""
1051
1052 def __init__(self, c=None):
1053 """Constructor for the monitor class
1054
1055 Can be passed a client to use. If none is specified then one
1056 will be created specially for the purpose.
1057
1058 Arguments:
1059 c -- client"""
1060 if c == None:
1061 c = client();
1062 self.c = c
1063
1064 def run(self):
1065 """Start monitoring logs. Continues monitoring until one of the
1066 message-specific methods returns False. Can be called more than once
1067 (but not recursively!)"""
1068 self.c.log(self._callback)
1069
1070 def when(self):
1071 """Return the timestamp of the current (or most recent) event log entry"""
1072 return self.timestamp
1073
1074 def _callback(self, c, line):
1075 try:
1076 bits = _split(line)
1077 except:
1078 return self.invalid(line)
1079 if(len(bits) < 2):
1080 return self.invalid(line)
1081 self.timestamp = int(bits[0], 16)
1082 keyword = bits[1]
1083 bits = bits[2:]
1084 if keyword == 'completed':
1085 if len(bits) == 1:
1086 return self.completed(bits[0])
1087 elif keyword == 'failed':
1088 if len(bits) == 2:
1089 return self.failed(bits[0], bits[1])
1090 elif keyword == 'moved':
1091 if len(bits) == 3:
1092 try:
1093 n = int(bits[1])
1094 except:
1095 return self.invalid(line)
1096 return self.moved(bits[0], n, bits[2])
1097 elif keyword == 'playing':
1098 if len(bits) == 1:
1099 return self.playing(bits[0], None)
1100 elif len(bits) == 2:
1101 return self.playing(bits[0], bits[1])
1102 elif keyword == 'queue' or keyword == 'recent-added':
1103 try:
1104 q = _list2dict(bits)
1105 except:
1106 return self.invalid(line)
1107 if keyword == 'queue':
1108 return self.queue(q)
1109 if keyword == 'recent-added':
1110 return self.recent_added(q)
1111 elif keyword == 'recent-removed':
1112 if len(bits) == 1:
1113 return self.recent_removed(bits[0])
1114 elif keyword == 'removed':
1115 if len(bits) == 1:
1116 return self.removed(bits[0], None)
1117 elif len(bits) == 2:
1118 return self.removed(bits[0], bits[1])
1119 elif keyword == 'scratched':
1120 if len(bits) == 2:
1121 return self.scratched(bits[0], bits[1])
d8055dc4
RK
1122 elif keyword == 'rescanned':
1123 return self.rescanned()
460b9539 1124 return self.invalid(line)
1125
1126 def completed(self, track):
1127 """Called when a track completes.
1128
1129 Arguments:
1130 track -- track that completed"""
1131 return True
1132
1133 def failed(self, track, error):
1134 """Called when a player suffers an error.
1135
1136 Arguments:
1137 track -- track that failed
1138 error -- error indicator"""
1139 return True
1140
1141 def moved(self, id, offset, user):
1142 """Called when a track is moved in the queue.
1143
1144 Arguments:
1145 id -- queue entry ID
1146 offset -- distance moved
1147 user -- user responsible"""
1148 return True
1149
1150 def playing(self, track, user):
1151 """Called when a track starts playing.
1152
1153 Arguments:
1154 track -- track that has started
1155 user -- user that submitted track, or None"""
1156 return True
1157
1158 def queue(self, q):
1159 """Called when a track is added to the queue.
1160
1161 Arguments:
1162 q -- dictionary of new queue entry"""
1163 return True
1164
1165 def recent_added(self, q):
1166 """Called when a track is added to the recently played list
1167
1168 Arguments:
1169 q -- dictionary of new queue entry"""
1170 return True
1171
1172 def recent_removed(self, id):
1173 """Called when a track is removed from the recently played list
1174
1175 Arguments:
1176 id -- ID of removed entry (always the oldest)"""
1177 return True
1178
1179 def removed(self, id, user):
1180 """Called when a track is removed from the queue, either manually
1181 or in order to play it.
1182
1183 Arguments:
1184 id -- ID of removed entry
1185 user -- user responsible (or None if we're playing this track)"""
1186 return True
1187
1188 def scratched(self, track, user):
1189 """Called when a track is scratched
1190
1191 Arguments:
1192 track -- track that was scratched
1193 user -- user responsible"""
1194 return True
1195
1196 def invalid(self, line):
1197 """Called when an event log line cannot be interpreted
1198
1199 Arguments:
1200 line -- line that could not be understood"""
1201 return True
1202
d8055dc4
RK
1203 def rescanned(self):
1204 """Called when a rescan completes"""
1205 return True
1206
460b9539 1207# Local Variables:
1208# mode:python
1209# py-indent-offset:2
1210# comment-column:40
1211# fill-column:72
1212# End: