-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.py
287 lines (215 loc) · 7.45 KB
/
browser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from re import compile, IGNORECASE
import traceback
from common import Listenable
from list import List
from status import Song
from wrapper import MPDWrapper
class Path(object):
def __init__(self, s=""):
self.list = []
for o in s.split("/"):
self.push(o)
def __str__(self):
return "/".join(self.list)
def copy(self):
return Path(unicode(self))
def name(self):
if len(self.list) > 0:
return self.list[-1]
return None
def pop(self):
if len(self.list) > 0:
del self.list[-1]
def push(self, s):
if len(s) > 0:
self.list.append(s)
class BrowserNode(object):
def __init__(self, mpd, parent, ntype):
self.mpd = mpd
self.parent = parent
self.ntype = ntype
class SongNode(BrowserNode):
def __init__(self, mpd, data, parent):
super(SongNode, self).__init__(mpd, parent, "song")
self.data = data
self.path = Path(data.file)
def __str__(self):
return "%s - %s (%s)" % (self.data.artist,
self.data.title, self.data.album)
class PlaylistNode(BrowserNode):
def __init__(self, mpd, data, parent):
super(PlaylistNode, self).__init__(mpd, parent, "playlist")
self.data = data
self.path = Path(data)
def __str__(self):
return self.data
class InternalNode(BrowserNode):
def __init__(self, mpd, parent, ntype):
self.mpd = mpd
self.parent = parent
self.ntype = ntype
self.children = []
self.sel = -1
def __getitem__(self, index):
return self.children[index]
def __len__(self):
return len(self.children)
def find_next(self, reg):
rl = compile(reg, IGNORECASE)
# TODO beautify
for i in range(self.sel + 1, len(self)) + range(0, self.sel):
if rl.search(unicode(self[i])):
return i
return -1
def select(self, index, rel=False):
self.sel = (self.sel + index) if rel else index
self.sel = min(max(0, self.sel), len(self) - 1)
return self.sel
def select_node(self, node):
self.select(self.children.index(node))
def selected(self):
if self.sel >= 0:
return self.children[self.sel]
return None
class DirectoryNode(InternalNode):
def __init__(self, mpd, path, parent):
super(DirectoryNode, self).__init__(mpd, parent, "directory")
self.path = path
def __str__(self):
return self.path.name() + "/"
def load(self):
children = []
try:
for v in self.mpd.ls(unicode(self.path)):
if "directory" in v:
c = DirectoryNode(self.mpd, Path(v["directory"]), self)
c.load()
children.append(c)
elif "playlist" in v:
c = PlaylistNode(self.mpd, v["playlist"], self)
#children.append(c)
elif "file" in v:
c = SongNode(self.mpd, Song(v), self)
children.append(c)
except:
traceback.print_exc()
raise
# Add links to root and previous directory
if self.parent != None:
children.insert(0, LinkNode(self.mpd, self, Path(), "/"))
children.insert(1, LinkNode(self.mpd, self,
self.parent.path.copy(), "../"))
self.children = children
self.select(0)
def lookup(self, name):
for n in self.children:
if n.path.name() and n.path.name() == name:
return n
return None
class SearchNode(InternalNode):
def __init__(self, mpd, s):
super(InternalNode, self).__init__(mpd, None, "search")
self.string = s
self.rl = map(lambda s: compile(s, IGNORECASE), self.string.split())
self.path = Path()
def _search(self, node):
for n in node.children:
if n.ntype == "directory":
self._search(n)
elif n.ntype == "song" and n.data.matches_all(self.rl):
self.children.append(n)
def search(self, tree):
self.children = []
self._search(tree)
self.select(0)
class LinkNode(BrowserNode):
def __init__(self, mpd, parent, path, name=None):
super(LinkNode, self).__init__(mpd, parent, "link")
self.name = name if name != None else path.name()
self.path = path
def __str__(self):
return self.name
class Browser(Listenable):
def __init__(self, mpd):
super(Browser, self).__init__()
self.mpd = mpd
self.tree = DirectoryNode(mpd, Path(), None)
self.curr_node = None
self.prev_node = None
self.search_node = None
def _set_selected(self, node):
if node:
self.curr_node = node
self.notify("browser_node_changed", self)
def enter(self):
selnode = self.curr_node.selected()
if selnode != None:
if selnode.ntype == "song":
self.mpd.add_and_play(selnode.data.file)
elif selnode.ntype == "playlist":
print(selnode.data) # TODO
elif selnode.ntype == "directory":
self._set_selected(selnode)
elif selnode.ntype == "link":
self.curr_node.select(0)
self.go_to(selnode.path)
def find_next(self, reg):
return self.curr_node.find_next(reg)
def go_to(self, path):
node = self.tree
for p in path.list:
node = node.lookup(p)
if node == None:
return False
if node.ntype == "song":
node.parent.select_node(node)
self._set_selected(node.parent)
elif node.ntype == "directory":
self._set_selected(node)
return True
def go_up(self):
if self.curr_node and self.curr_node.parent:
self.curr_node.select(0) # Restore selected for current node
self._set_selected(self.curr_node.parent)
def load(self):
self.tree.load()
if self.search_active():
self.search(None)
self._set_selected(self.tree)
def path_str(self, delim="/"):
if self.curr_node != None:
return delim.join(self.curr_node.path.list)
return ""
def _search_start(self, s):
self.search_node = SearchNode(self.mpd, s)
self.search_node.search(self.tree)
self.prev_node = self.curr_node
self.curr_node = self.search_node
self.notify("browser_search_started", self)
def _search_stop(self):
self.curr_node = self.prev_node
self.search_node = None
self.notify("browser_search_stopped", self)
def search(self, s):
if self.curr_node is self.search_node:
self._search_stop()
if s != None:
self._search_start(s)
def search_active(self):
return self.search_node != None
def select(self, index, rel=False):
self.curr_node.select(index, rel)
self.notify("browser_selected_changed", self)
def selected(self):
return self.curr_node.selected()
class BrowserListener(object):
def browser_node_changed(self, browser):
pass
def browser_selected_changed(self, browser):
pass
def browser_search_started(self, browser):
pass
def browser_search_stopped(self, browser):
pass