-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmud_gui.py
More file actions
288 lines (241 loc) · 9.73 KB
/
mud_gui.py
File metadata and controls
288 lines (241 loc) · 9.73 KB
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
288
"""
mud_gui.py
Currently just a poc - a gui to review the doublers.
"""
"""
#TODO:
* get rid of global variables
* give message if no more songs in queue
* create AudioPlayer from common class of VideoPlayer
* create gui for accessing duplicate directories/Albums
"""
# a place to move duplicates to
DUPES_DIR = '/var/tmp/dups'
import argparse
import remi.gui as gui
from remi import start, App
from remi import Widget
from remi.gui import decorate_constructor_parameter_types, decorate_set_on_listener, VideoPlayer
import pickle
from pprint import pprint
from multiprocessing import Process
from multiprocessing import Queue as MPQueue
import socket
import sys
import logging
import settings
import mud
from dedup import dedup
queue = MPQueue()
logger = logging.getLogger('mud_gui')
logger.setLevel(logging.DEBUG)
class AudioPlayer(Widget):
# some constants for the events
EVENT_ONENDED = 'onended'
@decorate_constructor_parameter_types([str, str, bool, bool])
def __init__(self, audio, poster=None, autoplay=False, loop=False, **kwargs):
super(AudioPlayer, self).__init__(**kwargs)
logger.debug('Creating Audio player for ' + audio)
self.type = 'audio'
self.attributes['src'] = audio
self.attributes['preload'] = 'auto'
self.attributes['type'] = 'audio/mpeg' # maybe this should be set according to file ending ...
self.attributes['controls'] = None
self.attributes['poster'] = poster
self.set_autoplay(autoplay)
self.set_loop(loop)
def set_autoplay(self, autoplay):
if autoplay:
self.attributes['autoplay'] = 'true'
else:
self.attributes.pop('autoplay', None)
def set_loop(self, loop):
"""Sets the VideoPlayer to restart video when finished.
Note: If set as True the event onended will not fire."""
if loop:
self.attributes['loop'] = 'true'
else:
self.attributes.pop('loop', None)
def onended(self):
"""Called when the media has been played and reached the end."""
return self.eventManager.propagate(self.EVENT_ONENDED, ())
@decorate_set_on_listener("onended", "(self,emitter)")
def set_on_ended_listener(self, callback, *userdata):
"""Registers the listener for the VideoPlayer.onended event.
Note: the listener prototype have to be in the form on_video_ended(self, widget).
Args:
callback (function): Callback function pointer.
"""
self.attributes['onended'] = "sendCallback('%s','%s');" \
"event.stopPropagation();event.preventDefault();" % (self.identifier, self.EVENT_ONENDED)
self.eventManager.register_listener(self.EVENT_ONENDED, callback, *userdata)
class MudConnector(object):
"""Connect to the mud database"""
def __init__(self, inst_num=0):
"""
Create a mud object for getting duplicates
inst_nume: integer, the mud instance number
"""
logger.debug('Creating MudConnector')
self.inst_num = inst_num
def __enter__(self):
"""
Start another process to fetch stuff from the database
"""
logger.debug('Spawning process for mud connection')
self.process = Process(target=mud.fill_dupes, args=(self.inst_num, queue))
self.process.start()
return self
def __exit__(self, exc_type, exc_value, traceback):
logger.debug('Stopping process for mud connection')
self.process.join()
def get_dup():
"""
Return a dup thingy
"""
logger.debug('Getting a dup item of the queue')
files_from_q = queue.get()
if not files_from_q: return None
files = []
for song_file in files_from_q:
files.append({
'file': song_file['file_path'],
})
return {
'files': files,
'reviewed': False,
}
class MudGui(App):
def __init__(self, *args):
self.static_path = settings.music_base_dir
super(MudGui, self).__init__(*args, static_file_path=self.static_path)
logger.debug('MudGui created')
def main(self):
logger.debug('Inside MudGui.main')
# returning the root widget
self.verticalContainer = gui.Widget(width=600)
#self.verticalContainer = gui.VBox(width=600)
#container = gui.VBox(width = 120, height = 100)
self.verticalContainer.style['display'] = 'block'
self.verticalContainer.style['overflow'] = 'hidden'
# control at the bottom
self.subcontainerBottom = horizontal_container()
self.done_bt = self._simple_button('Done')
self.skip_bt = self._simple_button('Skip')
self.subcontainerBottom.append(self.done_bt)
self.subcontainerBottom.append(self.skip_bt)
self.verticalContainer.append(self.dup_container(), key='duplicates')
self.verticalContainer.append(self.subcontainerBottom, key='bottom_control')
# returning the root widget
return self.verticalContainer
def dup_container(self):
"""Return a list of containers"""
dupContainer = horizontal_container()
self.dup_set = get_dup()
if not self.dup_set: return dupContainer
i = 0;
for song in self.dup_set['files']:
dupContainer.append(self.song_container(song), key=str(i))
i += 1
return dupContainer
def song_container(self, song):
"""
Create a container for a song file
song: dict, containing 'file', ...
"""
song_container = horizontal_container()
label = gui.Label(song['file'], width=250, height=50)
check = gui.CheckBoxLabel('keep', False, width=60, height=90)
check.style['margin'] = '10px'
partial_song_path = '/res' + song['file'].replace(self.static_path, '')
#TODO: check if partial song_path starts with a '/'
import urllib
partial_song_path = urllib.quote(partial_song_path)
audio = AudioPlayer(partial_song_path, width=120, height=30)
audio.style['margin'] = '10px'
song_container.append(check, key='check')
song_container.append(audio, key='audio')
song_container.append(label, key='label')
return song_container
def _simple_button(self, name):
"""
Create and return a simple blutton and register the on click listener name.lower() + '_button_pressed'
name: str, as it appears on the button
"""
logger.debug('Creating _simple_button with name "' + name + '"')
button = gui.Button(name, width=150, height=30)
button.style['margin'] = '10px'
listener_name = name.lower() + '_button_pressed'
listener_func = getattr(self, listener_name)
button.set_on_click_listener(listener_func)
return button
def done_button_pressed(self, widget):
logger.debug('Done button pressed')
dup_cont = self.verticalContainer.get_child('duplicates')
song_containers = []
reviewed = False
i = 0
while i < len(self.dup_set['files']):
song_container = dup_cont.get_child(str(i))
check = song_container.get_child('check')
self.dup_set['files'][i]['keep'] = check.get_value()
if check.get_value(): reviewed = True
i += 1
self.dup_set['reviewed'] = reviewed
pprint(self.dup_set)
self.move_files()
if reviewed: self.next_dupset()
else: pass # emit message like "use 'Next' if you don't want to decide right now"
def skip_button_pressed(self, widget):
logger.debug('Skip button pressed')
pprint(self.dup_set)
self.next_dupset()
def next_dupset(self):
"""Get the next set of dups"""
logger.debug('MyApp.next_dupset called')
self.verticalContainer.append(self.dup_container(), key='duplicates')
self.verticalContainer.append(self.subcontainerBottom, key='bottom_control')
def move_files(self):
"""
Move files that were not chosen to be kept
"""
if not self.dup_set['reviewed']: return
for song in self.dup_set['files']:
if song['keep']: continue
logger.debug('Moving ' + song['file'] + ' to ' + dedup.dir_dupes_target)
#dedup.dir_dupes_target = '/var/tmp'
dedup.move_files([song['file'],])
def horizontal_container():
"""Create a default horizontal container"""
horizontalContainer = gui.Widget(width='100%')
horizontalContainer.set_layout_orientation(gui.Widget.LAYOUT_HORIZONTAL)
horizontalContainer.style['display'] = 'block'
horizontalContainer.style['overflow'] = 'auto'
horizontalContainer.style['margin'] = '0px'
return horizontalContainer
def guess_ip():
"""Guess IP to listen on"""
return socket.gethostbyname(socket.gethostname())
def parse_cli_args():
"""
Parse cli args and return an object with the given args
"""
parser = argparse.ArgumentParser(
description='Start web gui to present and discard the found doubles.')
guessed_ip = guess_ip()
parser.add_argument('-i', '--ip-address',
type=str,
default=guessed_ip,
help='The ip address to listen on. Defaults to ' + guessed_ip)
return parser.parse_args()
def main():
"""The main function"""
cli_args = parse_cli_args()
dedup.simulate = True
dedup.dir_with_dupes = settings.music_base_dir
dedup.dir_dupes_target = DUPES_DIR
with MudConnector() as mud_connector:
start(MudGui, debug=True, address=cli_args.ip_address, port=8000, websocket_port=8001, start_browser=False)
#start(MudApp, debug=True, address='10.23.23.53', port=8000, websocket_port=8001, start_browser=False,username='isaac', password='xxxxx' )
if __name__ == '__main__':
main()