From e825818b0da9a7d954b54b2cfa1e4a659e202e50 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 6 May 2011 23:26:52 +0200 Subject: [PATCH] get metadata from album_description file --- sigal/image.py | 13 +++++-- sigal/theme.py | 103 +++++++++++++++++++++++++++++++------------------ 2 files changed, 74 insertions(+), 42 deletions(-) diff --git a/sigal/image.py b/sigal/image.py index e774d6f..9441766 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -25,7 +25,9 @@ Resize images, create thumbnails with some options (squared thumbs, ...). import os import Image import ImageDraw +from shutil import copy2 +DESCRIPTION_FILE = "album_description" class Gallery: "Prepare a gallery of images" @@ -43,7 +45,7 @@ class Gallery: self.jpgquality = params.getint('sigal', 'jpg_quality') self.exif = params.getint('sigal', 'exif') self.copyright = params.get('sigal', 'copyright') - self.fileExtList = params.get('sigal', 'fileExtList') + self.fileExtList = params.get('sigal', 'fileExtList').split(',') def getsize(self, string): "split size string to a tuple of int" @@ -57,9 +59,8 @@ class Gallery: for dirpath, dirnames, filenames in os.walk(self.input_dir): # filelist = [os.path.normcase(f) for f in os.listdir(dir)] imglist = [os.path.join(dirpath, f) for f in filenames \ - if os.path.splitext(f)[1] in self.fileExtList.split(',')] - if len(imglist) != 0: - yield dirpath, dirnames, imglist + if os.path.splitext(f)[1] in self.fileExtList] + yield dirpath, dirnames, imglist def build(self, input_dir, output_dir, force=False): "create image gallery" @@ -84,6 +85,10 @@ class Gallery: if not os.path.isdir(img_dir): os.mkdir(img_dir) + descfile = os.path.join(dirpath, DESCRIPTION_FILE) + if os.path.isfile(descfile): + copy2(descfile, img_dir) + if len(imglist) != 0: thumb_dir = os.path.join(img_dir, self.thumb_dir) if not os.path.isdir(thumb_dir): diff --git a/sigal/theme.py b/sigal/theme.py index 5a1b909..fc890e2 100644 --- a/sigal/theme.py +++ b/sigal/theme.py @@ -1,9 +1,12 @@ #!/usr/bin/env python2 # -*- coding:utf-8 -*- -"""render theme for sigal""" +""" +Generate html pages for each directory of images +""" import os +import codecs import Image from distutils.dir_util import copy_tree from jinja2 import Environment, PackageLoader @@ -24,7 +27,7 @@ class Theme(): self.bigimg_dir = params.get('sigal', 'bigimg_dir') self.thumb_dir = params.get('sigal', 'thumb_dir') self.thumb_prefix = params.get('sigal', 'thumb_prefix') - self.fileExtList = params.get('sigal', 'fileExtList') + self.fileExtList = params.get('sigal', 'fileExtList').split(',') if params.has_option('sigal', 'theme'): theme = params.get('sigal', 'theme') @@ -46,31 +49,47 @@ class Theme(): if os.path.splitext(f)[1] in self.fileExtList] self.data[dirpath]['subdir'] = [d for d in dirnames if d not in ignored] - def get_metadata(self, path): - descfile = os.path.join(path, ALBUM_DESCRIPTION) + def get_meta_value(self, data): + """ + Return the value for a line like: + key = "value" + """ + data = data.split('=')[1].strip() + + # Strip quotes + if data[0] == '"': + data = data[1:] + if data[-1] == '"': + data = data[:-1] + + return data + + + def get_metadata(self, path): + """ + Get album metadata from DESCRIPTION_FILE: + - album_name + - album_description + - album_representative + """ + descfile = os.path.join(path, DESCRIPTION_FILE) if not os.path.isfile(descfile): return - with open(descfile, 'r') as f: + with codecs.open(descfile, "r", "utf-8") as f: for l in f: if "album_name" in l: - self.data[path]['title'] = l.split('=')[1] + self.data[path]['title'] = self.get_meta_value(l) if "album_description" in l: - self.data[path]['description'] = l.split('=')[1] - - - # self.meta = {} - # self.meta['title'] = params.get('album', 'title') \ - # if params.has_option('album', 'title') else '' - # self.meta['author'] = params.get('album', 'author') \ - # if params.has_option('album', 'author') else '' - # self.meta['description'] = params.get('album', 'description') \ - # if params.has_option('album', 'description') else '' + self.data[path]['description'] = self.get_meta_value(l) + if "album_representative" in l: + self.data[path]['representative'] = self.get_meta_value(l) def find_representative(self, path): - """ find the representative image for a given album/path + """ + find the representative image for a given album/path at the moment, this is the first image found. """ @@ -87,25 +106,28 @@ class Theme(): return files[0] def render(self): + """ + Render the html page + """ - copy_tree(os.path.abspath(self.theme_dir), - os.path.abspath(self.path)) + copy_tree(os.path.abspath(self.theme_dir), os.path.abspath(self.path)) self.directory_list() - self.get_metadata() + for dirpath in self.data.keys(): + self.get_metadata(dirpath) + # print self.data[dirpath] + + gallery_name = self.data[self.path]['title'] + self.data[self.path]['title'] = '' # loop on directories for dirpath in self.data.keys(): - - print self.data[dirpath] - theme = { 'path': os.path.relpath(self.path, dirpath) } home_path = os.path.join(os.path.relpath(self.path, dirpath), INDEX_PAGE) - gallery_name = "" - if dirpath != self.path: - gallery_name = os.path.basename(dirpath).replace('_',' ').\ - replace('-',' ').capitalize() + if not self.data[dirpath].has_key('title'): + self.data[dirpath]['title'] = os.path.basename(dirpath).replace('_',' ').\ + replace('-',' ').capitalize() images = [] for i in self.data[dirpath]['img']: @@ -114,22 +136,27 @@ class Theme(): 'thumb': os.path.join(self.thumb_dir, self.thumb_prefix+i) } images.append(image) - # print image albums = [] for d in self.data[dirpath]['subdir']: - alb_thumb = self.find_representative(os.path.join(dirpath, d)) - album = {'path': os.path.join(d, INDEX_PAGE), - 'name': d, - 'thumb': os.path.join(d, self.thumb_dir, - self.thumb_prefix+alb_thumb), - } - albums.append(album) - # print album + alb_thumb = '' + if self.data[dirpath].has_key('representative'): + alb_thumb = self.data[dirpath]['representative'] - page = self.template.render(self.meta, gallery_name=gallery_name, + if not alb_thumb or not os.path.isfile(alb_thumb): + alb_thumb = self.find_representative(os.path.join(dirpath, d)) + + album = { + 'path': os.path.join(d, INDEX_PAGE), + 'name': d, + 'thumb': os.path.join(d, self.thumb_dir, + self.thumb_prefix+alb_thumb), + } + albums.append(album) + + page = self.template.render(self.data[dirpath], gallery_name=gallery_name, home_path=home_path, images=images, - albums=albums, theme=theme) + albums=albums, theme=theme).encode('utf-8') # save f = open(os.path.join(dirpath, INDEX_PAGE),"w")