Fix problem when build recursive gallery

This commit is contained in:
Titoko 2016-05-21 10:17:32 +02:00
parent 0bf5187796
commit bd8705bd65

View File

@ -232,29 +232,29 @@ def init():
return settings return settings
def build_gallery(gallery, settings,templates, parents_gallery=False): def build_gallery(gallery, settings, templates, parent_galleries=False):
gallery_index_template = templates.get_template("gallery-index.html")
page_template = templates.get_template("page.html") if parent_galleries:
gallery_cover = {} gallery_path = os.path.join(parent_galleries, gallery)
if parents_gallery:
parents_gallery = os.path.join(parents_gallery, gallery)
else: else:
parents_gallery = gallery gallery_path = gallery
dirs = filter(lambda x: x not in (".", "..") and os.path.isdir(os.path.join(parents_gallery,x)) and os.path.exists( gallery_settings = yaml.safe_load(open(os.path.join(os.getcwd(), gallery_path, "settings.yaml"), "r"))
os.path.join(os.getcwd(), gallery, x, "settings.yaml")), os.listdir(os.path.join(os.getcwd(), gallery)))
if not os.path.exists(os.path.join("build", gallery)):
if parents_gallery:
os.makedirs(os.path.join("build", parents_gallery, gallery))
else:
os.makedirs(os.path.join("build", gallery))
gallery_settings = yaml.safe_load(open(os.path.join(os.getcwd(), gallery, "settings.yaml"), "r"))
error(isinstance(gallery_settings, dict), "Your %s should be a dict" % (os.path.join(gallery, "settings.yaml"))) error(isinstance(gallery_settings, dict), "Your %s should be a dict" % (os.path.join(gallery, "settings.yaml")))
error(gallery_settings.get("title"), "You should specify a title in %s" % (os.path.join(gallery, "settings.yaml"))) error(gallery_settings.get("title"), "You should specify a title in %s" % (os.path.join(gallery, "settings.yaml")))
gallery_index_template = templates.get_template("gallery-index.html")
page_template = templates.get_template("page.html")
gallery_cover = {}
dirs = filter(lambda x: x not in (".", "..") and os.path.isdir(os.path.join(gallery_path, x)) and
os.path.exists(os.path.join(os.getcwd(), gallery_path, x, "settings.yaml")),
os.listdir(os.path.join(os.getcwd(), gallery_path)))
if not os.path.exists(os.path.join("build", gallery_path)):
os.makedirs(os.path.join("build", gallery_path))
if gallery_settings.get("public", True) or dirs: if gallery_settings.get("public", True) or dirs:
error(gallery_settings.get("title"), "Your gallery describe in %s need to have a " error(gallery_settings.get("title"), "Your gallery describe in %s need to have a "
"title" % (os.path.join(gallery, "settings.yaml"))) "title" % (os.path.join(gallery, "settings.yaml")))
@ -262,10 +262,12 @@ def build_gallery(gallery, settings,templates, parents_gallery=False):
"in %s" % (os.path.join(gallery, "settings.yaml"))) "in %s" % (os.path.join(gallery, "settings.yaml")))
if isinstance(gallery_settings["cover"], dict): if isinstance(gallery_settings["cover"], dict):
cover_image_path = os.path.join(gallery, gallery_settings["cover"]["name"]) cover_image_path = os.path.join(gallery_path, gallery_settings["cover"]["name"])
cover_image_url = os.path.join(gallery, gallery_settings["cover"]["name"])
cover_image_type = gallery_settings["cover"]["type"] cover_image_type = gallery_settings["cover"]["type"]
else: else:
cover_image_path = os.path.join(gallery, gallery_settings["cover"]) cover_image_path = os.path.join(gallery_path, gallery_settings["cover"])
cover_image_url = os.path.join(gallery, gallery_settings["cover"])
cover_image_type = "image" cover_image_type = "image"
error(os.path.exists(cover_image_path), "File for %s cover image doesn't exist at " error(os.path.exists(cover_image_path), "File for %s cover image doesn't exist at "
@ -278,29 +280,31 @@ def build_gallery(gallery, settings,templates, parents_gallery=False):
"date": gallery_settings.get("date", ""), "date": gallery_settings.get("date", ""),
"tags": gallery_settings.get("tags", ""), "tags": gallery_settings.get("tags", ""),
"cover_type": cover_image_type, "cover_type": cover_image_type,
"cover": cover_image_path, "cover": cover_image_url,
} }
if dirs: if dirs:
sub_page_galleries_cover = [] sub_page_galleries_cover = []
for subgallery in dirs: for subgallery in dirs:
sub_page_galleries_cover.append(build_gallery(subgallery, settings, templates, gallery)) sub_page_galleries_cover.append(
build_gallery(subgallery, settings, templates, gallery_path)
)
build_index(settings, sub_page_galleries_cover, templates) build_index(settings, sub_page_galleries_cover, templates, gallery_path)
return gallery_cover return gallery_cover
# this should probably be a factory # this should probably be a factory
Image.base_dir = os.path.join(os.getcwd(), gallery) Image.base_dir = os.path.join(os.getcwd(), gallery_path)
Image.target_dir = os.path.join(os.getcwd(), "build", gallery) Image.target_dir = os.path.join(os.getcwd(), "build", gallery_path)
Video.base_dir = os.path.join(os.getcwd(), gallery) Video.base_dir = os.path.join(os.getcwd(), gallery_path)
Video.target_dir = os.path.join(os.getcwd(), "build", gallery) Video.target_dir = os.path.join(os.getcwd(), "build", gallery_path)
template_to_render = page_template if gallery_settings.get("static") else gallery_index_template template_to_render = page_template if gallery_settings.get("static") else gallery_index_template
index_html = open(os.path.join("build", gallery, "index.html"), "w") index_html = open(os.path.join("build", gallery_path, "index.html"), "w")
index_html.write(template_to_render.render( index_html.write(template_to_render.render(
settings=settings, settings=settings,
@ -313,19 +317,19 @@ def build_gallery(gallery, settings,templates, parents_gallery=False):
return gallery_cover return gallery_cover
def build_index(settings, galleries_cover, templates): def build_index(settings, galleries_cover, templates, gallery_path=''):
index_template = templates.get_template("index.html") index_template = templates.get_template("index.html")
galleries_cover = reversed(sorted(filter(lambda x: x != {}, galleries_cover), key=lambda x: x["date"])) galleries_cover = reversed(sorted(filter(lambda x: x != {}, galleries_cover), key=lambda x: x["date"]))
# this should probably be a factory # this should probably be a factory
Image.base_dir = os.getcwd() Image.base_dir = os.path.join(os.getcwd(), gallery_path)
Image.target_dir = os.path.join(os.getcwd(), "build") Image.target_dir = os.path.join(os.getcwd(), "build", gallery_path)
Video.base_dir = os.getcwd() Video.base_dir = os.path.join(os.getcwd(), gallery_path)
Video.target_dir = os.path.join(os.getcwd(), "build") Video.target_dir = os.path.join(os.getcwd(), "build", gallery_path)
index_html = open(os.path.join("build", "index.html"), "w") index_html = open(os.path.join("build", gallery_path, "index.html"), "w")
index_html.write(index_template.render( index_html.write(index_template.render(
settings=settings, settings=settings,