def init_new_album(album_title, album_path, default_source_path, verbose): """ init_new_album: start or restart an album 20130103/RB: started function INPUT: - album_title: title of the page (as seen on the web page) - album_path: path to where the album is located on the computer - default_source_path: the default path to the folder with the originals INTERACTION: - the album will be saved. If it already exists it will ask for confirmation before it overwrites the old album. """ HBFUN.verbose("== INIT NEW ALBUM ==", True) # make new album album = HBAL.album(album_title = album_title, album_path = album_path, default_source_path = default_source_path, verbose = verbose) # save it, if it already exists, ask for confirmation if HBFUN.check_path_exists(pickle_path): if HBFUN.ask_user_confirmation("This will overwrite the old pickle. Destroy it? y/n"): HBAL.save_album(album, pickle_path) else: HBAL.save_album(album, pickle_path)
def make_folders(self, verbose = False): """ make_folders: create the web-folders 20130103/RB: started the function INPUT: None OUTPUT: - True: success, or False: failure """ HBFUN.verbose("\nHBalbum/make_folders()", verbose) if self.album_path == self._default_source_path: HBFUN.printError("album_path and default_source_path are the same, this is not allowed!", inspect.stack()) return False path = self.album_path + self.pics_dir HBFUN.check_and_make_folder(path, verbose) path = self.album_path + self.thumbs_dir HBFUN.check_and_make_folder(path, verbose) path = self.album_path + self.resources_dir HBFUN.check_and_make_folder(path, verbose) path = self.album_path + self.html_dir HBFUN.check_and_make_folder(path, verbose) return True
def set_folder_thumbnail(self, event_index, photo_index, verbose = False): """ set_folder_thumbnail: change the thumbnail used for an event 20130103/RB: started function INPUT: - event_index (int): index of the event in event_array. Use 'list_events' with 'show_photos = True' to find the correct index. - photo_index (int): index if the photo used for the thumbnail OUTPUT: - True: success, False: the one of the indices does not exist. """ try: HBFUN.verbose("set_folder_thumbnail, event: " + str(event_index) + " photo: " + str(photo_index), verbose) self.event_array[event_index].photo_array[photo_index] except IndexError: HBFUN.printError("Either the event_index or photo_index is incorrect", inspect.stack()) return False self.event_array[event_index].event_thumb = photo_index self.save_events_in_csv(verbose) return True
def add_photos(self, index, flag_new_properties_list = False, flag_redo_resize = False, flag_redo_thumbs = False, verbose = False): """ add_photos: add photos to an event 20130103/RB: started function INPUT: - index (int): index of the event in event_array. Use 'list_events' to find the correct index. - flag_new_properties_list (BOOL, False): complicated. The properties list is a csv-file with the file names and space for titles and caption for individual photos. You don't want to override it. The file is found in 'album_path + resources_dir + event_dir + 'props.csv'' - if the file does not exist, it will be made - if the file exists and the flag is False, it will not make a new one - if the file exists and the flag is True, it will make a new file, with a number appended to it (like 'props_1.csv'). THE OLD FILE WILL CONTINUE TO BE USED. You have to manually rename it to 'props.csv'. - flag_redo_resize (BOOL, False): the photos are copied from the source, resize and put in the destination folder. During this, the exif information is copied to the resized photos. - no photos found in destination: will always resize them - photos are found, flag is False: will not do anything - photos are found, flag is True: will resize the photos - flag_redo_thumbs (BOOL, False): similar to flag_redo_resize, but for the thumbnails. REMARK: This function is a convenient collection of steps. Error checking etc should be done in the respective functions. """ HBFUN.verbose("\nHBalbum/add_photos(): redo resize: " + str(flag_redo_resize), verbose) self.event_array[index].add_and_resize_photos(flag_redo_resize, verbose) self.event_array[index].add_and_resize_thumbs(flag_redo_thumbs, verbose) self.event_array[index].add_photos_to_array(verbose) if HBFUN.check_path_exists(self.album_path + self.resources_dir + self.event_array[index].event_dir) or flag_new_properties_list: self.event_array[index].make_new_properties_list(verbose) self.event_array[index].read_properties_list(verbose)
def make_new_properties_list(self, verbose = False): """ make_new_properties_list: write a new csv file where you can assign title and captions to photos 20130103/RB: started the function The property list can be found in 'album_path + resources_dir + event_dir + 'props.csv''. To prevent overwriting old files with all captions, new files will always have a number appended. You have to manually change it to 'props.csv', as this file is used. """ HBFUN.verbose("HBevent/make_new_properties_list()", verbose) # get the list with photos source_list = os.listdir(self.source_path + self.event_dir_src) source_list = [n for n in source_list if n[-4:] == ".jpg"] # create a save filename prop_fn = "props" prop_ext = "txt" path_and_filename = HBFUN.file_numbering(self.album_path + self.resources_dir + self.event_dir, prop_fn, prop_ext) # write the properties csvfile = open(path_and_filename, "wb") csvwriter = csv.writer(csvfile, delimiter = ";") for i in range(len(source_list)): csvwriter.writerow([source_list[i], " ", " "]) csvfile.close()
def __init__(self, album_title, album_path, default_source_path, verbose = False): """ __init__: start an album 20130103/RB: started the function INPUT: - album_title (str): title used for the site - album_path (str): the path with all the resized photos etc - default_source_path (str): the default path to the original photos OUTPUT: - True """ HBFUN.verbose("\nalbum/__init__", verbose) self.album_title = album_title # name of the album self._album_path = HBFUN.check_path(album_path, verbose) # path of the album self._default_source_path = HBFUN.check_path(default_source_path, verbose) self.events_csv_filename = "events" # default source of photos self._pics_dir = "pics/" # pictures self._thumbs_dir = "thumbs/" # thumbs self._resources_dir = "res/" # resources self._html_dir = "html/" # html self.event_array = [] # array with events self.make_folders(verbose)
def change_event_title(pickle_path, event_index, new_title, verbose): HBFUN.verbose("Change event title", verbose) album = HBAL.load_album(pickle_path) album.change_event_title(event_index, new_title) HBAL.save_album(album, pickle_path)
def check_event_exists(self, event_title, event_dir, verbose = False): """ check_event_exists 20130103/RB: started function INPUT: - event_title (str): the title of the event. It usually has the format 'Some event (July 2012)' - event_dir (str): the directory name of the event on the web OUTPUT: - True: if it does not exist, False if it does exist """ HBFUN.verbose("HBalbum/check_event_exists(): " + event_title + ", " + event_dir, verbose) ev_titles = [] ev_dirs = [] for i in range(len(self.event_array)): ev_titles.append(self.event_array[i].event_title) ev_dirs.append(self.event_array[i].event_dir) if event_title in ev_titles: HBFUN.printError("the event_title does already exist!", inspect.stack()) return False elif event_dir in ev_dirs: HBFUN.printError("the event_dir does already exist!", inspect.stack()) return False else: HBFUN.verbose(" HBalbum/check_event_exists(): event_title and event_dir do not yet exist", verbose) return True
def add_and_resize_thumbs(self, flag_redo_thumbs = False, verbose = False): """ add_and_resize_thumbs: copy photos from the source, resize and put them in web folder 20130103/RB: started the function See for details add_and_resize_photos. """ HBFUN.verbose("\nHBevent/add_and_resize_thumbs(): redo resize? " + str(flag_redo_thumbs), verbose) # compare the contents of the folders. No need to do double work! source_list = os.listdir(self.source_path + self.event_dir_src) thumbs_list = os.listdir(self.album_path + self.thumbs_dir + self.event_dir) # clean up the source list from non-jpgs source_list = [n for n in source_list if n[-4:] == ".jpg"] # remove items that already have been resized if flag_redo_thumbs: source_to_pics_list = source_list else: source_to_pics_list = [n for n in source_list if n not in thumbs_list] HBFUN.verbose(str(len(source_to_pics_list)) + " of " + str(len(source_list)) + " thumbs will be resized", verbose) src_path = self.source_path + self.event_dir_src dest_path = self.album_path + self.thumbs_dir + self.event_dir # actually resize the thumbs HBFUN.make_thumbs(src_path, dest_path, source_to_pics_list, self.thumb_size, verbose)
def make_html(pickle_path, verbose): """ make_html: compile the html 20130103/RB: started function """ HBFUN.verbose("Make html", verbose) album = HBAL.load_album(pickle_path) album = HBHTML.make_html(album, verbose = verbose) HBAL.save_album(album, pickle_path) # to save the newly imported properties
def disable_photo(pickle_path, event_index, photo_index, disable, verbose = False): """ disable_photo: see disable_event for info 20130103/RB: started function """ HBFUN.verbose("Disable photo", verbose) album = HBAL.load_album(pickle_path) album.disable_photo(event_index, photo_index, disable, verbose = True) HBAL.save_album(album, pickle_path)
def add_event(self, index, event_title, event_dir, event_dir_src = "", source_path = "", verbose = False): """ add_event: add an event to the album 20130103/RB: started the function INPUT: - index (int): position of the new album. Use 'list_events' to find the correct position. The newest event should have index = 0 - event_title (str): the title of the event. It usually has the format 'Some event (July 2012)' - event_dir (str): the directory name of the event on the web - event_dir_src (str, opt): if the source directory (with the original photos) has a different directory name - source_path (str, opt): is the source_path of the original photos is not the default. OUTPUT: - True: success or False: fail """ HBFUN.verbose("HBalbum/add_event(): " + str(index) + ", " + event_title + ", " + event_dir + ", " + source_path, verbose) # if no new source_path is given, use the default if source_path == "": source_path = self.default_source_path # if a new source_path is given, check for correctness else: source_path = HBFUN.check_path(source_path, verbose) if self.album_path == source_path: HBFUN.printError("album_path and source_path are the same, this is not allowed!", inspect.stack()) return False # check the event_dir event_dir = HBFUN.check_path(event_dir) # is the source event dir different? if event_dir_src == "": event_dir_src = event_dir else: event_dir_src = HBFUN.check_path(event_dir_src, verbose) # check if the source exists if HBFUN.check_path_exists(source_path + event_dir_src) == False: HBFUN.printError("The source path does not exist!", inspect.stack()) return False # check if the event already exists. If not, make it if self.check_event_exists(event_title, event_dir, verbose): ev = HBEV.event(event_title, event_dir, event_dir_src, self.album_title, self.album_path, source_path, self.pics_dir, self.thumbs_dir, self.resources_dir, self.html_dir, verbose) self.event_array.insert(index, ev) self.save_events_in_csv(verbose) return True
def change_event_title(self, event_index, new_title, verbose = False): """ change_event_title: change the title 20130127/RB: wrote this function """ HBFUN.verbose("HBalbum/change_event_title()", verbose) self.event_array[event_index].event_title = new_title return True
def disable_event(pickle_path, event_index, disable, verbose = False): """ disable_event: disable an event. This will exclude it from the events gallery, but it will not remove the html code itself 20130103/RB: started function - event_index (int): index of the event in event_array. Use 'list_events' to find the correct index. If index is -1, it will affect all albums. - disable (BOOL): True to disable, False to enable. If you try to re-disable an event, it will give a warning and continue """ HBFUN.verbose("Disable event", verbose) album = HBAL.load_album(pickle_path) album.disable_event(index = event_index, disable = disable, verbose = verbose) HBAL.save_album(album, pickle_path)
def list_events(pickle_path, show_photos = False, verbose = False): """ list_events: list events, and maybe also photos 20130103/RB: started function INPUT: - pickle_path (str): path and filename of the pickle - show_photos (BOOL, False): if True, it will also list the photos """ HBFUN.verbose("List events", verbose) album = HBAL.load_album(pickle_path) album.list_events(show_photos)
def change_event_thumb(pickle_path, event_index, photo_index, verbose): """ change_event_thumb: change the thumbnail used for the event 20130103/RB: started function INPUT: - event_index (int): a valid index of an event - photo_index: a valid index of a photo """ HBFUN.verbose("Change event thumbnail", verbose) album = HBAL.load_album(pickle_path) album.set_folder_thumbnail(event_index, photo_index) HBAL.save_album(album, pickle_path)
def load_events_from_csv(pickle_path, verbose): """ load_events_from_csv: should only be used if you had to re-init an album 20130105/RB: started function INPUT: - events_csv_filename (str): the filename, without extension, of the csv file. Warning: save_file_in_csv will number filenames! - pickle_path: path and filename of the pickle """ HBFUN.verbose("load_events_from_csv", verbose) album = HBAL.load_album(pickle_path) album.load_events_from_csv(verbose) HBAL.save_album(album, pickle_path)
def __init__(self, event_title, event_dir, event_dir_src, album_title, album_path, source_path, pics_dir, thumbs_dir, resources_dir, html_dir, verbose = False): """ __init__: start a new event 20130103/RB: started the function INPUT: - event_title (str): title of the event - event_dir (str): the directory name of the event on the web - event_dir_src (str): the directory name of the event on the computer - source_path (str): source path, where the photos can be found - album_title, album_path, pics_dir, thumbs_dir, resources_dir, html_dir OUTPUT: - True """ HBFUN.verbose("HBevent/__init__: " + event_title + ", " + event_dir + ", " + album_title + " etc.", verbose) # "inherited" self.album_title = album_title self.album_path = album_path self.source_path = source_path self.pics_dir = pics_dir self.thumbs_dir = thumbs_dir self.resources_dir = resources_dir self.html_dir = html_dir # own self.event_title = event_title self.event_dir = event_dir # correctness already checked by album self.event_dir_src = event_dir_src # source of the pics self.disabled = False # disable but not delete event self.photo_array = [] # array with photos self.event_thumb = 0 # index of thumb for event self.max_pic_size = 1500 # max size for pictures self.thumb_size = 150 # size for thumbs self.make_folders(verbose)
def disable_photo(self, event_index, photo_index, disable, verbose = False): """ disable_photo: disable or enable an event. 20130103/RB: started function INPUT: - event_index (int): index of the event in event_array. Use 'list_events' to find the correct index. - disable (BOOL): True to disable, False to enable. If you try to re-disable an event, it will give a warning and continue OUTPUT: - True: success or False: failure """ # use a bit of shorthand... sorry ev = self.event_array[event_index] ph = ev.photo_array[photo_index] if disable: dis = "disabled" else: dis = "enabled" HBFUN.verbose("HBalbum/disable_photo(): event " + ev.event_title + " photo " + ph.photo_filename + " to " + dis, verbose) if event_index > len(self.event_array): HBFUN.printError("The event index exceeds the length of the event_array", inspect.stack()) return False if photo_index > len(ev.photo_array): HBFUN.printError("The photo_index exceeds the length of the photo_array", inspect.stack()) return False if ph.disabled == disable: HBFUN.printWarning("Event " + ev.event_title + " photo " + ph.photo_filename + " is already " + dis, inspect.stack()) HBFUN.verbose(" event_title: " + ev.event_title, verbose) HBFUN.verbose(" event_dir: " + ev.event_dir, verbose) HBFUN.verbose(" photo_title: " + ph.photo_title, verbose) HBFUN.verbose(" photo_filename: " + ph.photo_filename, verbose) ph.disabled = disable return True
def add_event(pickle_path, event_index, event_title, event_dir, event_dir_src = "", source_path = "", verbose = False): """ add_event: add an event to the album and add the photos to the event 20130103/RB: started function INPUT: - pickle_path (str): path and filename of the pickle - index (int): position of the new album. Use 'list_events' to find the correct position. The newest event should have index = 0 - event_title (str): the title of the event. It usually has the format 'Some event (July 2012)' - event_dir (str): the directory name of the event on the web - event_dir_src (str, opt): if the source directory (with the original photos) has a different directory name - source_path (str, opt): if the source_path of the original photos is not the default. """ HBFUN.verbose("Add event", verbose) album = HBAL.load_album(pickle_path) album.add_event(event_index, event_title, event_dir, event_dir_src, source_path = source_path, verbose = verbose) album.add_photos(event_index, flag_new_properties_list = False, flag_redo_resize = False, flag_redo_thumbs = False, verbose = verbose) HBAL.save_album(album, pickle_path)
def add_photos_to_array(self, verbose = False): """ add_photos_to_array: actually add photos to the photo_array 20130103/RB: started the function If the photos are already in the array, it will skip them. """ HBFUN.verbose("HBevent/add_photos_to_array()", verbose) # get the list with photos already in photo_array photo_array_fn = self.get_photo_filename_from_photo_array() # get the list with photos source_list = os.listdir(self.source_path + self.event_dir_src) source_list = [n for n in source_list if n[-4:] == ".jpg"] # only add new photos to the array add_to_photo_array = [n for n in source_list if n not in photo_array_fn] if add_to_photo_array == []: HBFUN.verbose(" all photos are already in photo_array", verbose) return True for photo_filename in add_to_photo_array: # read the exif. this has to be done from the source exif = HBFUN.get_exif(self.source_path + self.event_dir_src + photo_filename) # make a photo object ph = HBPH.photo(self.album_path, self.event_dir, photo_filename, exif) # add object to photo_array self.photo_array.append(ph) return True
def add_and_resize_photos(self, flag_redo_resize = False, verbose = False): """ add_and_resize_photos: copy photos from the source, resize and put them in web folder 20130103/RB: started the function INPUT: - flag_redo_resize (BOOL, False): the photos are copied from the source, resize and put in the destination folder. During this, the exif information is copied to the resized photos. - no photos found in destination: will always resize them - photos are found, flag is False: will not do anything - photos are found, flag is True: will resize the photos REMARKS: The function reads the photos in the source and destination folder into two arrays. It compares the arrays and removes the ones that have already been resized (unless flag_redo_resize = True). """ HBFUN.verbose("\nHBevent/add_and_resize_photos(): redo resize? " + str(flag_redo_resize), verbose) # compare the contents of the folders. No need to do double work! source_list = os.listdir(self.source_path + self.event_dir_src) pics_list = os.listdir(self.album_path + self.pics_dir + self.event_dir) # clean up the source list from non-jpgs source_list = [n for n in source_list if n[-4:] == ".jpg"] # remove items that already have been resized if flag_redo_resize: source_to_pics_list = source_list else: source_to_pics_list = [n for n in source_list if n not in pics_list] HBFUN.verbose(str(len(source_to_pics_list)) + " of " + str(len(source_list)) + " pics will be resized", verbose) src_path = self.source_path + self.event_dir_src dest_path = self.album_path + self.pics_dir + self.event_dir # actually resize the photos HBFUN.resize_pics(src_path, dest_path, source_to_pics_list, self.max_pic_size, verbose)
def load_events_from_csv(self, verbose = False): """ 20130105/RB: started the function """ HBFUN.verbose("HBalbum/load_events_in_csv()", verbose) path_and_filename = self.album_path + self.resources_dir + self.events_csv_filename + ".txt" csvfile = open(path_and_filename, "rb") csvreader = csv.reader(csvfile, delimiter=';') temp_ev_list = [] for row in csvreader: temp_ev_list.append(row) for i in range(len(temp_ev_list)): self.add_event( index = int(temp_ev_list[i][0]), event_title = temp_ev_list[i][1], event_dir = temp_ev_list[i][2], event_dir_src = temp_ev_list[i][3], source_path = temp_ev_list[i][4], verbose = verbose) for i in range(len(temp_ev_list)): self.add_photos( index = int(temp_ev_list[i][0]), flag_new_properties_list = False, flag_redo_resize = False, flag_redo_thumbs = False, verbose = verbose) self.set_folder_thumbnail(int(temp_ev_list[i][0]), int(temp_ev_list[i][5]))
def read_properties_list(self, verbose = False): """ read_properties_list: read the csv properties list and write the titles and captions to the photo objects 20130103/RB: started the function """ HBFUN.verbose("HBevent/read_properties_list()", verbose) photo_array_fn = self.get_photo_filename_from_photo_array() path_and_filename = self.album_path + self.resources_dir + self.event_dir + "props.txt" csvfile = open(path_and_filename, "rb") csvreader = csv.reader(csvfile, delimiter=';') props_list = [] for row in csvreader: props_list.append(row) for i in range(len(props_list)): # we assume the photos are in the same order... self.photo_array[i].photo_title = props_list[i][1] self.photo_array[i].photo_caption = props_list[i][2]
def make_folders(self, verbose = False): """ make_folders: create the event specific folders 20130103/RB: started the function To clean up __init__ """ HBFUN.verbose("\nHBevent/make_folders()", verbose) path = self.album_path + self.pics_dir + self.event_dir HBFUN.check_and_make_folder(path, verbose) path = self.album_path + self.thumbs_dir + self.event_dir HBFUN.check_and_make_folder(path, verbose) path = self.album_path + self.resources_dir + self.event_dir HBFUN.check_and_make_folder(path, verbose) path = self.album_path + self.html_dir + self.event_dir HBFUN.check_and_make_folder(path, verbose) return True
def disable_event(self, index, disable, verbose = False): """ disable_event: disable or enable an event. 20130103/RB: started function INPUT: - index (int): index of the event in event_array. Use 'list_events' to find the correct index. If index is -1, it will affect all albums. - disable (BOOL): True to disable, False to enable. If you try to re-disable an event, it will give a warning and continue OUTPUT: - True: success or False: failure """ ev = ev = self.event_array[index] if disable: dis = "disabled" else: dis = "enabled" HBFUN.verbose("HBalbum/disable_event(): index " + str(index) + " to " + dis, verbose) if index > len(self.event_array) or index < -1: HBFUN.printError("The index exceeds the length of the array", inspect.stack()) return False if index == -1: HBFUN.verbose(" all events will be set to " + dis, verbose) for i in range(len(self.event_array)): ev.disabled = disable else: if ev.disabled == disable: HBFUN.printWarning("Event " + ev.event_title + " is already " + dis, inspect.stack()) HBFUN.verbose(" event_title: " + ev.event_title, verbose) HBFUN.verbose(" event_dir: " + ev.event_dir, verbose) ev.disabled = disable return True
def make_html(album, verbose = False): """ make_html: the do-all function to generate html 20130103/RB: started the function 20130127/RB: generate RSS feed at end of this function INPUT: - album (HBAL.album) """ HBFUN.verbose("make_html: Hi!", True) # some general strings no_nav = "" # value if there is no navigation element (first picture or so) # make album HBFUN.verbose(" make album: " + album.album_title, verbose) f = open(album.album_path + album.html_dir + "index.html", "wb") make_html_header(f, album.album_title) make_html_navigation(f, album.album_title) gallery = prepare_album_gallery(album) make_html_gallery(f, gallery) make_html_footer(f) f.close() # make event pages ev_array = range(len(album.event_array)) ev_array = [n for n in ev_array if album.event_array[n].disabled == False] for i in ev_array: #range(len(album.event_array)): event = album.event_array[i] # filter disabled photos ph_array = range(len(event.photo_array)) ph_array = [n for n in ph_array if event.photo_array[n].disabled == False] if ph_array == []: HBFUN.printWarning(event.event_title + " does not contain any photos or all photos are disabled. The event will be skipped!", inspect.stack()) else: event.read_properties_list(verbose) HBFUN.verbose(" make event: " + event.event_title, verbose) f = open(album.album_path + album.html_dir + event.event_dir + "index.html", "wb") page_title = album.album_title + " - " + event.event_title make_html_header(f, page_title, css_path = "../") if i == 0: prev_nav = no_nav else: prev_nav = "../" + album.event_array[i-1].event_dir + "index.html" if i == len(album.event_array) - 1: next_nav = no_nav else: next_nav = "../" + album.event_array[i+1].event_dir + "index.html" up_nav = "../index.html" make_html_navigation(f, event.event_title, prev_nav, up_nav, next_nav) gallery = prepare_event_gallery(event) make_html_gallery(f, gallery) make_html_footer(f) f.close() # make photo pages for j in ph_array: photo = event.photo_array[j] HBFUN.verbose(" make photo: " + photo.photo_title, verbose) f = open(album.album_path + album.html_dir + event.event_dir + photo.photo_html_name, "wb") page_title = album.album_title + " - " + event.event_title if photo.photo_title: page_title += " - " + photo.photo_title make_html_header(f, page_title, css_path = "../") if j == 0: prev_nav = no_nav else: prev_nav = event.photo_array[j-1].photo_html_name if j == len(event.photo_array) - 1: next_nav = no_nav else: next_nav = event.photo_array[j+1].photo_html_name up_nav = "index.html" make_html_navigation(f, event.event_title, prev_nav, up_nav, next_nav) make_photo_html(f, photo) make_html_footer(f) f.close() generate_rss(album) HBFUN.verbose("make_html: Bye!", True) return album