forked from lachlan-00/rb-fileorganizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileorganizer.py
214 lines (182 loc) · 7.76 KB
/
fileorganizer.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
#!/usr/bin/env python3
""" Fileorganizer
----------------Authors----------------
Lachlan de Waard <[email protected]>
Wolter Hellmund <[email protected]>
----------------Licence----------------
Creative Commons - Attribution Share Alike v3.0
"""
import configparser
import os
import rb
import shutil
from gi.repository import GObject, Peas, PeasGtk, Gtk, Notify, Gio
from gi.repository import RB
import fileops
from configurator import FileorganizerConf
PLUGIN_PATH = 'plugins/fileorganizer/'
CONFIG_FILE = 'fo.conf'
c = "conf"
class Fileorganizer(GObject.Object, Peas.Activatable, PeasGtk.Configurable):
__gtype_name = 'fileorganizer'
object = GObject.property(type=GObject.Object)
def __init__(self):
GObject.Object.__init__(self)
self.configurator = FileorganizerConf()
self.conf = configparser.RawConfigParser()
# Rhythmbox standard Activate method
def do_activate(self):
print("activating Fileorganizer")
shell = self.object
self.shell = shell
self.db = shell.props.db
self._check_configfile()
self.menu_build(shell)
# Rhythmbox standard Deactivate method
def do_deactivate(self):
print("deactivating Fileorganizer")
Gio.Application.get_default().remove_plugin_menu_item('browser-popup', 'selection-organize')
self.action_group = None
self.action = None
#self.source.delete_thyself()
self.source = None
# FUNCTIONS
# check if configfile is present, if not copy from template folder
def _check_configfile(self):
self.configfile = RB.find_user_data_file(PLUGIN_PATH + CONFIG_FILE)
if not os.path.isfile(self.configfile):
template = rb.find_plugin_file(self, 'template/' + CONFIG_FILE)
folder = os.path.split(self.configfile)[0]
if not os.path.exists(folder):
os.makedirs(folder)
shutil.copyfile(template, self.configfile)
# Build menu option
def menu_build(self, shell):
app = Gio.Application.get_default()
# create action
action = Gio.SimpleAction(name="organize-selection")
action.connect("activate", self.organize_selection)
app.add_action(action)
# create menu item
item = Gio.MenuItem()
item.set_label("Organize Selection")
item.set_detailed_action("app.organize-selection")
# add plugin menu item
app.add_plugin_menu_item('browser-popup', "Organize Selection", item)
app.add_action(action)
# Create the Configure window in the rhythmbox plugins menu
def do_create_configure_widget(self):
self.ui_file = rb.find_plugin_file(self, 'config.ui')
b = Gtk.Builder()
b.add_from_file(self.ui_file)
self._check_configfile()
self.conf.read(self.configfile)
window = b.get_object("fileorganizer")
b.get_object("closebutton").connect('clicked', lambda x:
window.destroy())
b.get_object("savebutton").connect('clicked', lambda x:
self.save_config(b))
b.get_object("log_path").set_text(self.conf.get(c, "log_path"))
b.get_object("cover_names").set_text(self.conf.get(c, "cover_names"))
if self.conf.get(c, "log_enabled") == "True":
b.get_object("logbutton").set_active(True)
if self.conf.get(c, "cover_enabled") == "True":
b.get_object("coverbutton").set_active(True)
if self.conf.get(c, "cleanup_enabled") == "True":
b.get_object("cleanupbutton").set_active(True)
if self.conf.get(c, "cleanup_empty_folders") == "True":
b.get_object("removebutton").set_active(True)
if self.conf.get(c, "update_tags") == "True":
b.get_object("tagsbutton").set_active(True)
if self.conf.get(c, "preview_mode") == "True":
b.get_object("previewbutton").set_active(True)
window.show_all()
return window
def save_config(self, builder):
if builder.get_object("logbutton").get_active():
self.conf.set(c, "log_enabled", "True")
else:
self.conf.set(c, "log_enabled", "False")
if builder.get_object("coverbutton").get_active():
self.conf.set(c, "cover_enabled", "True")
else:
self.conf.set(c, "cover_enabled", "False")
if builder.get_object("cleanupbutton").get_active():
self.conf.set(c, "cleanup_enabled", "True")
else:
self.conf.set(c, "cleanup_enabled", "False")
if builder.get_object("removebutton").get_active():
self.conf.set(c, "cleanup_empty_folders", "True")
else:
self.conf.set(c, "cleanup_empty_folders", "False")
if builder.get_object("tagsbutton").get_active():
self.conf.set(c, "update_tags", "True")
else:
self.conf.set(c, "update_tags", "False")
if builder.get_object("previewbutton").get_active():
self.conf.set(c, "preview_mode", "True")
else:
self.conf.set(c, "preview_mode", "False")
self.conf.set(c, "log_path",
builder.get_object("log_path").get_text())
self.conf.set(c, "cover_names",
builder.get_object("cover_names").get_text())
FILE = open(self.configfile, "w")
self.conf.write(FILE)
FILE.close()
# Organize selection
def organize_selection(self, action, shell):
page = self.shell.props.selected_page
if not hasattr(page, "get_entry_view"):
return
selected = page.get_entry_view()
#source = shell.get_property("selected_page")
#entry = RB.Source.get_entry_view(source)
selection = selected.get_selected_entries()
self.process_selection(selection)
#self.organize(selection)
# Process selection: Run in Preview Mode or Normal Mode
def process_selection(self, filelist):
self.conf.read(self.configfile)
# Run in Preview Mode
if self.conf.get(c, "preview_mode") == "True":
if filelist != []:
prelist = os.getenv('HOME') + '/.fileorganizer-preview.log'
FILE = open(prelist, "w")
FILE.close()
damlist = os.getenv('HOME') + '/.fileorganizer-damaged.log'
FILE = open(damlist, "w")
FILE.close()
for item in filelist:
item = fileops.MusicFile(self, item)
item.preview()
Notify.init('Fileorganizer')
title = 'Fileorganizer'
note = 'Preview Has Completed'
notification = Notify.Notification.new(title, note, None)
Notify.Notification.show(notification)
# Show Results of preview
self.results(prelist, damlist)
else:
# Run Normally
self.organize(filelist)
Notify.init('Fileorganizer')
title = 'Fileorganizer'
note = 'Your selection is organised'
notification = Notify.Notification.new(title, note, None)
Notify.Notification.show(notification)
def results(self, prelist, damlist):
if not os.stat(prelist)[6] == 0:
os.system('/usr/bin/xdg-open ' + prelist)
if not os.stat(damlist)[6] == 0:
os.system('/usr/bin/xdg-open ' + damlist)
# Organize array of files
def organize(self, filelist):
if filelist != []:
for item in filelist:
item = fileops.MusicFile(self, item)
item.relocate()
class PythonSource(RB.Source):
def __init__(self):
RB.Source.__init__(self)
GObject.type_register_dynamic(PythonSource)