aboutsummaryrefslogtreecommitdiff
path: root/pydora
diff options
context:
space:
mode:
authorMike Crute <mcrute@gmail.com>2015-07-19 21:03:19 -0700
committerMike Crute <mcrute@gmail.com>2015-07-19 21:03:19 -0700
commitc07bcaa8d008b6ef8058765f435bae6a858225c2 (patch)
tree7bd59bea538dafcc37f4a67bfb628aaac632905e /pydora
parent388fa58fa1151a527ffd14678e4d885a85e8021e (diff)
downloadpydora-c07bcaa8d008b6ef8058765f435bae6a858225c2.tar.bz2
pydora-c07bcaa8d008b6ef8058765f435bae6a858225c2.tar.xz
pydora-c07bcaa8d008b6ef8058765f435bae6a858225c2.zip
Reorganize pydora player code
Diffstat (limited to 'pydora')
-rw-r--r--pydora/mpg123.py56
-rwxr-xr-xpydora/player.py32
-rw-r--r--pydora/utils.py33
3 files changed, 62 insertions, 59 deletions
diff --git a/pydora/mpg123.py b/pydora/mpg123.py
index 2c5e4d3..e60ab43 100644
--- a/pydora/mpg123.py
+++ b/pydora/mpg123.py
@@ -1,60 +1,6 @@
1import os
2import select 1import select
3import subprocess
4 2
5 3from .utils import iterate_forever, SilentPopen
6def iterate_forever(func, *args, **kwargs):
7 """Iterate over a finite iterator forever
8
9 When the iterator is exhausted will call the function again to generate a
10 new iterator and keep iterating.
11 """
12 output = func(*args, **kwargs)
13
14 while True:
15 try:
16 yield next(output)
17 except StopIteration:
18 output = func(*args, **kwargs)
19
20
21class SilentPopen(subprocess.Popen):
22 """A Popen varient that dumps it's output and error
23 """
24
25 def __init__(self, *args, **kwargs):
26 self._dev_null = open(os.devnull, "w")
27 kwargs["stdin"] = subprocess.PIPE
28 kwargs["stdout"] = subprocess.PIPE
29 kwargs["stderr"] = self._dev_null
30 super(SilentPopen, self).__init__(*args, **kwargs)
31
32 def __del__(self):
33 self._dev_null.close()
34 super(SilentPopen, self).__del__()
35
36
37class PlayerCallbacks(object):
38
39 def play(self, song):
40 """Called once when a song starts playing
41 """
42 pass
43
44 def pre_poll(self):
45 """Called before polling for process status
46 """
47 pass
48
49 def post_poll(self):
50 """Called after polling for process status
51 """
52 pass
53
54 def input(self, value, song):
55 """Called after user input during song playback
56 """
57 pass
58 4
59 5
60class Player(object): 6class Player(object):
diff --git a/pydora/player.py b/pydora/player.py
index 9b99c8c..8cbd7d2 100755
--- a/pydora/player.py
+++ b/pydora/player.py
@@ -16,6 +16,34 @@ from .mpg123 import Player
16from .utils import Colors, Screen 16from .utils import Colors, Screen
17 17
18 18
19class PlayerCallbacks(object):
20 """Interface for Player Callbacks
21
22 This class simply exists to document the interface for callback
23 implementers implementers need not extend this class.
24 """
25
26 def play(self, song):
27 """Called once when a song starts playing
28 """
29 pass
30
31 def pre_poll(self):
32 """Called before polling for process status
33 """
34 pass
35
36 def post_poll(self):
37 """Called after polling for process status
38 """
39 pass
40
41 def input(self, value, song):
42 """Called after user input during song playback
43 """
44 pass
45
46
19class PlayerApp(object): 47class PlayerApp(object):
20 48
21 CMD_MAP = { 49 CMD_MAP = {
@@ -138,7 +166,3 @@ class PlayerApp(object):
138 166
139def main(): 167def main():
140 PlayerApp().run() 168 PlayerApp().run()
141
142
143if __name__ == "__main__":
144 main()
diff --git a/pydora/utils.py b/pydora/utils.py
index 92a15a3..fdbbf8d 100644
--- a/pydora/utils.py
+++ b/pydora/utils.py
@@ -1,8 +1,10 @@
1from __future__ import print_function 1from __future__ import print_function
2 2
3import os
3import sys 4import sys
4import termios 5import termios
5import getpass 6import getpass
7import subprocess
6 8
7 9
8def input(prompt): 10def input(prompt):
@@ -101,3 +103,34 @@ def clear_screen():
101 """ 103 """
102 sys.stdout.write("\x1b[2J\x1b[H") 104 sys.stdout.write("\x1b[2J\x1b[H")
103 sys.stdout.flush() 105 sys.stdout.flush()
106
107
108def iterate_forever(func, *args, **kwargs):
109 """Iterate over a finite iterator forever
110
111 When the iterator is exhausted will call the function again to generate a
112 new iterator and keep iterating.
113 """
114 output = func(*args, **kwargs)
115
116 while True:
117 try:
118 yield next(output)
119 except StopIteration:
120 output = func(*args, **kwargs)
121
122
123class SilentPopen(subprocess.Popen):
124 """A Popen varient that dumps it's output and error
125 """
126
127 def __init__(self, *args, **kwargs):
128 self._dev_null = open(os.devnull, "w")
129 kwargs["stdin"] = subprocess.PIPE
130 kwargs["stdout"] = subprocess.PIPE
131 kwargs["stderr"] = self._dev_null
132 super(SilentPopen, self).__init__(*args, **kwargs)
133
134 def __del__(self):
135 self._dev_null.close()
136 super(SilentPopen, self).__del__()