-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshotwell_sync.py
executable file
·308 lines (247 loc) · 11.7 KB
/
shotwell_sync.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#! /bin/env python3
# encoding: utf-8
import os
from concurrent.futures import Future, ThreadPoolExecutor
from threading import Lock
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, GLib
import sqlalchemy as sql
from shotwell_model import Event, Photo, Issue, Data
thread_pool = ThreadPoolExecutor()
class ThumbnailButton(Gtk.CheckButton):
def __init__(self, filename):
super(ThumbnailButton, self).__init__()
self.set_mode(draw_indicator=False)
self._image = Gtk.Image()
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename, width=220, height=220,preserve_aspect_ratio=True)
self._image.set_from_pixbuf(pixbuf)
self.set_image(self._image)
@property
def selected(self):
return self.get_active()
@selected.setter
def selected(self, active):
self.set_active(active)
class MatchFolderEventWindow(Gtk.Window):
_EVENT = 1
_PATH = 0
_LAST = False
_NEXT = True
def __init__(self, dbsession, data_directory):
self._data_iter = None
self._data = Data()
self._data_directory = data_directory
self.results = {}
self.thumbnails = []
self.dbsession = dbsession
self._busy_lock = Lock()
self._busy_future = Future()
self._cancel_future = Future()
#GUI
Gtk.Window.__init__(self, title="Shotwell folder <-> event sync")
self.set_border_width(10)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
self.progressbar = Gtk.ProgressBar()
vbox.pack_start(self.progressbar, False, True, 0)
self.label = Gtk.Label()
vbox.pack_start(self.label, False, True, 0)
select_all_button = Gtk.Button(label="Alle Auswählen")
select_all_button.connect("clicked", self.toggle_select_all_images)
vbox.pack_start(select_all_button, False, False, 0)
self._scrolled_thumbnails = Gtk.ScrolledWindow()
self._scrolled_thumbnails.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
self._busy_progressbar = Gtk.ProgressBar()
self._busy_progressbar.set_show_text(True)
self.set_busy_fraction(0, 1)
vbox.pack_start(self._busy_progressbar, False, True, 0)
self._thumbnailgrid_lock = Lock()
self._thumbnailgrid = Gtk.FlowBox()
self._thumbnailgrid.set_valign(Gtk.Align.START)
self._thumbnailgrid.set_max_children_per_line(30)
self._thumbnailgrid.set_selection_mode(Gtk.SelectionMode.NONE)
self._scrolled_thumbnails.add(self._thumbnailgrid)
vbox.pack_start(self._scrolled_thumbnails, True, True, 0)
self.button = []
chooseBox = Gtk.Box(spacing=6)
vbox.pack_start(chooseBox, False, True, 0)
path_label = Gtk.Label(label="Pfad:")
chooseBox.pack_start(path_label, False, True, 0)
self.button.insert(self._PATH, Gtk.Button(label="Pfad"))
self.button[self._PATH].connect("clicked", self.chose, self._PATH)
chooseBox.pack_start(self.button[self._PATH], True, True, 0)
self.entry = Gtk.Entry()
chooseBox.pack_start(self.entry, True, True, 0)
event_label = Gtk.Label(label="Event:")
chooseBox.pack_start(event_label, False, True, 0)
self.button.insert(self._EVENT, Gtk.Button(label="Event"))
self.button[self._EVENT].connect("clicked", self.chose, self._EVENT)
chooseBox.pack_start(self.button[self._EVENT], True, True, 0)
CASBox = Gtk.Box(spacing=6)
vbox.pack_start(CASBox, False, True, 0)
lastButton = Gtk.Button(label="Last", use_underline=True)
lastButton.connect("clicked", self.next, self._LAST)
CASBox.pack_start(lastButton, True, True, 0)
nextButton = Gtk.Button(label="next", use_underline=True)
nextButton.connect("clicked", self.next, self._NEXT)
CASBox.pack_start(nextButton, True, True, 0)
self._iter_buttons = {self._NEXT: nextButton, self._LAST: lastButton}
commitButton = Gtk.Button(label="Commit", use_underline=True)
commitButton.connect("clicked", self.commit)
CASBox.pack_start(commitButton, True, True, 0)
self.scan()
def set_busy_fraction(self, fraction, all):
self._busy_progressbar.set_fraction(fraction/all)
self._busy_progressbar.set_text("%s of %s" % (fraction, all))
def toggle_select_all_images(self, sender):
all_selected = all(image_button.selected for image_button, image_file in self.thumbnails)
for image_button, image_file in self.thumbnails:
image_button.selected = not all_selected
def _add_images_async(self, issue):
self.clear_images()
self._cancel_future = Future()
self._busy_future = thread_pool.submit(self._load_images, issue=issue, cancel_future=self._cancel_future)
self._busy_future.add_done_callback(self._add_images_done_callback)
def _load_images(self, issue, cancel_future):
files_len = len(issue.files)
for i, image_file in enumerate(issue.files):
if cancel_future.cancelled():
break
image_button = ThumbnailButton(image_file.filename)
GLib.idle_add(self._add_image, image_button, image_file)
self.set_busy_fraction(i+1, files_len)
def _add_image(self, image_button, image_file):
if self._cancel_future.cancelled():
return
with self._thumbnailgrid_lock:
self.thumbnails.append((image_button, image_file))
self._thumbnailgrid.add(image_button)
self._thumbnailgrid.show_all()
def _add_images_done_callback(self, future):
GLib.idle_add(self._add_images_done)
def _add_images_done(self):
if self._busy_lock.locked():
self._busy_lock.release()
self._label_iter_buttons_reset()
else:
raise Exception("Programming error: self._busy_lock allready released!?")
self._busy_future.result() # raise catched exceptions
def clear_images(self):
with self._thumbnailgrid_lock:
for thumbnail_fb_child in self._thumbnailgrid.get_children():
self._thumbnailgrid.remove(thumbnail_fb_child)
self._thumbnailgrid.hide()
childcount = len(self._thumbnailgrid.get_children())
print(childcount)
self.thumbnails.clear()
def scan(self):
for event in self.dbsession.query(Event).all():
if event.name is None:
if any(p.filename.lower().endswith(".jpg") for p in event.photos):
print("JPGs in None-named Event: {}".format([p.filename for p in event.photos
if p.filename.lower().endswith(".jpg")]))
# TODO: do not fully ignore None-named events
continue
for photo in event.photos:
photo_path = photo.filename.split("/")
if ".jpg" in photo_path[-1].lower():
folder = event.name # folders[2] --ignore
else:
folder = "/".join(photo_path[3:-1])
if event.name is None:
if os.path.exists(photo.filename):
print(folder + " # --kein event zugeordet!?--")
else:
print(photo.filename + " was not found on disc!")
elif folder != event.name:
if folder not in self._data:
self._data[folder] = Issue(folder, event, [photo])
else:
self._data[folder].files.append(photo)
self.progressbar.set_fraction(0.0)
self.progressbar.set_text("%s of %s" % (0, len(self._data)))
self.progressbar.set_show_text(True)
self._data_iter = iter(self._data)
self.next(None, True)
def fill_view(self, issue: Issue):
print(issue.folder, issue.event)
self.button[self._PATH].set_label("{0:^50}".format(issue.folder))
self.button[self._EVENT].set_label("{0:^50}".format(issue.event.name))
self._add_images_async(issue)
def chose(self, button, chosen):
if chosen is self._PATH:
self.entry.set_text(self._data_iter.this().folder)
elif chosen is self._EVENT:
self.entry.set_text(self._data_iter.this().event.name)
else:
self.entry.set_text("")
def _label_next_button_cancel(self):
for button in self._iter_buttons.values():
button.set_label("Cancel")
def _label_iter_buttons_reset(self):
for direction in self._iter_buttons:
self._iter_buttons[direction].set_label("Next" if direction is self._NEXT else "Last")
def next(self, button, direction):
if self._busy_lock.acquire(blocking=False):
self._label_next_button_cancel()
if direction:
current_issue = self._data_iter.next()
else:
current_issue = self._data_iter.prev()
self.fill_view(current_issue)
self.progressbar.set_fraction((int(self._data_iter) + 1.0) / len(self._data))
self.progressbar.set_text("%2s of %2s" % (int(self._data_iter), len(self._data)))
self.entry.set_text("")
else:
self._cancel_future.cancel()
def commit(self, button):
issue = self._data_iter.this()
label = self.entry.get_text()
if not label:
print("keep both %s and %s" % (r[1][self._PATH], r[1][self._EVENT]))
else:
if label != issue.event.name:
print("rename event %s to %s: " % (issue.event, label))
issue.event.name = label
# self.dbsession.merge(issue.event)
if label != issue.folder:
prefix = "/".join(issue.files[0].filename.split("/")[:3])
dst = prefix + "/" + label
print("move photos from %s to %s" % (prefix + issue.folder, dst))
try:
print("os.mkdir(dst)")
except FileExistsError:
print("mkdir: %s already exists.." % dst)
for p in issue.files:
ph = self.dbsession.query(Photo).get(p.id)
oldfilename = ph.filename
ph.filename = dst + "/"+ ph.filename.split("/")[-1]
tryagain = True
while tryagain:
try:
pass
# os.rename(oldfilename, ph.filename)
print("os.rename( %s, %s )" % (oldfilename,ph.filename))
tryagain = False
except FileExistsError:
print("os.rename: %s already existed!" % ph.filename)
ph.filename = ph.filename.lower().replace(".jpg", "1.jpg")
# session.merge(ph)
# session.commit()
self.dbsession.commit()
self.results = {}
self.scan()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Shotwell Event <-> Folder Sync")
parser.add_argument("db_path", nargs="?", type=str, default="/home/poku/.local/share/shotwell/data/photo.db")
parser.add_argument("data_directory", nargs="?", type=str, default="/data/Fotos")
args = parser.parse_args()
engine = sql.create_engine("sqlite:///"+args.db_path)
connection = engine.connect()
Session = sql.orm.sessionmaker(bind=engine)
win = MatchFolderEventWindow(Session(), data_directory=args.data_directory)
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()