fake_mpd.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python3
  2. """Minimal MPD server for exercising the UI's commands without touching a real
  3. player. Records every command received to the file given as the second
  4. argument. Usage: fake_mpd.py PORT LOGFILE [IDLE_EVENT_SECONDS [NEXT_DELAY_SECONDS]]
  5. """
  6. import socketserver
  7. import sys
  8. import threading
  9. import time
  10. log_path = sys.argv[2]
  11. idle_event_s = float(sys.argv[3]) if len(sys.argv) > 3 and sys.argv[3] else None
  12. delay_s = float(sys.argv[4]) if len(sys.argv) > 4 else 0
  13. class Player:
  14. state = "play"
  15. volume = 50
  16. song = 2
  17. player = Player()
  18. def record(line):
  19. with open(log_path, "a") as f:
  20. f.write(line + "\n")
  21. class Handler(socketserver.StreamRequestHandler):
  22. def send(self, text):
  23. self.wfile.write(text.encode())
  24. self.wfile.flush()
  25. def idle_event(self):
  26. with self.lock:
  27. if self.idling:
  28. self.idling = False
  29. self.send("changed: player\nOK\n")
  30. def handle(self):
  31. self.lock = threading.Lock()
  32. self.idling = False
  33. self.send("OK MPD 0.21.4\n")
  34. announced = False
  35. for raw in self.rfile:
  36. line = raw.decode("utf8").strip()
  37. cmd = line.split(" ")[0]
  38. if cmd != "idle":
  39. record(line)
  40. if cmd == "idle":
  41. # normally answered by the following noidle; optionally
  42. # announce a change once per connection, as a real server would
  43. with self.lock:
  44. self.idling = True
  45. if idle_event_s is not None and not announced:
  46. announced = True
  47. threading.Timer(idle_event_s, self.idle_event).start()
  48. continue
  49. if cmd == "noidle":
  50. # as MPD: ignored unless idle, then the idle gets its answer
  51. with self.lock:
  52. if self.idling:
  53. self.idling = False
  54. self.send("OK\n")
  55. continue
  56. if delay_s and cmd in ("next", "previous"):
  57. time.sleep(delay_s) # opening a song from a sleeping disk
  58. if cmd == "status":
  59. self.send(
  60. f"volume: {player.volume}\nrepeat: 1\nrandom: 1\nsingle: 0\n"
  61. f"consume: 0\nplaylist: 7\nplaylistlength: 10\n"
  62. f"state: {player.state}\nsong: {player.song}\nsongid: 3\n"
  63. "time: 83:296\nelapsed: 83.100\nbitrate: 320\nduration: 296.000\n"
  64. "OK\n"
  65. )
  66. elif cmd == "currentsong":
  67. self.send(
  68. "file: muzyka/test.flac\nArtist: Zażółć Gęślą\n"
  69. "Title: Jaźń – bardzo długi tytuł utworu, który na pewno "
  70. "nie zmieści się w jednej linii wyświetlacza\n"
  71. f"Pos: {player.song}\nId: 3\nOK\n"
  72. )
  73. elif cmd == "volume":
  74. player.volume += int(line.split(" ")[1].strip('"'))
  75. self.send("OK\n")
  76. elif cmd == "stop":
  77. player.state = "stop"
  78. self.send("OK\n")
  79. elif cmd == "play":
  80. player.state = "play"
  81. self.send("OK\n")
  82. elif cmd == "pause":
  83. player.state = "pause" if player.state == "play" else "play"
  84. self.send("OK\n")
  85. elif cmd == "next":
  86. player.song += 1
  87. self.send("OK\n")
  88. elif cmd == "previous":
  89. player.song -= 1
  90. self.send("OK\n")
  91. elif cmd == "close":
  92. return
  93. else:
  94. self.send(f"ACK [5@0] {{{cmd}}} unknown command\n")
  95. class Server(socketserver.ThreadingTCPServer):
  96. allow_reuse_address = True
  97. daemon_threads = True
  98. Server(("127.0.0.1", int(sys.argv[1])), Handler).serve_forever()