mutagen for titles and directories on command line
[riffle.git] / sync.py
blob726dca80221027a98a44262ef7e35abc99472796
1 #!/usr/bin/env python
2 import gtk
3 import os, sys
5 try: import mutagen
6 except ImportError:
7 sys.path.append(os.path.expanduser("~/src/ql/trunk/mutagen"))
8 import mutagen
10 SUPPORTED_EXTENSIONS = '.mp3'
11 # list store columns
12 TITLE=0
13 BACKGROUND=1
14 FULLPATH=2
15 LOCATION=3
16 # locations
17 INCOMING = 0
18 DEVICE = 1
19 # actual paths
20 LOC = {}
21 LOC[INCOMING] = "media"
22 LOC[DEVICE] = "device"
25 def list_files(top, ext):
26 if isinstance(ext,list):
27 accept = lambda e: e in ext
28 else:
29 accept = lambda e: e == ext
30 for r,ds,fs in os.walk(top):
31 for f in fs:
32 if accept( os.path.splitext(f)[1] ):
33 yield os.path.join(r,f)
35 def get_device_tracks():
36 return list_files(LOC[DEVICE], SUPPORTED_EXTENSIONS)
38 def get_incoming_tracks():
39 return list_files(LOC[INCOMING], SUPPORTED_EXTENSIONS)
41 class Sync:
42 def __init__(self):
43 self.window = gtk.Window()
44 self.window.set_title('sync')
45 self.window.connect('destroy', gtk.main_quit)
47 vbox = gtk.VBox()
49 scroll = gtk.ScrolledWindow()
50 scroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
51 vbox.add(scroll)
53 self.main_list = gtk.TreeView()
54 self.main_list.set_headers_visible(False)
56 text_renderer = gtk.CellRendererText()
57 column = gtk.TreeViewColumn("track", text_renderer)
58 column.add_attribute(text_renderer, "text", TITLE)
59 column.add_attribute(text_renderer, "background", BACKGROUND)
61 self.main_list.append_column( column )
62 scroll.add(self.main_list)
64 go_button = gtk.Button('Go!')
65 go_button.connect("clicked", self.do_sync)
66 vbox.add_with_properties(go_button,'expand',False)
68 self.window.add(vbox)
70 self.window.resize(400,600)
71 self.window.show_all()
73 def add_entries(self, lst):
74 if self.main_list.get_model() is None:
75 self.main_list.set_model( gtk.ListStore(str,str,str,int) )
76 for i in lst:
77 self.main_list.get_model().append( i )
79 def do_sync(self,*argv,**kw):
80 print "sync"
81 for row in self.main_list.get_model():
82 if row[LOCATION] == INCOMING:
83 print "mv %s %s" % (row[FULLPATH], LOC[DEVICE])
85 def track_title(t):
86 try: return mutagen.File(t).tags['TIT2']
87 except: return os.path.basename(t)
89 try:
90 LOC[DEVICE] = sys.argv[1]
91 LOC[INCOMING] = sys.argv[2]
92 except IndexError:
93 pass
95 app = Sync()
96 for entry in [(track_title(t),'#d0d0f0',t,DEVICE) for t in get_device_tracks()]:
97 app.add_entries([entry])
98 for entry in [(track_title(t),'#90ff90',t,INCOMING) for t in get_incoming_tracks()]:
99 app.add_entries([entry])
100 gtk.main()