Plugins: copyfiles.2.py

File copyfiles.2.py, 3.4 kB (added by felix, 3 months ago)

Updated version of the previous copyfiles.py. Now you can symlink the files instead and also specify the output format using tags.

Line 
1 # Copyright 2008 Felix McCoey <felix.mccoey@gmail.com>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2 as
5 # published by the Free Software Foundation
6
7 import os
8 import gtk
9 from plugins.songsmenu import SongsMenuPlugin
10 import config
11 import shutil
12 from parse import FileFromPattern
13
14 try: config.get("plugins", __name__+'_link')
15 except: config.set("plugins", __name__+'_link', 'False')
16 try: config.get("plugins", __name__+'_pattern')
17 except: config.set("plugins", __name__+'_pattern', '/tmp/<artist>/<tracknumber>_<title>')
18 try: config.get("plugins", __name__+'_spaces')
19 except: config.set("plugins", __name__+'_spaces', 'True')
20
21 class CopyFiles(SongsMenuPlugin):
22   PLUGIN_ID = "Copy Files"
23   PLUGIN_NAME = _("Copy Files")
24   PLUGIN_DESC = _("Copy (or link) the selected song list to a directory")
25   PLUGIN_ICON = gtk.STOCK_CONVERT
26   PLUGIN_VERSION = "0.2"
27
28   c_link = __name__ + '_link'
29   c_pattern = __name__ + '_pattern'
30   c_spaces = __name__ + '_spaces'
31
32   def plugin_songs(self, songs):
33     if not songs: return
34
35     msgbox = gtk.MessageDialog(
36         parent=None,
37         flags=0,
38         type=gtk.MESSAGE_INFO,
39         buttons=gtk.BUTTONS_OK,
40         message_format=None)
41
42     try:
43       for song in songs:
44         self.__copy(song)
45     except Exception, e:
46       msgbox.set_markup("An error occurred: %s" % e)
47     else:
48       msgbox.set_markup("Copy successful")
49
50     msgbox.run()
51     msgbox.destroy()
52
53   def __copy(self, song):
54     pattern = config.get('plugins', self.c_pattern)
55     target = FileFromPattern(pattern) % song
56     if config.getboolean('plugins', self.c_spaces):
57       target = target.replace(" ","_")
58     source_file = song["~filename"]
59     # make the path
60     try:
61       os.makedirs(os.path.dirname(target))
62     except: pass
63
64     make_links = config.getboolean('plugins', self.c_link)
65     try:
66       if make_links:
67         os.symlink(source_file,target)
68       else:
69         shutil.copyfile(source_file,target)
70     except: raise
71
72   def PluginPreferences(self, parent):
73     def pattern_changed(entry):
74       config.set('plugins', self.c_pattern, entry.get_text())
75
76     def link_changed(entry):
77       config.set('plugins', self.c_link, str(entry.get_active()))
78
79     def spaces_changed(entry):
80       config.set('plugins', self.c_spaces, str(entry.get_active()))
81
82     tooltips = gtk.Tooltips().set_tip
83     vb = gtk.VBox(spacing = 3)
84     pattern_box = gtk.HBox(spacing = 3)
85     pattern_box.set_border_width(3)
86     pattern = gtk.Entry()
87     pattern.set_text(config.get('plugins',self.c_pattern))
88     tooltips(pattern, "Enter the absolute path, using tags if need be, for the target location.")
89     pattern_box.pack_start(gtk.Label("Destination:"), expand = False)
90     pattern_box.pack_start(pattern)
91
92     link = gtk.CheckButton(label="Link files, don't copy")
93     link.set_active(config.getboolean('plugins', self.c_link))
94     tooltips(link, "Symbolically link the files instead of copying them.")
95
96     spaces = gtk.CheckButton(label="Convert spaces to underscores")
97     spaces.set_active(config.getboolean('plugins', self.c_spaces))
98     tooltips(spaces, "Convert spaces in the path to underscores.")
99
100     pattern.connect('changed', pattern_changed)
101     link.connect('toggled',link_changed)
102     spaces.connect('toggled',spaces_changed)
103
104     vb.pack_start(pattern_box)
105     vb.pack_start(link)
106     vb.pack_start(spaces)
107     return vb
108
109   PluginPreferences = classmethod(PluginPreferences)