1(import mpd
2 [playback.playback [Playback]]
3 [playback.playlist [Song]])
4(require [hy.contrib.walk [let]])
5
6(defclass Commands [object]
7 (defn __init__ [self]
8 (setv self.handlers {}))
9
10 (defn _serialize-dict [self dict &optional exclude]
11 (defn _serialize-values [dict]
12 (dfor (, key value) (.items dict)
13 [key (if (isinstance value bool)
14 (if value 1 0)
15 value)]))
16
17 (.rstrip (reduce (fn [rest pair]
18 (if (and (not (is None exclude))
19 (in (first pair) exclude))
20 rest
21 (+ rest
22 (.format "{}: {}" (first pair) (last pair))
23 mpd.DELIMITER)))
24 (.items (_serialize-values dict)) "") mpd.DELIMITER))
25
26 (defn _serialize-list [self list]
27 (.join mpd.DELIMITER list))
28
29 (defn _serialize-song [self song filter]
30 (._serialize-dict self
31 {#**
32 {
33 "file" (. song path)
34 "Pos" (. song position)
35 }
36 #**
37 (. song metadata)
38 } filter))
39
40 (defn _serialize-playback [self playback filter]
41 (with (playlist playback)
42 (._serialize-list self
43 (map (fn [song]
44 (._serialize-song self song filter))
45 playlist))))
46
47 (defn _serialize [self ctx value]
48 (cond
49 [(isinstance value dict)
50 (._serialize-dict self value)]
51 [(isinstance value list)
52 (._serialize-list self value)]
53 [(isinstance value Song)
54 (._serialize-song self value ctx.disabled-tags)]
55 [(isinstance value Playback)
56 (._serialize-playback self value ctx.disabled-tags)]
57 [True value]))
58
59 (defn add [self name]
60 (fn [func]
61 (if (in name self.handlers)
62 (raise (ValueError (% "%s already registered" name)))
63 (do
64 (assoc self.handlers name func)
65 func))))
66
67 (defn handle [self ctx cmd]
68 (if (in cmd.name self.handlers)
69 (let [handler (get self.handlers cmd.name)
70 resp (handler ctx cmd.args)]
71 (._serialize self ctx resp))
72 (raise (NotImplementedError (% "%s has not ben implemented" cmd.name))))))
73
74(setv commands (Commands))