-
Notifications
You must be signed in to change notification settings - Fork 0
/
picturedb.py
433 lines (335 loc) · 11.5 KB
/
picturedb.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright © 2012-2013 Martin Ueding <dev@martin-ueding.de>
"""
Library that encapsulates most of the logic in the picture database. There are
classes to model images and tags on those images. Additionally, there are
methods that can act on colletions of images.
"""
from iptcinfo import IPTCInfo
import itertools
import logging
import os.path
import re
import uuid
__docformat__ = "restructuredtext en"
next_id = 1
logging.basicConfig(level=logging.FATAL)
class Tag(object):
"""
Models a tag.
To make the tag filename safe, it does the following:
- Space with ``_``
"""
replacements = [
(' ', '_'),
]
"""
List with replacements to make them filename safe.
"""
def __init__(self, text):
"""
Creates a new Tag from human readable text.
:param text: Human readable text.
"""
self.text = text
@staticmethod
def from_escaped(escaped):
"""
Creates a new Tag from escaped text.
:param escaped: Escaped text.
:return: Unescaped text.
"""
unescaped = re.sub(r"_", " ", escaped)
for replace, pattern in Tag.replacements:
unescaped = re.sub(pattern, replace, unescaped)
return Tag(unescaped)
def escape(self):
"""
Makes the given tag name file name safe.
:return: Escaped string.
"""
escaped = self.text
for pattern, replace in self.replacements:
escaped = re.sub(pattern, replace, escaped)
return escaped
def __str__(self):
return self.text
def __repr__(self):
return "Tag('{}')".format(self.text)
def __lt__(self, other):
return self.text < other.text
def __eq__(self, other):
return self.text == other.text
def __hash__(self):
return hash(self.text)
class Image(object):
"""
Models an image filename with tags.
"""
def __init__(self, filename):
logging.info('Creating new Image from “{}”.'.format(filename))
self.basename = ""
self.date = ""
self.dirname = ""
self.event = ""
self.number = ""
self.origname = ""
self.prefix = ""
self.suffix = ""
self.iptc = None
self.tags = set()
self.origname = filename
self.dirname = os.path.dirname(filename)
self.basename = os.path.basename(filename)
self._parse_folder_name()
self._parse_filename()
self._load_iptc()
def add_tag(self, tag):
"""
Adds the given tag.
:param tag: Tag to add.
:type tag: Tag
:raise TypeError: Raised if not a :py:class:`Tag` given.
"""
if not isinstance(tag, Tag):
raise TypeError("Image::add_tag(hashtags.Tag)")
self.tags.add(tag)
def remove_tag(self, tag):
"""
Removes the tag, if it is there.
If the given tag is not in the set, no error is printed.
:param tag: Tag to remove.
:type tag: Tag
"""
if not isinstance(tag, Tag):
raise TypeError("Image::remove_tag(hashtags.Tag)")
self.tags.discard(tag)
def __repr__(self):
return "Image('{}')".format(self.current_path())
def rename(self):
"""
Performs the actual renaming.
If the file exists, it will try to increase the picture number. If the
attribute :py:attr:`tempname` is set, this name will be used instead of
:py:attr:`origname`.
"""
newname = self.current_path()
if os.path.isfile(newname):
print 'File “{}” already exists.'.format(newname)
answer = raw_input('Do you want to increase the number? [Y/n] ')
if answer != "n":
while os.path.isfile(newname):
self.number = str(int(self.number) +1)
newname = self.current_path()
print 'Now using “{}”.'.format(newname)
assert not os.path.isfile(newname)
oldname = self.origname
try:
oldname = self.tempname
except AttributeError:
pass
logging.info('Renaming “{}” to “{}”.'.format(self.origname, newname))
os.rename(oldname, newname)
def _tagstring(self):
tagstring = ""
if len(self.tags) > 0:
tagstring = "#" + "#".join(sorted([tag.escape() for tag in self.tags]))
return tagstring
def current_path(self):
"""
Gives the current path of this image.
:raises FilenameTooLongError: Raised if generated filename is longer than the filesystem probably supports.
:return: Current path.
:rtype: str
"""
filename = "{}-{}-{}{}.{}".format(
self.date, self.event, self.number, self._tagstring(), self.suffix
)
pathname = os.path.join(self.dirname, filename)
if len(pathname) > 256:
raise FilenameTooLongError("Filename “{}” is longer than 256 chars.".format(pathname))
return pathname
def _parse_filename(self):
"""
Parses the filename to find the hashtags.
:raises FilenameParseError: Raised if name could not be parsed.
"""
m = re.match(r"([^#]+)(#.*)*\.(\w+)", self.basename)
if m is None:
raise FilenameParseError('Could not parse “{}”.'.format(self.basename))
if not m.group(2) is None:
taglist = m.group(2).split("#")
for tag in taglist:
if len(tag) > 0:
self.add_tag(Tag.from_escaped(tag))
self.prefix = m.group(1)
self.suffix = m.group(3)
self._parse_prefix()
def _parse_prefix(self):
"""
Parses the first part of the filename.
If date or event are already set from the folder name, they are not
overwritten.
"""
prefixparts = self.prefix.split('-')
if len(prefixparts) >= 3:
if self.date == "":
self.date = prefixparts[0]
if self.event == "":
self.event = '-'.join(prefixparts[1:-1])
self.number = prefixparts[-1]
# The number could not be parsed yet, try to find a number. At this
# point, any number is fine.
if self.number == "":
numbers = re.findall(r"\d+", self.prefix)
if len(numbers) > 0:
self.number = numbers[-1]
if self.number == "":
global next_id
self.number = str(next_id)
next_id += 1
# In case anything is missing, this could not be parsed.
if self.date == "":
raise DateParseError('Could not parse “{}”.'.format(self.prefix))
if self.event == "":
raise EventParseError('Could not parse “{}”.'.format(self.prefix))
if self.number == "":
raise NumberParseError('Could not parse “{}”.'.format(self.prefix))
def _parse_folder_name(self):
"""
Parses date and event name from a folder name.
Sets :py:attr:`date` and :py:attr:`event`.
"""
if len(self.dirname) == 0:
return
pattern = re.compile(r"([012]\d{3}[01]\d[0123]\d)-([^/]+)/?")
album_dir = os.path.basename(self.dirname)
m = pattern.match(album_dir)
if m is None:
raise FolderParseError('Could not parse “{}”.'.format(album_dir))
self.date = m.group(1)
self.event = m.group(2)
def get_tags(self):
"""
Gives the list with all tags.
:return: A list with all tags.
:rtype: list
"""
return list(self.tags)
def _load_iptc(self):
"""
Loads the IPTC data from the original file and saves them with
:py:meth:`add_tag`.
"""
try:
self.iptc = IPTCInfo(self.origname, force=True)
except IOError as e:
pass
else:
logging.info('Found Tags “{}” in “{}”.'.format(
', '.join(sorted(self.iptc.keywords)), self.origname))
for keyword in self.iptc.keywords:
self.add_tag(Tag(keyword))
def write_iptc(self):
"""
Writes the IPTC data.
"""
self.iptc.data['keywords'] = list(sorted(self.get_tags()))
logging.info('Saving IPTC keywords to “{}”.'.format(self.origname))
self.iptc.save()
def name_changed(self):
"""
Checks whether the name that :py:meth:`current_path` gives is the same
than :py:attr:`origname`.
:return: Whether the filename was changed.
:rtype: bool
"""
return self.origname != self.current_path()
def iptc_changed(self):
"""
Check whether the tags match the IPTC tags.
:return: Whether the IPTC tags need to be rewritten.
:rtype: bool
"""
return sorted(map(Tag, self.iptc.keywords)) != sorted(self.get_tags())
def save(self):
"""
Renames the file and updates the IPTC fields.
"""
if self.iptc_changed():
self.write_iptc()
if self.name_changed():
self.rename()
def rename_to_temp(self):
"""
Renames the image to a temporary name.
It generates a random UUID 4 and renames the picture to it. The
temporary name is stored in the :py:attr:`tempname` attribute.
"""
self.tempname = str(uuid.uuid4())
os.rename(self.origname, self.tempname)
class PictureDBError(Exception):
"""
Exception class for this module.
"""
pass
class PictureParseError(PictureDBError):
"""
General exception class for parsing the information in the file names.
"""
pass
class FilenameParseError(PictureParseError):
pass
class FolderParseError(PictureParseError):
pass
class PrefixParseError(FilenameParseError):
pass
class NumberParseError(PrefixParseError):
pass
class EventParseError(PrefixParseError, FolderParseError):
"""
Error related to the parsing of the event from either folder or image.
"""
pass
class DateParseError(PrefixParseError, FolderParseError):
"""
Error related to the parsing the date from either the folder or the image
file.
"""
pass
class FilenameTooLongError(PictureDBError):
pass
def compress_numbers(images):
"""
Compresses the numbers in the filenames.
This has to be done to a whole collection so that they are numbered
correctly. The number of images are taken into account when padding the
number with leadings zeros.
:param images: Images to rename.
:type images: list
"""
image_count = len(images)
digit_count = len(str(image_count))
format_string = '{:0'+str(digit_count)+'d}'
for n, image in zip(itertools.count(1), images):
image.number = format_string.format(n)
def batch_rename(images):
"""
Renames a whole batch of images, avoiding temporary name collisions.
Say you have images like so:
- foo-0.jpg
- foo-1.jpg
- foo-2.jpg
If you use the compress option, the images will be renamed to:
- foo-1.jpg
- foo-2.jpg
- foo-3.jpg
The problem is that those names are always in use, so that renaming will
fail. This function renames all images to some temporary name and then
renames them all back.
"""
for image in images:
image.rename_to_temp()
for image in images:
image.rename()