|
| 1 | +""" |
| 2 | +Defines a set of functions that handle naming of the downloaded files. |
| 3 | +""" |
| 4 | + |
| 5 | +from collections import defaultdict |
| 6 | + |
| 7 | + |
| 8 | +def title(pset, photo, suffix): |
| 9 | + """ |
| 10 | + Name file after title. |
| 11 | +
|
| 12 | + @param pset: Flickr.Photoset, the photoset |
| 13 | + @param photo: Flickr.Photo, the photo |
| 14 | + @param suffice: str, optional suffix |
| 15 | + @return: str, the filename |
| 16 | + """ |
| 17 | + return '{0}{1}.jpg'.format(photo.title, suffix) |
| 18 | + |
| 19 | + |
| 20 | +def idd(pset, photo, suffix): |
| 21 | + """ |
| 22 | + Name file after id |
| 23 | +
|
| 24 | + @param pset: Flickr.Photoset, the photoset |
| 25 | + @param photo: Flickr.Photo, the photo |
| 26 | + @param suffice: str, optional suffix |
| 27 | + @return: str, the filename |
| 28 | + """ |
| 29 | + return '{0}{1}.jpg'.format(photo.id, suffix) |
| 30 | + |
| 31 | + |
| 32 | +def title_and_id(pset, photo, suffix): |
| 33 | + """ |
| 34 | + Name file after title and photo id |
| 35 | +
|
| 36 | + @param pset: Flickr.Photoset, the photoset |
| 37 | + @param photo: Flickr.Photo, the photo |
| 38 | + @param suffice: str, optional suffix |
| 39 | + @return: str, the filename |
| 40 | + """ |
| 41 | + return '{0}-{1}{2}.jpg'.format(photo.title, photo.id, suffix) |
| 42 | + |
| 43 | + |
| 44 | +INCREMENT_INDEX = defaultdict(lambda: defaultdict(int)) |
| 45 | +"""Photoset -> filename index for title_increment function duplicate tracking""" |
| 46 | + |
| 47 | + |
| 48 | +def title_increment(pset, photo, suffix): |
| 49 | + """ |
| 50 | + Name file after photo title, but add an incrementing counter on duplicates |
| 51 | +
|
| 52 | + @param pset: Flickr.Photoset, the photoset |
| 53 | + @param photo: Flickr.Photo, the photo |
| 54 | + @param suffice: str, optional suffix |
| 55 | + @return: str, the filename |
| 56 | + """ |
| 57 | + extra = '' |
| 58 | + photo_index = INCREMENT_INDEX[pset.id][photo.title] |
| 59 | + if photo_index: |
| 60 | + extra = '({0})'.format(photo_index) |
| 61 | + INCREMENT_INDEX[pset.id][photo.title] += 1 |
| 62 | + return '{0}{1}{2}.jpg'.format(photo.title, suffix, extra) |
| 63 | + |
| 64 | + |
| 65 | +HANDLERS = { |
| 66 | + 'title': title, |
| 67 | + 'id': idd, |
| 68 | + 'title_and_id': title_and_id, |
| 69 | + 'title_increment': title_increment, |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +def get_filename_handler(name='title'): |
| 74 | + """ |
| 75 | + Returns the given filename handler as a function |
| 76 | + @param name: str, name of the handler to return |
| 77 | + @return: Function, handler |
| 78 | + """ |
| 79 | + return HANDLERS[name] |
0 commit comments