1(import mpd)
2(require [hy.contrib.walk [let]])
3
4(defclass Commands [object]
5 (defn --init-- [self]
6 (setv self.handlers {}))
7
8 (defn convert-dict [self dict]
9 (dfor (, key value) (.items dict)
10 [key (if (isinstance value bool)
11 (if value 1 0)
12 value)]))
13
14 (defn dict->mpdstr [self dict]
15 (.rstrip (reduce (fn [rest pair]
16 (+ rest
17 (.format "{}: {}" (first pair) (last pair))
18 mpd.DELIMITER))
19 (.items (self.convert-dict dict)) "") mpd.DELIMITER))
20
21 (defn add [self name]
22 (fn [func]
23 (if (in name self.handlers)
24 (raise (ValueError (% "%s already registered" name)))
25 (do
26 (assoc self.handlers name func)
27 func))))
28
29 (defn call [self mpv cmd]
30 (if (in cmd.name self.handlers)
31 (let [resp ((get self.handlers cmd.name) mpv cmd.args)]
32 (if (isinstance resp dict)
33 (self.dict->mpdstr resp)
34 resp))
35 (raise (NotImplementedError (% "%s has not ben implemented" cmd.name))))))
36
37(setv commands (Commands))