diff options
author | Mike Crute <mcrute@gmail.com> | 2015-07-19 21:03:19 -0700 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2015-07-19 21:03:19 -0700 |
commit | c07bcaa8d008b6ef8058765f435bae6a858225c2 (patch) | |
tree | 7bd59bea538dafcc37f4a67bfb628aaac632905e /pydora | |
parent | 388fa58fa1151a527ffd14678e4d885a85e8021e (diff) | |
download | pydora-c07bcaa8d008b6ef8058765f435bae6a858225c2.tar.bz2 pydora-c07bcaa8d008b6ef8058765f435bae6a858225c2.tar.xz pydora-c07bcaa8d008b6ef8058765f435bae6a858225c2.zip |
Reorganize pydora player code
Diffstat (limited to 'pydora')
-rw-r--r-- | pydora/mpg123.py | 56 | ||||
-rwxr-xr-x | pydora/player.py | 32 | ||||
-rw-r--r-- | pydora/utils.py | 33 |
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 @@ | |||
1 | import os | ||
2 | import select | 1 | import select |
3 | import subprocess | ||
4 | 2 | ||
5 | 3 | from .utils import iterate_forever, SilentPopen | |
6 | def 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 | |||
21 | class 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 | |||
37 | class 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 | ||
60 | class Player(object): | 6 | class 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 | |||
16 | from .utils import Colors, Screen | 16 | from .utils import Colors, Screen |
17 | 17 | ||
18 | 18 | ||
19 | class 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 | |||
19 | class PlayerApp(object): | 47 | class PlayerApp(object): |
20 | 48 | ||
21 | CMD_MAP = { | 49 | CMD_MAP = { |
@@ -138,7 +166,3 @@ class PlayerApp(object): | |||
138 | 166 | ||
139 | def main(): | 167 | def main(): |
140 | PlayerApp().run() | 168 | PlayerApp().run() |
141 | |||
142 | |||
143 | if __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 @@ | |||
1 | from __future__ import print_function | 1 | from __future__ import print_function |
2 | 2 | ||
3 | import os | ||
3 | import sys | 4 | import sys |
4 | import termios | 5 | import termios |
5 | import getpass | 6 | import getpass |
7 | import subprocess | ||
6 | 8 | ||
7 | 9 | ||
8 | def input(prompt): | 10 | def 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 | |||
108 | def 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 | |||
123 | class 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__() | ||